PayPal AdaptivePayments PaymentDetail PayKey

时间:2011-02-06 18:47:25

标签: php paypal paypal-adaptive-payments

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

问题是:我需要在return-url中有一个PayKey。原因是:我需要调用PaymentDetail API来审核return_url中的付款。并且,我不想使用IPN,因为我需要在返回的Url上使用一些令牌进行验证。

我遇到的问题是:PayKey正在生成所有参数,包括return-url(因此在我构建实际数组后,我从中得到我的$响应。我不能将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 个答案:

答案 0 :(得分:6)

我自己也在这个时代。我终于弄明白了。 Paypal文档很难遵循。我在下载的paypal自适应pdf指南中找到了答案。它指定将payKey=${payKey}添加到return_url的末尾。我只是在那里尝试过,paypal获取请求到我的返回URL现在包含paykey。

因此,我使用return_url的轨道就像这样。按照指南

的指示将php变量(我认为)写入url
:return_url      => "http://***********.com/paypal-return?payKey=${payKey}"

答案 1 :(得分: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表示事务的其余部分,因此您不必构建另一个巨型查询字符串。

答案 2 :(得分: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}解释为双引号之间的变量。 更改简单引号(')

的双引号(“)

答案 3 :(得分:0)

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

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