Paypal API TransactionSearch NVP - 成功但没有结果
我正在编辑 Ultimate Frisbee 组织的网站,我是该组织的一部分,需要在用户尝试登录时验证会员付款。
为此,我使用 PHP Paypal API(NVP 版本,而不是 SOAP)一)向 Paypal 发送请求(称为 TransactionSearch),要求使用特定电子邮件地址从开始日期开始进行交易。这里的问题是 Paypal 向我返回请求成功但没有结果。如果我登录贝宝网站并尝试执行相同的搜索,它会返回我想要的交易。
以下是创建发送到 PayPal 的参数字符串以及响应分析的代码。
$nvpStr; //The parameters string to send to paypal (will contain the start date and the email address)
if (date('m') < 9)
$startDateStr= '08/01/' . (date('y') - 1); //the 1st of april of last year
else
$startDateStr= '08/01/' . date('y'); //the 1st of april this year
if(isset($startDateStr)) {
$start_time = strtotime($startDateStr);
$iso_start = date('Y-m-d\T00:00:00\Z', $start_time);
$nvpStr="&STARTDATE=$iso_start"; //we apply the format paypal requires
}
$nvpStr .= "&EMAIL=" . $_SESSION['Email']; //the user's email address
/* Make the API call to PayPal, using API signature.
The API response is stored in an associative array called $resArray */
$resArray = PPHttpPost("TransactionSearch", $nvpStr);
/* After that we check the values returned by paypal to verify if there is a
transaction related to this email address after the 1st of April*/
这里是 paypal 请求的执行(代码直接取自 paypal 网站)。
session_start();
$environment = 'live'; //"live" or 'beta-sandbox' or 'sandbox'
/**
* 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, $API_Password, $API_Signature;
// Set up your API credentials, PayPal end point, and API version.
$API_UserName = urlencode($API_UserName);
$API_Password = urlencode($API_Password);
$API_Signature = urlencode($API_Signature);
$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');
// 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;
}
然后我为你 var_dump 一些变量:
//$nvpStr we use as the second parameter for the PPHttpPost (I hid the email address)
string(60) "&STARTDATE=2011-08-01T00:00:00Z&[email protected]"
//$nvpreq we use to create the message to send to paypal (I hid the password, user and signature)
string(222) "METHOD=TransactionSearch&VERSION=51.0&PWD=XXX&USER=YYY&SIGNATURE=ZZZ&STARTDATE=2011-08-01T00:00:00Z&[email protected]"
//$resArray the response from paypal
array(5) {
["TIMESTAMP"]=>
string(28) "2011%2d12%2d07T17%3a55%3a13Z"
["CORRELATIONID"]=>
string(13) "8f1c9593e26c0"
["ACK"]=>
string(7) "Success"
["VERSION"]=>
string(6) "51%2e0"
["BUILD"]=>
string(7) "2230381"
}
//$resArray the response from paypal that works when I use the sandbox (I hid the email address)
array(16) {
["L_TIMESTAMP0"]=>
string(28) "2011%2d12%2d07T00%3a26%3a12Z"
["L_TIMEZONE0"]=>
string(3) "GMT"
["L_TYPE0"]=>
string(7) "Payment"
["L_EMAIL0"]=>
string(26) "account%40domain%2ecom"
["L_NAME0"]=>
string(24) "Raphael%20Royer%2dRivard"
["L_TRANSACTIONID0"]=>
string(17) "25V35432PY2041246"
["L_STATUS0"]=>
string(9) "Completed"
["L_AMT0"]=>
string(7) "20%2e00"
["L_CURRENCYCODE0"]=>
string(3) "CAD"
["L_FEEAMT0"]=>
string(9) "%2d0%2e88"
["L_NETAMT0"]=>
string(7) "19%2e12"
["TIMESTAMP"]=>
string(28) "2011%2d12%2d07T18%3a19%3a40Z"
["CORRELATIONID"]=>
string(13) "53733eef8b4e2"
["ACK"]=>
string(7) "Success"
["VERSION"]=>
string(6) "51%2e0"
["BUILD"]=>
string(7) "2230381"
}
在我的测试中,我使用的是 PayPal 沙箱,它运行得很好(我们可以看到我们有一笔交易)...我不知道为什么它不能与真实的环境一起工作一。正如我们所看到的,我的凭据很好,因为它不会给我带来身份验证错误。
I am editing a web site of an Ultimate Frisbee organisation I'm part of that needs to validate a membership payment when the user tries to sign in.
To do so, I am using a PHP Paypal API (the NVP version, not the SOAP one) that sends a request (called TransactionSearch) to Paypal asking for transactions from a start date with a specific email address. The problem here is that Paypal returns me that the request was successful but had no result. If I sign in on the paypal web site and I try to execute the same search, it gives me back the transactions I want.
Here is the code that creates the parameters string to send to paypal and the analysis of the response.
$nvpStr; //The parameters string to send to paypal (will contain the start date and the email address)
if (date('m') < 9)
$startDateStr= '08/01/' . (date('y') - 1); //the 1st of april of last year
else
$startDateStr= '08/01/' . date('y'); //the 1st of april this year
if(isset($startDateStr)) {
$start_time = strtotime($startDateStr);
$iso_start = date('Y-m-d\T00:00:00\Z', $start_time);
$nvpStr="&STARTDATE=$iso_start"; //we apply the format paypal requires
}
$nvpStr .= "&EMAIL=" . $_SESSION['Email']; //the user's email address
/* Make the API call to PayPal, using API signature.
The API response is stored in an associative array called $resArray */
$resArray = PPHttpPost("TransactionSearch", $nvpStr);
/* After that we check the values returned by paypal to verify if there is a
transaction related to this email address after the 1st of April*/
And here is the paypal request execution (code taken directly from paypal web site).
session_start();
$environment = 'live'; //"live" or 'beta-sandbox' or 'sandbox'
/**
* 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, $API_Password, $API_Signature;
// Set up your API credentials, PayPal end point, and API version.
$API_UserName = urlencode($API_UserName);
$API_Password = urlencode($API_Password);
$API_Signature = urlencode($API_Signature);
$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');
// 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;
}
And then I var_dump some of the variables for you :
//$nvpStr we use as the second parameter for the PPHttpPost (I hid the email address)
string(60) "&STARTDATE=2011-08-01T00:00:00Z&[email protected]"
//$nvpreq we use to create the message to send to paypal (I hid the password, user and signature)
string(222) "METHOD=TransactionSearch&VERSION=51.0&PWD=XXX&USER=YYY&SIGNATURE=ZZZ&STARTDATE=2011-08-01T00:00:00Z&[email protected]"
//$resArray the response from paypal
array(5) {
["TIMESTAMP"]=>
string(28) "2011%2d12%2d07T17%3a55%3a13Z"
["CORRELATIONID"]=>
string(13) "8f1c9593e26c0"
["ACK"]=>
string(7) "Success"
["VERSION"]=>
string(6) "51%2e0"
["BUILD"]=>
string(7) "2230381"
}
//$resArray the response from paypal that works when I use the sandbox (I hid the email address)
array(16) {
["L_TIMESTAMP0"]=>
string(28) "2011%2d12%2d07T00%3a26%3a12Z"
["L_TIMEZONE0"]=>
string(3) "GMT"
["L_TYPE0"]=>
string(7) "Payment"
["L_EMAIL0"]=>
string(26) "account%40domain%2ecom"
["L_NAME0"]=>
string(24) "Raphael%20Royer%2dRivard"
["L_TRANSACTIONID0"]=>
string(17) "25V35432PY2041246"
["L_STATUS0"]=>
string(9) "Completed"
["L_AMT0"]=>
string(7) "20%2e00"
["L_CURRENCYCODE0"]=>
string(3) "CAD"
["L_FEEAMT0"]=>
string(9) "%2d0%2e88"
["L_NETAMT0"]=>
string(7) "19%2e12"
["TIMESTAMP"]=>
string(28) "2011%2d12%2d07T18%3a19%3a40Z"
["CORRELATIONID"]=>
string(13) "53733eef8b4e2"
["ACK"]=>
string(7) "Success"
["VERSION"]=>
string(6) "51%2e0"
["BUILD"]=>
string(7) "2230381"
}
For my testing I was using PayPal sandbox and it was woking great (we can see that we have a transaction)... I have no idea why it isn't working with the real one. As we can see, my creedentials are good because it does not give me an authentification error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论