简体   繁体   English

Omnipay,paypal REST与laravel 5

[英]Omnipay, paypal REST with laravel 5

The response i keep getting at dd($finalResponse); 我不断得到dd($finalResponse);的回应dd($finalResponse); is: 是:

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"
  ]
}

Here is the code. 这是代码。

$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());
    }

After logging in as the payer and completing purchase, what else does the payer need to approve and how? 以付款人身份登录并完成购买后,付款人还需要批准什么以及如何批准?

No, you aren't understanding the PayPal payment flow correctly. 不,您没有正确理解PayPal付款流程。 Here is the correct flow: 这是正确的流程:

  1. You do the call to Omnipay::create(), $gateway->initialize() and $gateway->authorize() just like you have them above. 你可以调用Omnipay :: create(),$ gateway-> initialize()和$ gateway-> authorize()就像你上面的那样。 However for returnUrl you have to provide a URL on your site, just like you have for cancelUrl. 但是对于returnUrl,您必须在您的网站上提供一个URL,就像您对cancelUrl一样。 Perhaps you mean to use http://localhost:8000/return (although better would be to have a transaction ID or something in the return URL). 也许你的意思是使用http:// localhost:8000 / return (尽管更好的方法是在返回URL中有一个事务ID)。

  2. The response from the $gateway->authorize() will be of type RedirectResponse. $ gateway-> authorize()的响应类型为RedirectResponse。 You can check this: 你可以检查一下:

// 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->isRedirect()) {
    // Yes it's a redirect.  Redirect the customer to this URL:
    $redirectUrl = $response->getRedirectUrl();
}

At that point the initial handshake with the customer is over. 此时,与客户的初始握手已经结束。 You have now redirected the customer to the PayPal web site where they will authorize the transaction by logging in with their PayPal account email address and password, check the invoice, click the button that says that they agree to pay. 您现在已将客户重定向到PayPal网站,他们将通过登录PayPal帐户电子邮件地址和密码来授权交易,检查发票,单击表示同意支付的按钮。

The next thing that happens is that the customer is redirected by PayPal back to your web site, on the redirectUrl that you provided in the authorize() call. 接下来发生的事情是客户被PayPal重定向回您在authorize()调用中提供的redirectUrl上的网站。 That will jump to a different place in your code. 这将跳转到您的代码中的不同位置。 At that point you call completeAuthorize, just like you had in your code earlier: 此时,您调用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());
}

Note that you need to have stored the Payer ID and transactionReference from the authorize call and be able to recover those in your returnUrl code. 请注意,您需要从授权调用中存储付款人ID和transactionReference,并且能够在returnUrl代码中恢复这些内容。

You also need to be able to handle the cancelUrl case, where the customer has decided not to agree to the payment on PayPal and gets sent back to the cancelUrl URL on your website instead. 您还需要能够处理cancelUrl案例,其中客户已决定不同意PayPal上的付款并将其发送回您网站上的cancelUrl URL。

Finally you need to be able to handle the occasional cases where the customer completes the payment on the PayPal web site but does not end back on your returnUrl. 最后,您需要能够处理客户在PayPal网站上完成付款但不会以returnUrl结束的情况。 This could be because of a network issue, the browser crashed, or because the customer closed their browser between clicking "Agree to Pay" on PayPal and landing back on your site. 这可能是因为网络问题,浏览器崩溃,或者是因为客户在点击PayPal上的“同意付款”并重新登陆您的网站之间关闭了浏览器。 The best way to handle those is with the omnipay-paypal calls fetchPurchase() or listPurchase(). 处理这些问题的最佳方法是使用omnipay-paypal调用fetchPurchase()或listPurchase()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM