【问题标题】:paypal IPN doesnt add data to the mysql?paypal IPN 不向 mysql 添加数据?
【发布时间】:2013-01-25 00:09:39
【问题描述】:

paypal IPN由于某种原因没有将数据输入mysql数据库! 我正在使用贝宝沙箱,付款通过,它们显示在我的沙箱帐户中,但详细信息不会显示在 mysql 中!!

这是我的 IPN 文件。尽管我将其称为payments.php,并且我还使用IPN模拟器指向它,但它确实表示IPN发送成功。所以我不知道这里有什么问题:

<?php
// Database variables
$host = "localhost"; //database location
$user = "my details"; //database username
$pass = "pass"; //database password
$db_name = "my details"; //database name

// PayPal settings
$paypal_email = 'MYSANDBOXEMAIL@PAYPAL.COM';
$return_url = 'http://some site/';
$cancel_url = 'http://some site/';
$notify_url = 'http://some site/thanks.php';

$item_name = 'Test Item';
$item_amount = 5.00;

// Include Functions
include("functions.php");

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

    // Firstly Append paypal account to querystring
    $querystring .= "?business=".urlencode($paypal_email)."&";  

    // Append amount& currency (£) to quersytring so it cannot be edited in html

    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }

    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
    $querystring .= "notify_url=".urlencode($notify_url);

    // Append querystring with custom field
    //$querystring .= "&custom=".USERID;

    // Redirect to paypal IPN
    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
    exit();

}else{

    // Response from Paypal

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    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";
    }

    // assign posted variables to local variables
    $data['item_name']          = $_POST['item_name'];
    $data['item_number']        = $_POST['item_number'];
    $data['payment_status']     = $_POST['payment_status'];
    $data['payment_amount']     = $_POST['mc_gross'];
    $data['payment_currency']   = $_POST['mc_currency'];
    $data['txn_id']             = $_POST['txn_id'];
    $data['receiver_email']     = $_POST['receiver_email'];
    $data['payer_email']        = $_POST['payer_email'];
    $data['custom']             = $_POST['custom'];

    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

    if (!$fp) {
        // HTTP ERROR
    } else {    

        fputs ($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp($res, "VERIFIED") == 0) {

                // Used for debugging
                //@mail("you@youremail.com", "PAYPAL DEBUGGING", "Verified Response<br />data = <pre>".print_r($post, true)."</pre>");

                // Validate payment (Check unique txnid & correct price)
                $valid_txnid = check_txnid($data['txn_id']);
                $valid_price = check_price($data['payment_amount'], $data['item_number']);
                // PAYMENT VALIDATED & VERIFIED!
                if($valid_txnid && $valid_price){               
                    $orderid = updatePayments($data);       
                    if($orderid){                   
                        // Payment has been made & successfully inserted into the Database                              
                    }else{                              
                        // Error inserting into DB
                        // E-mail admin or alert user
                    }
                }else{                  
                    // Payment made but data has been changed
                    // E-mail admin or alert user
                }                       

            }else if (strcmp ($res, "INVALID") == 0) {

                // PAYMENT INVALID & INVESTIGATE MANUALY! 
                // E-mail admin or alert user

                // Used for debugging
                //@mail("you@youremail.com", "PAYPAL DEBUGGING", "Invalid Response<br />data = <pre>".print_r($post, true)."</pre>");
            }       
        }       
    fclose ($fp);
    }   
}
?>

更新:这是function.php..我希望这可以帮助有人帮助我解决这个问题!

<?php
// functions.php
function check_txnid($tnxid){
    global $link;
    return true;
    $valid_txnid = true;
    //get result set
    $sql = mysql_query("SELECT * FROM payments WHERE txnid = '$tnxid'", $link);     
    if($row = mysql_fetch_array($sql)) {
        $valid_txnid = false;
    }
    return $valid_txnid;
}

function check_price($price, $id){
    $valid_price = false;
    //you could use the below to check whether the correct price has been paid for the product

    /* 
    $sql = mysql_query("SELECT amount FROM `products` WHERE id = '$id'");       
    if (mysql_numrows($sql) != 0) {
        while ($row = mysql_fetch_array($sql)) {
            $num = (float)$row['amount'];
            if($num == $price){
                $valid_price = true;
            }
        }
    }
    return $valid_price;
    */
    return true;
}

function updatePayments($data){ 
    global $link;
    if(is_array($data)){                
        $sql = mysql_query("INSERT INTO payments (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                '".$data['txn_id']."' ,
                '".$data['payment_amount']."' ,
                '".$data['payment_status']."' ,
                '".$data['item_number']."' ,
                '".date("Y-m-d H:i:s")."' 
                )", $link);
    return mysql_insert_id($link);
    }
}
?>

【问题讨论】:

  • 好吧,我们不想修复您的大 php 文件。告诉我们您首先遇到的错误。
  • updatePayments函数的内容是什么?是否调用过 updatePayments 函数?
  • @j0k,说实话,我不认为这是一个“大”的 php 文件。特别是对于知道自己在做什么的人。不,我根本没有收到任何错误。没有错误,但它也不起作用。
  • @beanland,是的,我已经更新了代码并添加了 function.php 的代码。 updatePayment 在 function.php 文件中调用。有什么建议吗?
  • 我遇到了同样的问题。 OP,你解决过这个问题吗?

标签: php mysql paypal paypal-ipn


【解决方案1】:

看起来你工作有点“盲目”,因为这是一个“无头”脚本,你不知道它在哪里失败。

在各个点添加一些 error_log 行,以便您可以通过脚本检查流程。特别是,在执行数据库查询之前添加 error_log($sql) 会将语句打印到错误日志中。拿这个并尝试在数据库中独立运行它。可能是某些表约束失败而不是 PHP 中的错误

在流程的各个点添加日志行并在这些点打印出变量也将帮助您诊断发生故障的位置,并希望让您更准确地查明错误。

【讨论】:

【解决方案2】:

尝试Printing 或echoing 这些值替换 - // Payment has been made &amp; successfully inserted into the Database

print $data['item_name']; print $data['item_number'];.......

然后我可以从那里更新到正确的地方和错误的地方。

【讨论】:

  • 谢谢,但没有帮助。还是行不通。我的意思是付款按原样进行,但数据库仍然没有被贝宝 IPN 更新!!!
  • 再次感谢,我添加了 $data = array();当我在我的沙盒帐户中使用 IPN 模拟器时,我在 paypal 中收到此错误:IPN 交付失败。 HTTP 错误代码 404:未找到。
  • 好的,试试我提供的最后一次更新并删除$data = array();
  • 好的,我已经尝试了你提到的所有选项,但仍然不起作用!我现在对这个贝宝 IPN 的事情束手无策。没有任何效果。我不知道该怎么办!!!
  • 您好,我是来自 PayPal 的 Tim。您可以执行 var_export 之类的操作,然后将整个 IPN 帖子邮寄给自己。然后,您可以检查哪些数据通过,哪些不通过。
猜你喜欢
  • 2015-04-02
  • 2010-12-14
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 2013-02-22
  • 2015-06-01
相关资源
最近更新 更多