PayPal AdaptivePayments PaymentDetail PayKey

发布于 2024-10-15 21:42:57 字数 3668 浏览 2 评论 0原文

我正在使用 PayPal Pay API 和自适应(链式)付款。我正在尝试将用户转发到 PayPal,然后返回到我预定义的 return_url。

问题是:我的返回网址中需要有一个 PayKey。原因:我需要调用 PaymentDetail API 来查看 return_url 中的付款。而且,我不想使用 IPN,因为我需要在返回 URL 上使用一些令牌进行验证。

我遇到的问题是: PayKey 是使用所有参数生成的,包括 return-url (因此在我构建从中获取 $response 的实际数组之后。我无法将 PayKey 放在 return-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" => "[email protected]", //TODO
                       "receiverList.receiver(0).amount" => $price, //TODO
                       "receiverList.receiver(0).primary" => "true", //TODO
                       "receiverList.receiver(1).email" => "[email protected]", //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/>";
     }

有人可以帮忙吗?

I am using the PayPal Pay API, with Adaptive (Chained) Payments. I am trying to forward a user to paypal and afterwards back to my predefined return_url.

The problem is: I need to have a PayKey within my return-url. Reason for that: I need to call a PaymentDetail API to review the payment within the return_url. And, I don't want to use IPN since I need the validation with some token right on my return Url.

The problem I have is: The PayKey is beeing generated with all the parameters, including the return-url (hence after I build the actual array from which I get my $response from. I can't put the PayKey in the return-Url since it's not generated at this point yet.

  //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" => "[email protected]", //TODO
                       "receiverList.receiver(0).amount" => $price, //TODO
                       "receiverList.receiver(0).primary" => "true", //TODO
                       "receiverList.receiver(1).email" => "[email protected]", //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/>";
     }

Can somebody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

渡你暖光 2024-10-22 21:42:57

我自己也从事这个工作很多年了。我终于想通了。 Paypal 文档很难理解。我在下载的 paypal 自适应 pdf 指南中找到了答案。它指定将 payKey=${payKey} 添加到 return_url 的末尾。我刚刚在那里尝试过,对我的返回网址的 paypal 获取请求现在包含 paykey。

所以在 Rails 中我使用的 return_url 看起来像这样。按照指南的指示将 php 变量(我认为)写入 url

:return_url      => "http://***********.com/paypal-return?payKey=${payKey}"

I was at this for ages too myself. I finally figured it out. Paypal docs are hard to follow. I found the answer in the paypal adaptive pdf guide which I downloaded. It specifies to add payKey=${payKey} to the end of your return_url. I just tried it there and the paypal get request to my return url now contains the paykey.

So in rails which I'm using the return_url looks like this. Writing a php variable (I think) into the url as instructed by the guide

:return_url      => "http://***********.com/paypal-return?payKey=${payKey}"
她比我温柔 2024-10-22 21:42:57

您似乎遗漏了序列中的一个步骤。

第一步是将您的交易参数发送至

https://svcs.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
[sandbox]https://svcs.sandbox.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah

您将在此响应中获得付款密钥。

成功检索到 paykey 后,您将调用:

https://www.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
[sandbox]https://www.sandbox.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx

在第二次调用中,payKey 代表交易的其余部分,因此您不必构建另一个巨大的查询字符串。

It seems that you're missing a step in the sequence.

The first step is to send your transaction parameters to

https://svcs.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah
[sandbox]https://svcs.sandbox.paypal.com/AdaptivePayments/Pay&yourtransactionparameters=blah

You'll get the paykey in this response.

Once you've retrieved the paykey successfully, you'll call:

https://www.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx
[sandbox]https://www.sandbox.paypal.com/webscr&cmd=_ap-payment&paykey=xxxx

In the second call, the payKey represents the rest of your transaction so you don't have to build another giant query string.

独﹏钓一江月 2024-10-22 21:42:57

更改代码:

//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" => "[email protected]", //TODO
                       "receiverList.receiver(0).amount" => $price, //TODO
                       "receiverList.receiver(0).primary" => "true", //TODO
                       "receiverList.receiver(1).email" => "[email protected]", //TODO
                       "receiverList.receiver(1).amount" => $receiver_gets, //TODO
                       "receiverList.receiver(1).primary" => "false" //TODO
                       );

PHP 将 ${payKey} 解释为双引号之间的变量。
将双引号 (") 更改为单引号 (')

Change your code for:

//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" => "[email protected]", //TODO
                       "receiverList.receiver(0).amount" => $price, //TODO
                       "receiverList.receiver(0).primary" => "true", //TODO
                       "receiverList.receiver(1).email" => "[email protected]", //TODO
                       "receiverList.receiver(1).amount" => $receiver_gets, //TODO
                       "receiverList.receiver(1).primary" => "false" //TODO
                       );

PHP interprets ${payKey} as variable between the double quotes.
Change double quotes (") for simple quotes (')

淡忘如思 2024-10-22 21:42:57

显然,您可以在返回 URL 中嵌入动态变量: https://www.x.com/thread/49785< /a>

不幸的是,{$payKey} 无效。所以它必须是 $paykey 或 $pay_key。祝你好运!

Apparently you can embed dynamic variables in the return URL: https://www.x.com/thread/49785

Unfortunately, {$payKey} is invalid. so it must be $paykey or $pay_key. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文