简体   繁体   中英

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

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

The web site is using express checkout. The web site is USA based. The php script is hosted on our server (UK).

Paypal is showing sent and if I re-send the IPN's they re-appear as sent. 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.

Also I do not get my wife's customer details from the IPN but I do from the simulator.

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";         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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