Omnipay,paypal REST与laravel 5

Har*_*osh 4 php paypal laravel omnipay laravel-5

我一直得到的回应dd($finalResponse);是:

RestResponse {#298 ?
  #statusCode: 400
  #request: RestCompletePurchaseRequest {#300 ?}
  #data: array:4 [?
    "name" => "PAYMENT_NOT_APPROVED_FOR_EXECUTION"
    "message" => "Payer has not approved payment"
    "information_link" => "https://developer.paypal.com/webapps/developer/docs/api/#PAYMENT_NOT_APPROVED_FOR_EXECUTION"
    "debug_id" => "5471589613718"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是代码.

$gateway = Omnipay::create('PayPal_Rest');

    // Initialise the gateway
    $gateway->initialize(array(
       'clientId' => env('PAYMENT_SANDBOX_PAYPAL_CLIENTID'),
       'secret'   => env('PAYMENT_SANDBOX_PAYPAL_SECRET'),
       'testMode' => true, // Or false when you are ready for live transactions
    ));

    // Do an authorisation transaction on the gateway
    $transaction = $gateway->authorize(array(
        'returnUrl'=> env('PAYMENT_SANDBOX_PAYPAL_URL'),
        'cancelUrl' => 'http://localhost:8000/cancel',
       'amount'        => '10.00',
       'currency'      => 'AUD',
       'description'   => 'This is a test authorize transaction.',
       // 'card'          => $card,
    ));

    $response = $transaction->send();
    if ($response->isSuccessful()) {
       // Find the authorization ID
       $authResponse = $response->getTransactionReference();
       echo "Authorize transaction was successful!\n".$authResponse;
    }else{
        echo "Failed to auth transaction";
        dd($response);
    }

    // Once the transaction has been approved, we need to complete it.
    $transaction = $gateway->completePurchase(array(
        'payerId'             => $request->PayerID,
        'transactionReference' => $authResponse            
    ));

    $finalResponse = $transaction->send();

    dd($finalResponse);

    if ($finalResponse->getData()) {
       echo "Transaction was successful!\n";
       // Find the authorization ID
       $results = $finalResponse->getTransactionReference();
        dd($results);
    }else{
        dd($finalResponse->getData());
    }
Run Code Online (Sandbox Code Playgroud)

以付款人身份登录并完成购买后,付款人还需要批准什么以及如何批准?

del*_*bel 12

不,您没有正确理解PayPal付款流程.这是正确的流程:

  1. 你可以调用Omnipay :: create(),$ gateway-> initialize()和$ gateway-> authorize()就像你上面的那样.但是对于returnUrl,您必须在您的网站上提供一个URL,就像您对cancelUrl一样.也许你的意思是使用http:// localhost:8000/return(尽管更好的方法是在返回URL中有一个事务ID).

  2. $ gateway-> authorize()的响应类型为RedirectResponse.你可以检查一下:

//在网关上执行授权事务

$transaction = $gateway->authorize(array(
    'returnUrl'=> env('PAYMENT_SANDBOX_PAYPAL_URL'),
    'cancelUrl' => 'http://localhost:8000/cancel',
    'amount'        => '10.00',
    'currency'      => 'AUD',
    'description'   => 'This is a test authorize transaction.',
    // 'card'          => $card,
));

$response = $transaction->send();
if ($response->isRedirect()) {
    // Yes it's a redirect.  Redirect the customer to this URL:
    $redirectUrl = $response->getRedirectUrl();
}
Run Code Online (Sandbox Code Playgroud)

此时,与客户的初始握手已经结束.您现在已将客户重定向到PayPal网站,他们将通过登录PayPal帐户电子邮件地址和密码来授权交易,检查发票,单击表示同意支付的按钮.

接下来发生的事情是客户被PayPal重定向回您在authorize()调用中提供的redirectUrl上的网站.这将跳转到您的代码中的不同位置.此时,您调用completeAuthorize,就像您之前在代码中所做的那样:

// Once the transaction has been approved, we need to complete it.
$transaction = $gateway->completePurchase(array(
    'payerId'             => $request->PayerID,
    'transactionReference' => $authResponse            
));

$finalResponse = $transaction->send();

dd($finalResponse);

if ($finalResponse->getData()) {
   echo "Transaction was successful!\n";
   // Find the authorization ID
   $results = $finalResponse->getTransactionReference();
    dd($results);
}else{
    dd($finalResponse->getData());
}
Run Code Online (Sandbox Code Playgroud)

请注意,您需要从授权调用中存储付款人ID和transactionReference,并且能够在returnUrl代码中恢复这些内容.

您还需要能够处理cancelUrl案例,其中客户已决定不同意PayPal上的付款并将其发送回您网站上的cancelUrl URL.

最后,您需要能够处理客户在PayPal网站上完成付款但不会以returnUrl结束的情况.这可能是因为网络问题,浏览器崩溃,或者是因为客户在点击PayPal上的"同意付款"并重新登陆您的网站之间关闭了浏览器.处理这些问题的最佳方法是使用omnipay-paypal调用fetchPurchase()或listPurchase().