簡體   English   中英

Omnipay,paypal REST與laravel 5

[英]Omnipay, paypal REST with laravel 5

我不斷得到dd($finalResponse);的回應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());
    }

以付款人身份登錄並完成購買后,付款人還需要批准什么以及如何批准?

不,您沒有正確理解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帳戶電子郵件地址和密碼來授權交易,檢查發票,單擊表示同意支付的按鈕。

接下來發生的事情是客戶被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());
}

請注意,您需要從授權調用中存儲付款人ID和transactionReference,並且能夠在returnUrl代碼中恢復這些內容。

您還需要能夠處理cancelUrl案例,其中客戶已決定不同意PayPal上的付款並將其發送回您網站上的cancelUrl URL。

最后,您需要能夠處理客戶在PayPal網站上完成付款但不會以returnUrl結束的情況。 這可能是因為網絡問題,瀏覽器崩潰,或者是因為客戶在點擊PayPal上的“同意付款”並重新登陸您的網站之間關閉了瀏覽器。 處理這些問題的最佳方法是使用omnipay-paypal調用fetchPurchase()或listPurchase()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM