Php PayPal自适应支付PaymentDetail PayKey
我使用的是PayPal支付API,带有自适应(链接)支付。我正在尝试将用户转发到paypal,然后返回到预定义的返回url 问题是:我需要在我的返回url中有一个PayKey。原因:我需要调用PaymentDetail API来查看return_url中的付款。而且,我不想使用IPN,因为我需要在返回Url上使用一些令牌进行验证 我的问题是:PayKey是使用所有参数生成的,包括返回url(因此,在我构建从中获取$response的实际数组之后)。我无法将PayKey放入返回url中,因为此时尚未生成它Php PayPal自适应支付PaymentDetail PayKey,php,paypal,paypal-adaptive-payments,Php,Paypal,Paypal Adaptive Payments,我使用的是PayPal支付API,带有自适应(链接)支付。我正在尝试将用户转发到paypal,然后返回到预定义的返回url 问题是:我需要在我的返回url中有一个PayKey。原因:我需要调用PaymentDetail API来查看return_url中的付款。而且,我不想使用IPN,因为我需要在返回Url上使用一些令牌进行验证 我的问题是:PayKey是使用所有参数生成的,包括返回url(因此,在我构建从中获取$response的实际数组之后)。我无法将PayKey放入返回url中,因为此时尚
//Create request payload with minimum required parameters
$bodyparams = array ("requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"currencyCode" => "USD",
"cancelUrl" => "http://www.paypal.com",
"returnUrl" => $return_url . "&payKey=${payKey}", **// Does not work - PAYKEY NEEDED TO ADD???**
"receiverList.receiver(0).email" => "account1@hotmail.com", //TODO
"receiverList.receiver(0).amount" => $price, //TODO
"receiverList.receiver(0).primary" => "true", //TODO
"receiverList.receiver(1).email" => "account2@hotmail.com", //TODO
"receiverList.receiver(1).amount" => $receiver_gets, //TODO
"receiverList.receiver(1).primary" => "false" //TODO
);
// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38)); // Generates body data
try
{
//create request and add headers
$params = array("http" => array(
"method" => "POST",
"content" => $body_data,
"header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
"X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
"X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
"X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
"X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
"X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
));
//create stream context
$ctx = stream_context_create($params);
//open the stream and send request
$fp = @fopen($url, "r", false, $ctx);
//get response
$response = stream_get_contents($fp);
//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
fclose($fp);
//parse the ap key from the response
$keyArray = explode("&", $response);
foreach ($keyArray as $rVal){
list($qKey, $qVal) = explode ("=", $rVal);
$kArray[$qKey] = $qVal;
}
//set url to approve the transaction
$payPalURL = "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=" . $kArray["payKey"]; **// Here it works fine, since the PayKey is generated at this point ...**
//print the url to screen for testing purposes
If ( $kArray["responseEnvelope.ack"] == "Success") {
echo '<p><a href="' . $payPalURL . '" target="_blank">' . $payPalURL . '</a></p>';
}
else {
echo 'ERROR Code: ' . $kArray["error(0).errorId"] . " <br/>";
echo 'ERROR Message: ' . urldecode($kArray["error(0).message"]) . " <br/>";
}
//使用所需的最小参数创建请求负载
$bodyparams=数组(“requestEnvelope.errorLanguage”=>“en_US”,
“actionType”=>“支付”,
“货币代码”=>“美元”,
“取消URL”=>“http://www.paypal.com",
“returnUrl”=>$return\u url.&payKey=${payKey}”,***//不工作-需要添加payKey**
“接收者列表。接收者(0)。电子邮件”=>”account1@hotmail.com“,//待办事项
“receiverList.receiver(0).amount”=>$price,//待办事项
“receiverList.receiver(0).primary”=>“true”,//TODO
“接收者列表。接收者(1)。电子邮件”=>”account2@hotmail.com“,//待办事项
“receiverList.receiver(1).amount”=>$receiver\u获取,//TODO
“receiverList.receiver(1).primary”=>“false”//TODO
);
//将有效负载数组转换为url编码的查询字符串
$body_data=http_build_query($bodyparams,“,chr(38));//生成body数据
尝试
{
//创建请求并添加标题
$params=array(“http”=>array(
“方法”=>“发布”,
“内容”=>$body\u数据,
“header”=>“X-PAYPAL-SECURITY-USERID:”.$API\u UserName.“\r\n”。
“X-PAYPAL-SECURITY-SIGNATURE:”.$API\U签名。“\r\n”。
“X-PAYPAL-SECURITY-PASSWORD:”.$API\U密码。“\r\n”。
“X-PAYPAL-APPLICATION-ID:.$API\U AppID.\r\n”。
“X-PAYPAL-REQUEST-DATA-FORMAT:”.$API\U RequestFormat.“\r\n”。
X-PAYPAL-RESPONSE-DATA-FORMAT:“.$API\U ResponseFormat”。\r\n”
));
//创建流上下文
$ctx=流\u上下文\u创建($params);
//打开流并发送请求
$fp=@fopen($url,“r”,false,$ctx);
//得到回应
$response=stream\u get\u内容($fp);
//检查流是否打开
如果($response==false){
抛出新异常(“php错误消息=”.“$php_errormsg”);
}
fclose($fp);
//从响应中解析ap密钥
$keyArray=explode(&,$response);
foreach($keyArray作为$rVal){
列表($qKey,$qVal)=分解(“=”,$rVal);
$kArray[$qKey]=$qVal;
}
//设置url以批准交易
$payPalURL=”https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=“.$kArray[“paykey”];**//在这里它可以正常工作,因为paykey是在此时生成的**
//出于测试目的,将url打印到屏幕
If($kArray[“responseenevelope.ack”]=“Success”){
回声“”;
}
否则{
回显“错误代码:”.$kArray[“错误(0).errorId”]。“
”;
echo“ERROR Message:”.urldecode($kArray[“ERROR(0).Message”])。“
”;
}
有人能帮忙吗?显然,您可以在返回URL中嵌入动态变量:
不幸的是,{$payKey}无效。因此它必须是$payKey或$pay_key。祝您好运!您似乎错过了序列中的一个步骤 第一步是将事务参数发送到
https://svcs.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
[sandbox]https://svcs.sandbox.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
您将在该响应中获得支付密钥
成功检索支付密钥后,您将调用:
https://www.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
[sandbox]https://www.sandbox.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
在第二次调用中,payKey代表事务的其余部分,因此您不必构建另一个巨大的查询字符串。更改代码:
//Create request payload with minimum required parameters
$bodyparams = array ("requestEnvelope.errorLanguage" => "en_US",
"actionType" => "PAY",
"currencyCode" => "USD",
"cancelUrl" => "http://www.paypal.com",
"returnUrl" => $return_url . '&payKey=${payKey}', **// That's right**
"receiverList.receiver(0).email" => "account1@hotmail.com", //TODO
"receiverList.receiver(0).amount" => $price, //TODO
"receiverList.receiver(0).primary" => "true", //TODO
"receiverList.receiver(1).email" => "account2@hotmail.com", //TODO
"receiverList.receiver(1).amount" => $receiver_gets, //TODO
"receiverList.receiver(1).primary" => "false" //TODO
);
PHP将${payKey}解释为双引号之间的变量。
将双引号(“)改为简单引号(”)我自己也这么做了很久。我终于明白了。Paypal文档很难理解。我在下载的Paypal adaptive pdf指南中找到了答案。它指定添加
payKey=${payKey}
到您的返回\u url
的末尾。我刚刚在那里尝试过,对我的返回url的paypal get请求现在包含了paykey
因此,在rails中,我使用的return\uURL
如下所示
:return_url => "http://***********.com/paypal-return?payKey=${payKey}"
这真的很简单。我在浏览PayPal的IPN文档时看到了这一点,我想我只需要将它添加到URL中就可以了。非常好。