简体   繁体   English

贝宝IPN可在沙盒中工作,但不能实时运行

[英]Paypal IPN works in sandbox but not live

I have obtained and adapted the following code from here http://www.sanwebe.com/2012/07/paypal-instant-payment-notification-ipn/comment-page-1#comment-6871 我已从此处http://www.sanwebe.com/2012/07/paypal-instant-payment-notification-ipn/comment-page-1#comment-6871获取并改编了以下代码

<?php
include 'dbdata.php';
//Change these with your information
$paypalmode = 'sandbox'; //Sandbox for testing or empty ''
$paypalmode = '';
if($_POST)
{
    if($paypalmode=='sandbox')
    {
        $paypalmode     =   '.sandbox';
    }
    $req = 'cmd=' . urlencode('_notify-validate');
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_URL, 'https://www'.$paypalmode.'.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    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_HTTPHEADER, array('Host: www'.$paypalmode.'.sandbox.paypal.com'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
    $res = curl_exec($ch);
    curl_close($ch);

    if (strcmp ($res, "VERIFIED") == 0)
    {
        $transactionID = $_POST['txn_id'];
        $payerEmail = $_POST['payer_email'];
        $paymentStatus = $_POST['payment_status'];
        $firstName = $_POST['first_name'];
        $surname = $_POST['last_name'];
        $payerID = $_POST['payer_id'];
        $addressName = $_POST['address_name'];         
        $addressCountry = $_POST['address_country'];
        $addressCountryCode = $_POST['address_country_code'];
        $addressZip = $_POST['address_zip'];
        $addressState = $_POST['address_state'];
        $addressCity = $_POST['address_city'];
        $addressStreet = $_POST['address_street'];
        $mcGross = $_POST['mc_gross'];
        $mcShipping = $_POST['mc_shipping'];
        $mcCurrency = $_POST['mc_currency'];
        $paymentDate = $_POST['payment_date'];


        $mdate= date('Y-m-d H:i:s',strtotime($paymentDate));
        $otherStuff = json_encode($_POST);

          $query5 = "INSERT INTO paypal (transationID,payerEmail,paymentStatus,firstName,surname,paymentDate,payerID,addressName,addressCountry,addressCountryCode,addressZip, addressState,addressCity,addressStreet,mcGross,mcShipping,mcCurrency,allData) VALUES  ('".$transactionID."','".$payerEmail."','".$paymentStatus."','".$firstName."','".$surname."','".$mdate."','".$payerID."','".$addressName."','".$addressCountry."','".$addressCountryCode."','".$addressZip."','".$addressState."','".$addressCity."','".$addressStreet."','".$mcGross."','".$mcShipping."','".$mcCurrency."','".$otherStuff."')";


     mysql_query($query5); 
  $mail_From    = "xxx@googlemail.com";
  $mail_To      = "xxx@googlemail.com";
  $mail_Subject = "VERIFIED IPN";
  $mail_Body    = $otherStuff;
  //mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

    }

    if (strcmp ($res, "INVALID") == 0)
    {
        $transaction_id = $_POST['txn_id'];
        $payerid = $_POST['payer_id'];
        $firstname = $_POST['first_name'];
        $lastname = $_POST['last_name'];
        $payeremail = $_POST['payer_email'];
        $paymentdate = $_POST['payment_date'];
        $paymentstatus = $_POST['payment_status'];
        $mdate= date('Y-m-d h:i:s',strtotime($paymentdate));
        $otherStuff = json_encode($_POST);

         $query5 = "INSERT INTO paypal (transationID,payerEmail,paymentStatus) VALUES  ('".$transaction_id."','".$payeremail."','".$paymentstatus."')";
     mysql_query($query5); 

    }
}
?>

If I use sandbox mode and the IPN simulator I get the data inserted correctly into the tables. 如果我使用沙盒模式和IPN模拟器,则会将数据正确插入表中。 I have now set the clients paypal account's IPN setting to point to my script and turned on IPN but I do not get anything happening. 现在,我已将客户的贝宝帐户的IPN设置设置为指向我的脚本并打开了IPN,但没有任何反应。

The web site is using express checkout. 该网站正在使用快速结帐。 The web site is USA based. 该网站位于美国。 The php script is hosted on our server (UK). php脚本托管在我们的服务器(英国)上。

Paypal is showing sent and if I re-send the IPN's they re-appear as sent. 贝宝显示已发送,如果我重新发送IPN,则它们会重新显示为已发送。 Can anyone see what is wrong here? 谁能看到这里出什么问题了?

  • Update. 更新。 I have used my UK based paypal account and set the IPN notification to the same URL and sent a payment from my wife's paypal account and I got an entry into the database. 我已经使用了基于英国的Paypal帐户,并将IPN通知设置为相同的URL,并从我妻子的Paypal帐户发送了付款,然后我进入了数据库。

Also I do not get my wife's customer details from the IPN but I do from the simulator. 另外,我没有从IPN获取我妻子的客户详细信息,但从模拟器获得了。

MrWarby 沃比先生

Just replace 只需更换

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

with

foreach ($_POST as $key => $value){
    $value = urlencode(stripslashes($value));
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
    $req .= "&$key=$value";         
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM