PayPal AdaptivePayments PaymentDetail PayKey

Posted

技术标签:

【中文标题】PayPal AdaptivePayments PaymentDetail PayKey【英文标题】: 【发布时间】:2011-06-22 08:49:46 【问题描述】:

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

问题是:我需要在我的返回 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" => "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/>";
     

有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

我自己也有很多年了。我终于弄明白了。 Paypal 文档很难遵循。我在下载的 paypal 自适应 pdf 指南中找到了答案。它指定将payKey=$payKey 添加到return_url 的末尾。我刚刚在那里尝试过,对我的返回 URL 的贝宝获取请求现在包含支付密钥。

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

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

【讨论】:

这真的很简单。我正在查看 PayPal 的 IPN 文档并看到了这一点,并认为我只需要将其添加到 URL 中就可以了。完美。【参考方案2】:

您似乎缺少序列中的一个步骤。

第一步是将你的交易参数发送到

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 代表您交易的其余部分,因此您不必构建另一个巨大的查询字符串。

【讨论】:

【参考方案3】:

更改您的代码:

//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 解释为双引号之间的变量。 将双引号 (") 更改为单引号 (')

【讨论】:

【参考方案4】:

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

很遗憾,$payKey 无效。所以它必须是 $paykey 或 $pay_key。祝你好运!

【讨论】:

以上是关于PayPal AdaptivePayments PaymentDetail PayKey的主要内容,如果未能解决你的问题,请参考以下文章

什么会导致 PayPal 的 AdaptivePayments API 返回“内部服务器错误。请检查服务器日志以获取详细信息”?

Paypal自适应退款

如何使用 Paypal 自适应支付 SDK?

Ruby 的 Paypal 自适应支付

无法转换货币 paypal pay API 错误

Paypal API,未收到 IPN 消息