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

时间:2016-05-08 16:38:58

标签: php mysql paypal paypal-ipn

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

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

所以如何更新数据库而不用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>";
}
?>

1 个答案:

答案 0 :(得分:1)

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

这是错误的:

  

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

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

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

有关