我可以在没有贝宝重定向到成功页面的情况下更新数据库吗


Can i update the database without paypal redirecting to success page?

对不起,我是这个PAYPAL IPN 的新手

所以我得到了我的IPN文件
因此,我的问题是,一旦用户付费后被重定向到success/ipn页面,我的脚本就会更新记录
但如果用户没有被重定向怎么办?我自己测试了一下。paypal将我重定向到paypal THANK YOU ORDER页面,然后mozilla弹出一个窗口提醒,说"你连接到一个不安全的网站bla bla CLICK(是)或(取消)"。如果我点击是,我就会被重定向到我的成功/ipn页面,
但如果我点击取消,我将留在PAYPAL谢谢页面
所以我担心用户可能会点击取消,他不会被重定向,数据也不会被更新

那么我如何在没有贝宝重定向到我的成功/ipn页面的情况下更新数据库

,请建议,不要投反对票

$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'); // 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,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
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);
// WRITE LOG 
$fh = fopen('result.txt', 'w');
fwrite($fh, $res .'--'. $req);
fclose($fh);

// 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
$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 = $_POST['payer_email'];
$custom = $_POST['custom'];
// Insert your actions here
if(($payment_status == 'Completed') && ($receiver_email == 'paypal-test@bolddata-ap.com')){
        include('php/config.php'); //Open Database connection
        $check = $dbo->prepare('SELECT * FROM order_bytes WHERE trans_id = ?');
        $check->execute(array($txn_id));
        if($check->rowCount() >= 1){
            die('ERROR FOUND SAME TXN ID IN DATABASE');
        }else{
            /*Data token = EAZZn0OkC2zo4H3CF9vrrSlU-grBGr0oQzE8NZ6jnYGRTuJkJS0howDNK48*/
                        $stmt = $dbo->prepare("UPDATE `order_bytes` SET stat=? ,paid=?, trans_id=? WHERE `tracker`=? AND `user_id`=?");
                        /*$stmt->bindValue(":stats", 'PAID', PDO::PARAM_STR);
                        $stmt->bindParam(":pay", $item_price, PDO::PARAM_STR);
                        $stmt->bindParam(":it", $item_transaction, PDO::PARAM_STR);                            
                        $stmt->bindParam(":trackers", $_SESSION['random_id'], PDO::PARAM_STR);
                        $stmt->bindParam(":uid", $_SESSION['byte_user'], PDO::PARAM_STR);*/
                        if(!$stmt->execute(array($payment_status,$payment_amount,$txn_id,$_SESSION['tracker'],$_SESSION['byte_user']))){
                            //print_r($stmt->errorInfo());
                        }else {
                            if($stmt->rowCount() == 1){
                                notify(getbyteuser($item_no),'A User Ordered your Byte<br />Tracker ID: '.$_SESSION['tracker'].'','NEW ORDER');
                            //mail_booked($item_transaction);
                            echo "<div class='"alert alert-success'"><h3>Thank you<br>Your payment has been successfull</h3>
                                    <p>Keep track of your order in your Dashboard</p>
                                </div>";
                            echo '<a href="index.php" class="btn btn-primary" rel="noindex" nofollow="">Done</a>';
                            }else{
                                print alert_danger('Error');
                            }
                        }
        }
}
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
     echo "The response from IPN was: <b>" .$res ."</b>";
}
?>

您对IPN和返回页感到困惑。

这是错误的:

所以我的问题是,一旦用户付费,就会被重定向到成功/ipn页面

IPN不是一个成功的页面。这是PayPal在后台调用的一个页面,用于通知您交易状态。它独立于用户会话。(notify_url)

PayPal在完成(或取消)付款后重定向用户的页面与IPN不同。这与用户会话(return_url)

有关