2011-10-21 188 views
4

我一直在尋找關於這件事的信息在過去5個小時,並發現很多不起作用。我已經成功地能夠使用setExpressCheckout API,獲取詳細信息,並收取一定的正常秩序,但是當我想建立一個定期付款資料,我總是得到一個無效的令牌錯誤。我知道這不是一個無效的標記,但我不知道該怎麼做。PayPal API CreateRecurringPaymentsProfile拒絕工作

我目前使用的setExpressCheckout和createRecurringPaymentsProfile PayPal的示例代碼,但沒有成功。

下面是來自PayPal我可笑的簡化代碼。

<? 

$environment = 'sandbox'; // or 'beta-sandbox' or 'live' 
$ROOT_URL = 'http://example.com/paypal/'; 

/** 
* Send HTTP POST Request 
* 
* @param string The API method name 
* @param string The POST Message fields in &name=value pair format 
* @return array Parsed HTTP Response body 
*/ 
function PPHttpPost($methodName_, $nvpStr_) { 

    global $environment; 

    $API_UserName = urlencode('email'); 
    $API_Password = urlencode('pass'); 
    $API_Signature = urlencode('sig'); 
    $API_Endpoint = "https://api-3t.paypal.com/nvp"; 
    if("sandbox" === $environment || "beta-sandbox" === $environment) { 
     $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp"; 
    } 
    $version = urlencode('51.0'); 

    // setting the curl parameters. 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $API_Endpoint); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 

    // turning off the server and peer verification(TrustManager Concept). 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1); 

    // NVPRequest for submitting to server 
    $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_"; 

    // setting the nvpreq as POST FIELD to curl 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); 

    // getting response from server 
    $httpResponse = curl_exec($ch); 

    if(!$httpResponse) { 
     exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')'); 
    } 

    // Extract the RefundTransaction response details 
    $httpResponseAr = explode("&", $httpResponse); 

    $httpParsedResponseAr = array(); 
    foreach ($httpResponseAr as $i => $value) { 
     $tmpAr = explode("=", $value); 
     if(sizeof($tmpAr) > 1) { 
      $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1]; 
     } 
    } 

    if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) { 
     exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint."); 
    } 

    return $httpParsedResponseAr; 
} 


$paymentAmount = urlencode(34.00); 
if(!isset($_REQUEST['token'])){ 
    // Set request-specific fields. 

    $currencyID = urlencode('USD');       // or other currency code ('GBP', 'EUR', 'JPY', 'CAD', 'AUD') 
    $paymentType = urlencode('Authorization');    // or 'Sale' or 'Order' 

    $returnURL = urlencode($ROOT_URL.'/buy.php?return=1'); 
    $cancelURL = urlencode($ROOT_URL.'/buy.php?cancel=1'); 

    // Add request-specific fields to the request string. 
    $nvpStr = "&Amt=$paymentAmount&ReturnUrl=$returnURL&CANCELURL=$cancelURL&PAYMENTACTION=$paymentType&CURRENCYCODE=$currencyID"; 

    // Execute the API operation; see the PPHttpPost function above. 
    $httpParsedResponseAr = PPHttpPost('SetExpressCheckout', $nvpStr); 

    print_r($httpParsedResponseAr); 

    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) { 
     // Redirect to paypal.com. 
     $token = urldecode($httpParsedResponseAr["TOKEN"]); 
     $payPalURL = "https://www.paypal.com/webscr&cmd=_express-checkout&token=$token"; 
     if("sandbox" === $environment || "beta-sandbox" === $environment) { 
      $payPalURL = "https://www.$environment.paypal.com/webscr&cmd=_express-checkout&token=$token"; 
     } 

     //header("Location: $payPalURL"); 
     echo '<a href="'.$payPalURL.'">PayPal</a>'; 
     exit; 
    } else { 
     exit('SetExpressCheckout failed: ' . print_r($httpParsedResponseAr, true)); 
    } 
}else{ 
    $token = urlencode($_REQUEST['token']); 
    //Now create recurring profile 
    ?> 
    <h1>Yes!</h1> 
    <? 


$currencyID = urlencode("USD");      // or other currency code ('GBP', 'EUR', 'JPY', 'CAD', 'AUD') 
$startDate = urlencode("2012-9-6T0:0:0"); 
$billingPeriod = urlencode("Month");    // or "Day", "Week", "SemiMonth", "Year" 
$billingFreq = urlencode("4");      // combination of this and billingPeriod must be at most a year 

$nvpStr="&TOKEN=$token&AMT=$paymentAmount&CURRENCYCODE=$currencyID&PROFILESTARTDATE=$startDate"; 
$nvpStr .= "&BILLINGPERIOD=$billingPeriod&BILLINGFREQUENCY=$billingFreq"; 

$httpParsedResponseAr = PPHttpPost('CreateRecurringPaymentsProfile', $nvpStr); 

if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) { 
    exit('CreateRecurringPaymentsProfile Completed Successfully: '.print_r($httpParsedResponseAr, true)); 
} else { 
    exit('CreateRecurringPaymentsProfile failed: ' . print_r($httpParsedResponseAr, true)); 
} 
} 
?> 

很明顯,這裏的email,pass和sig是錯誤的。我的腳本的名稱是example.com/paypal/buy.php如果不清楚...

更新:我終於找到了一些可行的方法。我還沒有漂亮的代碼,但它至少通過了。 https://www.x.com/developers/paypal/forums/nvp/createrecurringpaymentsprofile-invalid-token-0

回答

6

你得到這一切都想通了嗎?如果您沒有在您的SetExpressCheckout請求中包含結算協議(定期付款)信息,那麼您得到無效令牌錯誤的原因是。

這是我在出現這個問題時總是用來向人們展示的sample set。請注意SEC中包含的BILLINGTYPE和BILLINGAGREEMENTDESCRIPTION。

+0

我想補充一件事:在GetExpressCheckoutDetails之前,您應該將用戶重定向到https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=或https://www.paypal.com/cgi- bin/webscr?cmd = _express-checkout&token =讓他接受agrrement。 – Oleg