L5 Paypal集成 - 查找控制器方法

时间:2015-06-13 06:56:08

标签: laravel laravel-5

我正在尝试使用此软件包将Paypal集成到我的Laravel 5网站中: http://packalyst.com/packages/package/netshell/paypal

当我去/paypal/checkout时,我收到了这个错误:

  

UrlGenerator.php第561行中的InvalidArgumentException:Action   App \ Http \ Controllers \ PayPalController @ getDone未定义。

这是我的路线:

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

这是PayPalController:

<?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');
    }

}

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:2)

虽然laravel允许您生成动作的URL。我研究了代码中发生的事情。

在UrlGenerator类中:

/**
 * Get the URL to a controller action.
 *
 * @param  string  $action
 * @param  mixed   $parameters
 * @param  bool    $absolute
 * @return string
 *
 * @throws \InvalidArgumentException
 */
public function action($action, $parameters = [], $absolute = true)
{
    if ($this->rootNamespace && !(strpos($action, '\\') === 0)) {
        $action = $this->rootNamespace.'\\'.$action;
    } else {
        $action = trim($action, '\\');
    }

    if (!is_null($route = $this->routes->getByAction($action))) {
        return $this->toRoute($route, $parameters, $absolute);
    }

    throw new InvalidArgumentException("Action {$action} not defined.");
}

它将使用指定的操作搜索已定义的路由,并实际上返回基于该路由的链接。所以你必须定义到PayPayController @ getDone的路由才能工作。