Laravel 5.3 - Omnipay Paypal Express 未返回成功消息

Laravel 5.3 - Omnipay Paypal Express not returning success message

我是 Laravel 的新手。几天来,我一直在努力在我的网站上实施 Paypal Express Checkout,以便能够向非营利组织捐款。感谢 these explanations 我已经能够安装 Omnipay,让用户输入他想要捐赠的金额并转到 Paypal。 但是,当我尝试结束交易(支付)时,我没有被重定向到我的成功消息。我的沙盒账户也没有显示任何交易,所以似乎付款没有正确完成。我猜我的 "getSuccessPayment" 函数有问题,但我不知道是什么...

到目前为止,这是我的控制器:

<?php namespace App\Http\Controllers;
use Omnipay\Omnipay;
use Session;
use App\Http\Requests\PaymentRequest;

class PaymentController extends Controller {

    public function postPayment(PaymentRequest $request)
    {
        $price = $request->get('price');

        $items[] = array('name' => 'Don', 'quantity' => 1, 'price' => $price);

        $params = array(
            'cancelUrl'=>url('/donner'),
            'returnUrl'=>url('/payment_success'),
            'amount' =>  $price,
            'currency' => 'EUR'
        );

        Session::put('params', $params);
        Session::save();

        $gateway = Omnipay::create('PayPal_Express');
        $gateway->setUsername('my sandbox email'); 
        $gateway->setPassword('my sandbox password');  
        $gateway->setSignature('my sandbox signature');
        $gateway->setTestMode(true);

       $response = $gateway->purchase($params)->setItems($items)->send();

        if ($response->isSuccessful()) {
            print_r($response);
        } elseif ($response->isRedirect()) {
            $response->redirect();
        } else {
            echo $response->getMessage();
        }
    }

     public function getSuccessPayment()
    {

        $gateway = Omnipay::create('PayPal_Express');
        $gateway->setUsername('my sandbox email'); 
        $gateway->setPassword('my sandbox password');  
        $gateway->setSignature('my sandbox signature');
        $gateway->setTestMode(true);

        $params = Session::get('params');
        $response = $gateway->completePurchase($params)->send();
        $paypalResponse = $response->getData(); 

        if(isset($paypalResponse['PAYMENTINFO_0_ACK']) && $paypalResponse['PAYMENTINFO_0_ACK'] === 'Success') {
            return redirect('/payment_success');

        } else {
            //payment fails
        return redirect('/payment_failure');

        }
    }
}
?>

还有我的路线:

Route::post('donner',
    ['as' => 'payment', 'uses' => 'PaymentController@postPayment']);

Route::get('payment_success', 'PaymentController@getSuccessPayment');

Route::get('payment_failure', 'PaymentController@getSuccessPayment');

创建网关参数时,您将 /donner 作为 returnUrl 传递,这是您的用户在完成 PayPal 快速登录和付款确认后返回的地方,因此 Laravel会看到您没有的 Route::get('donner'... 路由,将其更改为 'returnUrl'=>url('/payment_success'), 将使您的用户回到您的成功路由并允许您提交 completePurchase 调用。

根据已编辑的问题和评论编辑更多详细信息:

如果客户成功完成 PayPal 登录和结帐屏幕,他们将返回到您的 returnUrl,如果他们出于某种原因退出该过程,他们将转到 cancelUrl

在您的 PaymentController@getSuccessPayment 方法中,paypal 将在查询字符串中发回 tokenpayerID (www.example.com/payment_success?token=EC- 12345&PayerID=ABC123,omnipay-paypal 将在 completePurchase 调用中自动接收此信息,您可以通过 PayPal 确认客户正确完成结帐并且交易成功。

为了避免混淆,我会将您当前的 Route::get('payment_success', 'PaymentController@getSuccessPayment'); 路由重命名为 Route::get('complete_payment', 'PaymentController@getCompletePayment'); 之类的名称,并创建一个新的 payment_success 路由,在您确认状态后将用户发送到该路由使用 PayPal 付款。