PayPal IPN适用于沙箱,但不适用于实时

时间:2017-06-12 18:11:52

标签: php paypal paypal-ipn

我已经阅读了很多问题,但我无法找到解决问题的正确方法。

我目前是一家网店,他们可以付款,如果他们付款,我会将其插入我的数据库,但如果我尝试在sandbox.paypal.com上运行此代码,那就完全没问题,我得到它交易表也是如此(我多么想要)。但是,如果我将它从sandbox.paypal.com更改为paypal.com,它将不再起作用。这是我没有沙盒的情况下的代码。

包裹页面(将人们发送给PayPal付款,我会发现一些不应该有意义的信息):

$paypalurl = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amount=".$row['price']."&business=".$paypalemail."&item_name=".$name."&item_number=".$row['id']."&return=".$url."packages.php"."&rm=2&notify_url=**********/gateway/paypalipn.php"."&cancel_return=".$url."packages.php"."&no_note=1&currency_code=USD";

header("Location: ".$paypalurl);

这是IPN文件,问题应该是:

require_once('config.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";
}

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr'); // change to [...]sandbox.paypal[...] when using sandbox to test
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)) ) {
    curl_close($ch);
    exit;
}
curl_close($ch);
if (strcmp ($res, "VERIFIED") == 0) {
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    if ($_POST['mc_gross'] != NULL)
        $payment_amount = $_POST['mc_gross'];
    else
        $payment_amount = $_POST['mc_gross1'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = mysqli_real_escape_string($con,$_POST['payer_email']);

    echo $item_name.'<br>';
    echo $item_number.'<br>';
    echo $payment_status.'<br>';
    echo $payment_amount.'<br>';
    echo $payment_currency.'<br>';
    echo $txn_id.'<br>';
    echo $payer_email.'<br>';

    $date = time();

    $datae = explode("A8D",$item_name);
    $name = $datae[0];
    $uid = $datae[1];
    $uip = $datae[2];

    $query = "INSERT INTO transactions (uid,uip,date,payeremail,paymentstatus,package,amount,currency) VALUES ($uid,'$uip',$date,'$payer_email','$payment_status',$item_number,'$payment_amount','$payment_currency')";
    mysqli_query($con,$query);

    //After uploading in transaction checking if could find package with this id
    //Should be USD
    if($payment_currency == 'USD'){
        $query = "SELECT * FROM packages WHERE price=$payment_amount";
        $result = mysqli_query($con,$query);
        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_assoc($result)){
                $package = $row['id'];
                $date = time();
                if($row['length'] > 0){
                    $length = $date + $row['length'];
                }else{
                    $length = 0;
                }
                $query2 = "UPDATE users SET rank=2,package=$package,length=$length WHERE id=$uid";
                mysqli_query($con,$query2);
            }
        }
    }

} else if (strcmp ($res, "INVALID") == 0) {
    die('Well.. Nice try though! But no ;(');
}

但我不明白,因为它确实适用于sandbox.paypal.com。我只是不知道为什么它适用于sandbox.paypal.com但不适用于现场PayPal版本!

0 个答案:

没有答案