PayPal IPN模拟器无法正常工作?

时间:2015-04-24 14:23:20

标签: php paypal paypal-ipn

我正在尝试在事务完成时将新条目插入到我的数据库中。所以我在服务器上创建了这个IPN侦听器脚本:

    <?php

include 'MessageManager.php';

// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream. 
$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 IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$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";
}
$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'));
if( !($res = curl_exec($ch)) ) {
    error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);

if (preg_match("!(VERIFIED)\s*\Z!",$res)){
    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $quantity = $_POST['custom'];
    $receiver_email = $_POST['receiver_email'];

    if($payment_status === "Completed"){
        addMessage($item_name,(int) $quantity , $payment_amount);
    } else {
    }

} else {
}
?>

这是基于PayPal提供的脚本。我正在使用IPN模拟器来测试脚本。该脚本应该在销售完成后在我的数据库中插入一个新条目。插入代码本身运行良好,但是当我运行模拟器时,它表示在paypal sute上成功,但是我的数据库中没有新条目。

我做错了什么吗?

编辑:我收到此错误消息:

  

1SSL证书问题失败

比你的时间。

1 个答案:

答案 0 :(得分:2)

尝试关闭ssl verify选项。我检查了我的IPN脚本,它就在那里。这里有some caveats,但它应该是安全的,因为您要连接到PayPal并验证数据。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);