班级' PayPal'没找到 - Laravel 5

时间:2015-06-08 11:49:58

标签: laravel laravel-5

我正在尝试使用此软件包将Paypal集成到我的网络应用中:

http://packalyst.com/packages/package/netshell/paypal

我按照说明安装了它。

控制器是:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use PayPal;
use Redirect;

use Illuminate\Http\Request;

class PayPalController extends Controller {

    private $_apiContext;
    public function __construct() {
        $this->_apiContext = PayPal::ApiContext(
            config('services.paypal.client_id'),
            config('services.paypal.secret'));

        $this->_apiContext->setConfig(array(
            'mode' => 'sandbox',
            'service.EndPoint' => 'https://api.sandbox.paypal.com',
            'http.ConnectionTimeOut' => 30,
            'log.LogEnabled' => true,
            'log.FileName' => storage_path('logs/paypal.log'),
            'log.LogLevel' => 'FINE'
        ));
    }

    public function getCheckout() {
        $payer = PayPal::Payer();
        $payer->setPaymentMethod('paypal');

        $amount = PayPal:: Amount();
        $amount->setCurrency('EUR');
        $amount->setTotal(42); // This is the simple way,
        // you can alternatively describe everything in the order separately;
        // Reference the PayPal PHP REST SDK for details.

        $transaction = PayPal::Transaction();
        $transaction->setAmount($amount);
        $transaction->setDescription('What are you selling?');

        $redirectUrls = PayPal:: RedirectUrls();
        $redirectUrls->setReturnUrl(action('PayPalController@getDone'));
        $redirectUrls->setCancelUrl(action('PayPalController@getCancel'));

        $payment = PayPal::Payment();
        $payment->setIntent('sale');
        $payment->setPayer($payer);
        $payment->setRedirectUrls($redirectUrls);
        $payment->setTransactions(array($transaction));

        $response = $payment->create($this->_apiContext);
        $redirectUrl = $response->links[1]->href;

        return Redirect::to( $redirectUrl );
    }

    public function getDone(Request $request) {
        $id = $request->get('paymentId');
        $token = $request->get('token');
        $payer_id = $request->get('PayerID');

        $payment = PayPal::getById($id, $this->_apiContext);

        $paymentExecution = PayPal::PaymentExecution();

        $paymentExecution->setPayerId($payer_id);
        $executePayment = $payment->execute($paymentExecution, $this->_apiContext);

        // Clear the shopping cart, write to database, send notifications, etc.

        // Thank the user for the purchase
        return view('checkout.done');
    }

    public function getCancel() {
        // Curse and humiliate the user for cancelling this most sacred payment (yours)
        return view('checkout.cancel');
    }

}

然后我创建了一条路线:

Route::get('/paypal/checkout', [
    'as' => 'get-paypal-checkout', 'uses' => 'PayPalController@getCheckout'
]);

但是,当我走那条路时,我得到了:

  

PayPalController.php第14行中的FatalErrorException:   班级&#39; PayPal&#39;未找到   在PayPalController.php第14行

从我所看到的情况来看,我已正确完成了我的命名空间和use。 这一切都是新的。

任何帮助将不胜感激。 谢谢!

编辑:

我已将PayPalController.php的顶部更改为:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Netshell\PayPal;
use Redirect;
use Illuminate\Http\Request;

但它仍然无法奏效。 Class 'Netshell\PayPal' not found

4 个答案:

答案 0 :(得分:6)

可能是因为您在班级名称中输入了错字。 use Paypal;而不是use PayPal;

答案 1 :(得分:1)

我有同样的问题,但这里是解决方案: 添加提供者列表:

Anouar\Paypalpayment\PaypalpaymentServiceProvider::class,

和别名:

'Paypal' => Anouar\Paypalpayment\Facades\PaypalPayment::class,

并且会有效。

答案 2 :(得分:0)

在此处查看文件PayPal.phphttps://github.com/net-shell/laravel-paypal/blob/master/src/Netshell/Paypal/Paypal.php(您可以在项目中添加作曲家之后在项目中找到此内容:vendor/Netshell/Paypal最有可能)

第一行显示名称空间为:namespace Netshell\Paypal;

使用PayPalController.php代替use Netshell/Paypal更新您的use Paypal,它应该有效。

答案 3 :(得分:0)

检查别名并确保其PayPal而不是Paypal(&#39; PayPal&#39; =&gt; Netshell \ Paypal \ Facades \ Paypal :: class,)