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

时间:2017-02-15 10:10:18

标签: laravel-5 laravel-5.3 omnipay paypal

我是Laravel的新手。我一直在努力在我的网站上实施Paypal Express Checkout几天,以便向非营利组织捐款。感谢these explanations我已经能够安装Omnipay,让用户输入他想捐赠的金额并转到Paypal。 但是,当我尝试结束交易(Pay)时,我没有被重定向到我的成功消息。我的沙箱帐户也没有显示任何交易,因此似乎付款未正确完成。我猜我的“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');

1 个答案:

答案 0 :(得分:0)

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

根据已修改的问题和评论进行修改以获取更多详细信息:

如果成功完成PayPal登录和结帐屏幕,客户将返回completePurchase,如果出于任何原因退出流程,则会转到returnUrl

cancelUrl方法中,paypal会在查询字符串中发回PaymentController@getSuccessPaymenttoken(www.example.com/payment_success?token=EC-12345&PayerID=ABC123, omn​​ipay-paypal将在payerID电话中自动接听,您可以通过PayPal确认客户已正确完成结账并且交易成功。

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