PayPal IPN变量的问题

时间:2014-02-02 20:25:58

标签: php paypal paypal-ipn sandbox paypal-sandbox

我使用PayPal作为主要支付系统,因此我决定将其添加到我自定义构建的脚本中,但是我遇到了IPN变量的问题,因为当我尝试回显任何这些变量时,例如

$item_number
$txn_id

我从上面的变量中得不到任何结果。一切都正确传递给PayPal并收到付款,但是当用户被重定向到我的“谢谢”页面时,我需要获取$ item_number项目编号以更新数据库,不会传递任何内容。我现在正在使用Sandbox进行测试。

这是重定向到我的网站时的网址值:

http://example.com/thankyou.php?tx=8LT93279034418017&st=Completed&amt=20%2e00&cc=USD&cm=&item_number=29

这是我的IPN代码:

<?php
// STEP 1: Read POST data

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}


// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment

    // assign posted variables to local variables
    $custom_value = $_POST['custom'];
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}
?>

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您对PDT和IPN感到困惑。 PDT是将数据发送回您的返回URL的内容。 IPN完全独立于您的结账流程,并不是任何人在屏幕上看到的东西,因此任何人都不会看到回应数据。

IPN确实是处理数据库更新,电子邮件通知等的推荐方式,因为即使启用了自动返回功能,也无法保证用户将其返回到ReturnURL(使用Payments Standard时),在这种情况下代码永远不会为这些订单运行。

看起来您只是从PayPal中提取了IPN示例,因此您需要根据自己的需要对其进行自定义,再次请记住,这不是您在屏幕上看到的任何内容。您需要检查PayPal的IPN历史记录和您的Web服务器日志,看它是否被点击以及是否可能发生任何错误。

当然,您还需要确保在PayPal个人资料中配置了IPN,或者您已在付款代码中设置了notify_url。

所以你的感谢页面实际上只是那个,然后IPN将处理更新数据库并通知用户他们需要知道的任何事情。