6

我正在使用带有自适应(链式)支付的 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/>";
     }

有人可以帮忙吗?

4

4 回答 4

6

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

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

:return_url      => "http://***********.com/paypal-return?payKey=${payKey}"
于 2014-10-17T19:46:09.800 回答
3

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

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

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

于 2012-03-02T18:53:46.517 回答
2

更改您的代码:

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

于 2013-09-04T12:24:34.893 回答
0

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

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

于 2011-03-04T13:32:52.507 回答