class ExpressCheckout {
static function PPHttpPost($methodName_, $nvpStr_) {
require_once("DataClass.php");
$API_Data = Data::GetPayPalData();
// Set up your API credentials, PayPal end point, and API version.
$environment = $API_Data['enviro'];
$API_UserName = urlencode($API_Data['user']);
$API_Password = urlencode($API_Data['pass']);
$API_Signature = urlencode($API_Data['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('72.0');
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Turn 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);
// Set the API operation, version, and API signature in the request.
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
// Set the request as a POST FIELD for curl.
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
// Get response from the server.
$httpResponse = curl_exec($ch);
if(!$httpResponse) {
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
}
// Extract the 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;
}
static function send() {
require_once("DataClass.php");
$API_Data = Data::GetPayPalData();
// Set request-specific fields.
$paymentAmount = urlencode($API_Data['pay']);
$currencyID = urlencode($API_Data['currency']); // or other currency code ('USD', 'EUR', 'JPY', 'CAD', 'AUD')
$paymentType = urlencode($API_Data['type']); // or 'Sale' or 'Order'
$returnURL = urlencode($API_Data['return']);
$cancelURL = urlencode($API_Data['cancel']);
$bad = urlencode("MESS subscription");
// Add request-specific fields to the request string.
$nvpStr = "&CANCELURL=$cancelURL&REQCONFIRMSHIPPING=0&NOSHIPPING=1&PAYMENTREQUEST_0_AMT=$paymentAmount&PAYMENTREQUEST_0_ITEMAMT=$paymentAmount&PAYMENTREQUEST_0_TAXAMT=0&RETURNURL=$returnURL&L_BILLINGTYPE0=RecurringPayments&PAYMENTREQUEST_0_DESC=$bad&L_BILLINGAGREEMENTDESCRIPTION0=$bad&PAYMENTREQUEST_0_PAYMENTACTION=$paymentType&PAYMENTREQUEST_0_CURRENCYCODE=$currencyID";
// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = ExpressCheckout::PPHttpPost('SetExpressCheckout', $nvpStr);
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"); //commented out to view token and parsed response
return array($token, $httpParsedResponseAr);
} else {
return 'SetExpressCheckout failed: ' . print_r($httpParsedResponseAr, true);
}
}
}
我上面的代码可以正常检索令牌(响应为 ACK = 'SUCCESS'),但是当我使用令牌重定向到贝宝时,我收到以下错误:
此交易无效。请返回收款人的网站以使用他们的常规结账流程完成您的交易。
我已经尝试了几种方法,在网上搜索,现在3天仍然没有运气。谁能看到我哪里出错了?提前致谢。
哦,这被称为:var_dump($token = ExpressCheckout::send($_SESSION['email']));
var dump()
用于在header("Location: $payPalURL");
注释掉时查看令牌和响应。