【问题标题】:Omnipay, paypal REST with laravel 5Omnipay,带有 laravel 5 的贝宝 REST
【发布时间】:2016-01-26 07:02:54
【问题描述】:

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

这是代码。

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

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

【问题讨论】:

    标签: php laravel paypal laravel-5 omnipay


    【解决方案1】:

    不,您没有正确理解 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();
    }
    

    此时与客户的初次握手结束。您现在已将客户重定向到 PayPal 网站,他们将在该网站上使用他们的 PayPal 帐户电子邮件地址和密码登录来授权交易,检查发票,点击显示他们同意付款的按钮。

    接下来发生的事情是,客户通过您在 authorize() 调用中提供的 redirectUrl 被 PayPal 重定向回您的网站。这将跳转到代码中的不同位置。此时您调用 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());
    }
    

    请注意,您需要存储来自授权调用的付款人 ID 和 transactionReference,并能够在您的 returnUrl 代码中恢复它们。

    您还需要能够处理 cancelUrl 案例,在这种情况下,客户决定不同意在 PayPal 上付款并被发送回您网站上的 cancelUrl URL。

    最后,您需要能够处理客户在 PayPal 网站上完成付款但没有在您的 returnUrl 上结束的偶尔情况。这可能是因为网络问题、浏览器崩溃,或者是因为客户在点击 PayPal 上的“同意付款”并返回您的网站之间关闭了浏览器。处理这些问题的最佳方法是使用 omnipay-paypal 调用 fetchPurchase() 或 listPurchase()。

    【讨论】:

    • 感谢您的详尽解释,至少现在有信息可供他人使用。然而,这不是我希望我的敌人发生的事情,更不用说我的客户了。我以前使用的一个解决方案跳过了批准过程。完成后我会在这里发布给其他可能感兴趣的人。
    • Harry,我的任务清单是更新omnipay-paypal 模块的文档,以便更清楚地了解重定向付款必须发生的情况。尽管重定向付款通常由 Omnipay 处理,但 PayPal 似乎是人们最常使用的需要重定向的网关。其他如 VTPayments(用于银联和支付宝中国支付)也需要与上述类似的重定向,但使用频率较低。无论如何,我会向 Kayla 提交一些 PR,他们将通过正常的审核和批准流程。
    • 这就是我切换到 PayPal_Express 的原因,在我看来,PayPal_Rest 流程存在原生 UX 问题,由 PayPal 引起
    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 2014-01-12
    • 2018-05-26
    • 2015-10-27
    • 2013-04-03
    • 2015-03-04
    • 2016-06-20
    • 2018-04-25
    相关资源
    最近更新 更多