2015-10-27 68 views
4

我一直在dd($finalResponse);得到的迴應是:Omnipay,貝寶休息與laravel 5

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

作爲付款人登錄並完成購買後,付款人還需要批准什麼以及如何操作?

回答

12

不,您不能正確理解PayPal付款流程。這是正確的流程:

  1. 你做調用Omnipay:創建(),$網關 - >初始化()和$網關 - >授權()就像你有他們的上方。但是對於returnUrl,您必須在您的網站上提供一個網址,就像您使用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()); 
} 

請注意,您需要先存儲在授權呼叫計費用戶標識和transactionReference並能夠恢復那些在您RETURNURL代碼。

您還需要能夠處理cancelUrl的情況,即客戶已決定不同意PayPal上的付款並將其返回到您網站上的cancelUrl網址。

最後,您需要能夠處理客戶在PayPal網站上完成付款但不會退回到您的returnUrl上的偶然情況。這可能是因爲網絡問題,瀏覽器崩潰,或者因爲客戶在點擊PayPal上的「同意付款」並重新登錄您的網站之間關閉了瀏覽器。處理這些問題的最好方法是使用omnipay-paypal調用fetchPurchase()或listPurchase()。

+0

感謝您的詳細解釋,至少現在有信息供他人使用。 然而,這不是我希望對我的敵人,更不用說我的客戶。我之前使用的解決方案跳過了審批流程。當它完成時,我會在這裏發佈其他可能感興趣的內容。 –

+0

哈利,這是我要做的更新omnipay-paypal模塊文檔的清單,以提高重定向支付的必要性。雖然重定向支付一般由Omnipay處理,但PayPal似乎是人們最常使用的需要重定向的網關。其他如VTP支付(用於銀聯和支付寶中國付款)也需要重定向,就像上面一樣,但使用頻率較低。無論如何,我會提交一些PR到凱拉,他們將通過正常的審查和批准程序。 – delatbabel

+0

這就是爲什麼我切換到PayPal_Express,在我看來PayPal_Rest流程有一個本地用戶體驗問題,由PayPal引起 –