2013-02-08 100 views
0

我對paypal api非常陌生,我遵循this教程,並根據我的需要調整了代碼。交易標識無效10609

我想提出一個基本的授權&捕獲的3個步驟,但我一直在捕捉

  1. 顧客土地paypal_pay_redirect.php用於登錄到貝寶失敗,並作出paymet
  2. 的付款罰款和我們土地paypal_success.php,在這裏我請求授權,並保留TRANSACTIONID爲下一步
  3. paypal_capture.php我使用transactionid捕獲支付,但我總是得到「10609:交易編號無效」

這裏的代碼,哪裏出錯?

感謝

paypal_lib.php

<?php 
    // code from http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/ 
class Paypal { 
    /** 
    * Last error message(s) 
    * @var array 
    */ 
    protected $_errors = array(); 

    /** 
    * API Credentials 
    * Use the correct credentials for the environment in use (Live/Sandbox) 
    * @var array 
    */ 
    protected $_credentials = array(
        'USER' => 'xxxxxxxxxxxxxxxxxxxx', 
        'PWD' => 'xxxxxxxxxxxxx', 
        'SIGNATURE' => 'xxxxxxxxxxxxxxxxxxxxxxxxx' 
       ); 
    /** 
    * API endpoint 
    * Live - https://api-3t.paypal.com/nvp 
    * Sandbox - https://api-3t.sandbox.paypal.com/nvp 
    * @var string 
    */ 
    protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp'; 

    /** 
    * API Version 
    * @var string 
    */ 
    protected $_version = '74.0'; 

    /** 
    * Make API request 
    * 
    * @param string $method string API method to request 
    * @param array $params Additional request parameters 
    * @return array/boolean Response array/boolean false on failure 
    */ 
    public function request($method,$params = array()) { 
    $this -> _errors = array(); 
    if(empty($method)) { //Check if API method is not empty 
     $this -> _errors = array('API method is missing'); 
     return false; 
    } 

    //Our request parameters 
    $requestParams = array_merge(
       array(
         'METHOD' => $method, 
         'VERSION' => $this -> _version 
         ), 
       $this -> _credentials 
       ); 

    //Building our NVP string 
    $request = http_build_query(array_merge($requestParams, $params)); 

    //cURL settings 
    $curlOptions = array (
       CURLOPT_URL => $this -> _endPoint, 
       CURLOPT_VERBOSE => 1, 
       CURLOPT_SSL_VERIFYPEER => true, 
       CURLOPT_SSL_VERIFYHOST => 2, 
       CURLOPT_RETURNTRANSFER => 1, 
       CURLOPT_POST => 1, 
       CURLOPT_POSTFIELDS => $request 
      ); 

    $ch = curl_init(); 
    curl_setopt_array($ch,$curlOptions); 

    //Sending our request - $response will hold the API response 
    $response = curl_exec($ch); 

    //Checking for cURL errors 
    if (curl_errno($ch)) { 
     $this -> _errors = curl_error($ch); 
     curl_close($ch); 
     return false; 
     //Handle errors 
    } else { 
     curl_close($ch); 
     $responseArray = array(); 
     parse_str($response,$responseArray); // Break the NVP string to an array 
     return $responseArray; 
    } 
    } 
} 

?> 

paypal_pay_redirect.php

<?php 
require("paypal_lib.php"); 

//Our request parameters 
$requestParams = array(
       'RETURNURL' => 'https://www.domain.com/paypal_success.php', 
       'CANCELURL' => 'https://www.domain.com/paypal_fail.php' 
       ); 

$orderParams = array(
      'PAYMENTREQUEST_0_AMT' => "57.00", 
      'PAYMENTREQUEST_0_SHIPPINGAMT' => '0', 
      'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR', 
    'PAYMENTREQUEST_0_ITEMAMT' => "57.00" 
      ); 

$item = array(
      'L_PAYMENTREQUEST_0_NAME0' => 'xxxxxxxxxx', 
      'L_PAYMENTREQUEST_0_DESC0' => 'xxxxxxxxxx', 
      'L_PAYMENTREQUEST_0_AMT0' => "57.00", 
    'L_PAYMENTREQUEST_0_QTY0' => '1' 
     ); 

$paypal = new Paypal(); 
$response = $paypal -> request('SetExpressCheckout',$requestParams + $orderParams + $item); 
if(is_array($response) && $response['ACK'] == 'Success') { //Request successful 
    $token = $response['TOKEN']; 
    header('Location: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . urlencode($token)); 
} 


?> 

paypal_success.php

<?php 
require("paypal_lib.php"); 

if(isset($_GET['token']) && !empty($_GET['token'])) { // Token parameter exists 
    // Get checkout details, including buyer information. 
    // We can save it for future reference or cross-check with the data we have 
    $paypal = new Paypal(); 
    $checkoutDetails = $paypal -> request('GetExpressCheckoutDetails', array('TOKEN' => $_GET['token'])); 

    // Complete the checkout transaction 
    $requestParams = array(
      'TOKEN' => $_GET['token'], 
      'PAYMENTACTION' => 'Authorization', 
      'PAYERID' => $_GET['PayerID'], 
      'PAYMENTREQUEST_0_AMT' => '57', // Same amount as in the original request 
      'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR' // Same currency as the original request 
      ); 

    $response = $paypal -> request('DoExpressCheckoutPayment',$requestParams); 
    if(is_array($response) && $response['ACK'] == 'Success') { // Payment successful 
    // We'll fetch the transaction ID for internal bookkeeping 
    $transactionId = $response['PAYMENTINFO_0_TRANSACTIONID']; 
    echo "OK id: ".$transactionId; 
    var_dump($response); 
    } 

var_dump($checkoutDetails); 

} 
?> 

paypal_capture.php

<?php 
require("paypal_lib.php"); 

$paypal = new Paypal(); 

// Complete the checkout transaction 
$requestParams = array(
       'AMT' => '57.00', 
       'AUTHORIZATIONID' => 'xxxxxxxxxxxxxxxxxxx', //what I get in paypal_success.php 
       'CURRENCYCODE' => 'EUR', 
       'COMPLETETYPE' => 'Complete', // Same amount as in the original request 
       ); 

$response = $paypal -> request('DoCapture',$requestParams); 

var_dump($response); 

?> 

回答

1

兩件事情:

首先,在paypal_pay_redirect.php,加PAYMENTREQUEST_0_PAYMENTACTION => 'Authorization'$orderParams

二,在paypal_success.php中,將PAYMENTACTION更改爲PAYMENTREQUEST_0_PAYMENTACTION

這裏的原因:

在您的SetExpressCheckout電話,如果不設置PAYMENTREQUEST_0_PAYMENTACTION,貝寶假定這將是Sale。如果在您的SetExpressCheckout呼叫中設置爲Sale,則PayPal將只允許您在您的DoExpressCheckoutPayment呼叫中將其設置爲Sale

此外,在您的DoExpressCheckoutPayment調用中,您正在混合新舊風格的變量名稱。 (當PayPal爲Express Checkout引入並行付款時,一些變量(如PAYMENTACTION)被重新命名,PAYMENTACTION是舊風格; PAYMENTREQUEST_0_PAYMENTACTION是新風格。)當您混合使用舊風格和新風格的名稱時,PayPal會識別變量一種風格,而忽略另一種風格。在你的情況下,PAYMENTREQUEST_0_AMTPAYMENTREQUEST_0_CURRENCYCODE正在被識別,但PAYMENTACTION被忽略。

這兩個因素的組合意味着您的交易很可能作爲銷售交易運行,而不是授權。由於您無法捕獲Sale交易(因爲它在技術上已被捕獲),PayPal會迴應說交易ID是無效的。