Php Paypal IPN集成

Php Paypal IPN集成,php,paypal,e-commerce,paypal-ipn,Php,Paypal,E Commerce,Paypal Ipn,因此,我正在努力获得经常性付款,以便在我正在开发的网站上使用(PHP)。我已经在我的Paypal帐户上启用了IPN,并设置了按钮等 当我点击我网站上的按钮时,它会把我带到Paypal,我完成注册的过程。然后,它使用大的“auth”get变量将我转发回正确的url。交易历史记录可以在我的卖方和买方帐户中查看 问题在于更新我的数据库(mysql)。我尝试了几种不同的IPN脚本,并将Google、Stackoverflow和各种教程中的不同脚本拼凑在一起。我尝试过的所有方法都没有更新我的数据库,这让我

因此,我正在努力获得经常性付款,以便在我正在开发的网站上使用(PHP)。我已经在我的Paypal帐户上启用了IPN,并设置了按钮等

当我点击我网站上的按钮时,它会把我带到Paypal,我完成注册的过程。然后,它使用大的“auth”get变量将我转发回正确的url。交易历史记录可以在我的卖方和买方帐户中查看

问题在于更新我的数据库(mysql)。我尝试了几种不同的IPN脚本,并将Google、Stackoverflow和各种教程中的不同脚本拼凑在一起。我尝试过的所有方法都没有更新我的数据库,这让我相信是我的方法出了问题

以下是我的ipn.php脚本:

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
// 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";

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

$notify_email =  "xxx"; //email address to which debug emails are sent to
$DB_Server = "xxx"; //your MySQL Server
$DB_Username = "xxx"; //your MySQL User Name
$DB_Password = "xxx"; //your MySQL Password
$DB_DBName = "xxx"; //your MySQL Database Name


if (!$fp) 
{
    // HTTP ERROR
} 
else 
{
mail($notify_email, "IPN Triggered 1", "IPN Triggered 1");  

fputs ($fp, $header . $req);

while (!feof($fp)) 
{
    //Already used this
    //$res = fgets ($fp, 1024);

    //Using this to see if it sends response
    $res = stream_get_contents($fp, 1024);


    if (strcmp ($res, "VERIFIED") == 0) 
    {   
        mail($notify_email, "VERIFIED 1", "VERIFIED 1");

        //create MySQL connection
        $Connect = mysql_connect($DB_Server, $DB_Username, $DB_Password)
        or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());


        //select database
        $Db = @mysql_select_db($DB_DBName, $Connect)
        or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());


        $fecha = date("m")."/".date("d")."/".date("Y");
        $fecha = date("Y").date("m").date("d");

        //check if transaction ID has been processed before
        $checkquery = "SELECT txn_id FROM payments WHERE txn_id='".$txn_id."'";
        $sihay = mysql_query($checkquery) or die("Duplicate txn id check query failed:<br>" . mysql_error() . "<br>" . mysql_errno());
        $nm = mysql_num_rows($sihay);

        if ($nm == 0)
        {
            //execute query
            if ($txn_type == "subscr_signup")
            {
                $strQuery = "INSERT INTO payments SET txn_id='".$txn_id."', user_id='$custom'";

                 $result = mysql_query($strQuery) or die("Cart - paypal_payment_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno());
            }
        // send an email in any case
         //echo "Verified";
            mail($notify_email, "VERIFIED IPN", "$res\n $req\n $strQuery\n $struery\n  $strQuery2");
        }
        else 
        {
            // send an email
            mail($notify_email, "VERIFIED DUPLICATED TRANSACTION", "$res\n $req \n $strQuery\n $struery\n  $strQuery2");
        }
    }

    // if the IPN POST was 'INVALID'...do this
    else if (strcmp ($res, "INVALID") == 0) 
    {
        // log for manual investigation
        mail($notify_email, "INVALID IPN", "$res\n $req");
    }
}

fclose ($fp);
}
?>

出于某种原因,在Paypal上禁用然后重新启用我的IPN似乎可以解决问题。

您的查询变量似乎没有设置在任何地方<例如,code>$txn_id
。感谢您的评论,我确实将变量设置在DB变量设置位置的正下方,以及if语句的正上方。我把它们拿出来是因为这是一个相当长的变量列表,我觉得没有必要包括在内。