gpt4 book ai didi

php - PayPal API CreateRecurringPaymentsProfile 拒绝工作

转载 作者:可可西里 更新时间:2023-10-31 22:47:20 24 4
gpt4 key购买 nike

在过去的 5 个小时里,我一直在搜索关于这个东西的信息,并且发现了很多不起作用的信息。我已经成功地能够使用 setExpressCheckout API,获取详细信息并收取正常订单费用,但是当我想要创建定期付款配置文件时,我总是会收到无效 token 错误。我知道这不是无效 token ,但我不知道该怎么做。

我目前正在使用 PayPal 的 setExpressCheckout 和 createRecurringPaymentsProfile 示例代码,但没有成功。

这是我从 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

最佳答案

你想通了吗?您收到无效 token 错误的原因是您没有在 SetExpressCheckout 请求中包含计费协议(protocol)(定期付款)信息。

这是一个 sample set当这个问题出现时,我总是用来向人们展示。请注意 SEC 中包含的 BILLINGTYPE 和 BILLINGAGREEMENTDESCRIPTION。

关于php - PayPal API CreateRecurringPaymentsProfile 拒绝工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7846728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com