PayPal IPN变量的问题



im使用paypal作为主支付系统,因此我决定在自定义构建的脚本中添加它,但是我对IPN变量有问题,因为当我尝试回声时,例如

$item_number
$txn_id

我从上面的变量中一无所获。一切都正确地传递给了PayPal并收到了付款,但是当用户重定向到我的"感谢"页面时,我需要抓取该$ item_number项目编号以更新数据库。我目前使用沙箱进行测试。

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

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
}
?>

非常感谢您的帮助!

您正在混淆PDT和IPN。PDT是将数据发送回您的返回URL的原因。IPN与您的结帐流完全分开发生,并且没有任何人在屏幕上看到的东西,因此任何人都不会看到数据。

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

看来您只是从PayPal中取出IPN样品,因此您需要自定义以适应您的需求,然后再记住,这不是您在屏幕上看到的任何东西。您需要检查PayPal的IPN历史记录和您的Web服务器日志,以查看是否受到打击以及是否发生任何错误。

当然,您也需要确保在PayPal配置文件中也配置了IPN,或者您已经在付款代码中设置了Notify_url。

因此,您的感谢页面确实不过是这样,然后IPN将处理更新数据库并通知用户他们需要知道的任何内容。

相关内容

  • 没有找到相关文章

最新更新