【问题标题】:Paypal IPN Integration贝宝 IPN 集成
【发布时间】:2013-11-23 17:19:50
【问题描述】:

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

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

问题在于更新我的数据库(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);
}
?>

【问题讨论】:

  • 看起来您的任何查询变量都没有设置在任何地方。例如$txn_id
  • 感谢您的评论,我确实将我的变量设置在我的数据库变量设置的正下方,以及 if 语句的正上方。我把它们拿出来是因为我觉得不需要包含的变量列表很长。

标签: php paypal e-commerce paypal-ipn


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 2016-07-13
    • 2011-10-09
    • 2012-08-06
    • 2015-06-16
    • 2016-05-09
    • 2015-01-02
    • 2016-09-12
    • 2023-03-11
    相关资源
    最近更新 更多