php - PayPal AdaptivePayments PaymentDetail PayKey

标签 php paypal paypal-adaptive-payments

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

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

我遇到的问题是: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/>";
     }

有人可以帮忙吗?

最佳答案

我自己也从事此行业多年。我终于弄明白了。 Paypal 文档很难遵循。我在下载的 paypal adaptive pdf 指南中找到了答案。它指定将 payKey=${payKey} 添加到 return_url 的末尾。我刚刚在那里尝试过,对我的返回 url 的 paypal get 请求现在包含 paykey。

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

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

关于php - PayPal AdaptivePayments PaymentDetail PayKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4915329/

相关文章:

php - 如何使用 php $row 检索 sql datetime 对象?

php - Symfony/PHP - 表单类型类中其他实体内的实体

paypal - 为我的场景选择 Paypal 网关

javascript - PHP - 通知 : Undefined index: and not displaying data

paypal - PayFlow 沙箱集成问题

payment-gateway - Paypal CreateRecurringPaymentsProfile 自动为第一个月计费

Paypal 定期付款

ios - Paypal 信用卡交易在沙盒模式下不起作用

paypal - 使用PayPal编程无需手动输入帐户和密码即可转账

javascript - Mysql 数据库的 Ajax 请求