需要 Paypal IPN 侦听器帮助

Posted

技术标签:

【中文标题】需要 Paypal IPN 侦听器帮助【英文标题】:Paypal IPN Listener Help Needed 【发布时间】:2021-09-22 00:05:24 【问题描述】:

大家好, 我在使用 PayPal IPN 时遇到了一些麻烦。看来 PayPal 确实到达了我的 php 侦听器并获得了 200 的回报,一旦交易完成,我将返回我的网站并收到一条正面消息,即我的交易成功,但购买的商品未记入该帐户。我几乎碰壁了,我不确定可能是什么问题。我是否使用过时的命令?有什么不寻常的地方吗?感谢您的宝贵时间!

<?php
require_once('globals_nonauth.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";
$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);

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

if (!$fp)

    // HTTP ERROR

else

    fputs($fp, $header . $req);
    while (!feof($fp))
    
        $res = fgets($fp, 1024);
        if (strcmp($res, "VERIFIED") == 0)
        
            $txn_db = $db->escape(stripslashes($txn_id));
            // check the payment_status is Completed
            if ($payment_status != "Completed")
            
                fclose($fp);
                die("");
            
            $dp_check =
                    $db->query(
                            "SELECT COUNT(`dpID`)
                             FROM `dps_accepted`
                             WHERE `dpTXN` = '$txn_db'");
            if ($db->fetch_single($dp_check) > 0)
            
                $db->free_result($dp_check);
                fclose($fp);
                die("");
            
            $db->free_result($dp_check);
            // check that txn_id has not been previously processed
            // check that receiver_email is your Primary PayPal email
            if ($receiver_email != $set['paypal'])
            
                fclose($fp);
                die("");
            
            // check that payment_amount/payment_currency are correct
            if ($payment_currency != "USD")
            
                fclose($fp);
                die("");
            
            // parse for pack
            $packr = explode('|', $item_name);
            if (str_replace("www.", "", $packr[0])
                    != str_replace("www.", "", $_SERVER['HTTP_HOST']))
            
                fclose($fp);
                die("");
            
            if ($packr[1] != "DP")
            
                fclose($fp);
                die("");
            
            $pack = $packr[2];
            if ($pack != 1 and $pack != 2 and $pack != 3 and $pack != 4
                    and $pack != 5)
            
                fclose($fp);
                die("");
            
            if (($pack == 1 || $pack == 2 || $pack == 3)
                    && $payment_amount != "3.00")
            
                fclose($fp);
                die("");
            
            if ($pack == 4 && $payment_amount != "5.00")
            
                fclose($fp);
                die("");
            
            if ($pack == 5 && $payment_amount != "10.00")
            
                fclose($fp);
                die("");
            
            // grab IDs
            $buyer = abs((int) $packr[3]);
            $for = $buyer;
            // all seems to be in order, credit it.
            if ($pack == 1)
            
                $db->query(
                        "UPDATE `users` AS `u`
                         LEFT JOIN `userstats` AS `us`
                         ON `u`.`userid` = `us`.`userid`
                         SET `u`.`money` = `u`.`money` + 5000,
                         `u`.`crystals` = `u`.`crystals` + 50,
                         `us`.`IQ` = `us`.`IQ` + 50,
                         `u`.`donatordays` = `u`.`donatordays` + 30
                         WHERE `u`.`userid` = $for");
                $d = 30;
                $t = "standard";
            
            else if ($pack == 2)
            
                $db->query(
                        "UPDATE `users` AS `u`
                         SET `u`.`crystals` = `u`.`crystals` + 100,
                         `u`.`donatordays` = `u`.`donatordays` + 30
                         WHERE `u`.`userid` = $for");
                $d = 30;
                $t = "crystals";
            
            else if ($pack == 3)
            
                $db->query(
                        "UPDATE `users` AS `u`
                         LEFT JOIN `userstats` AS `us`
                         ON `u`.`userid` = `us`.`userid`
                         SET `us`.`IQ` = `us`.`IQ` + 50,
                         `u`.`donatordays` = `u`.`donatordays` + 30
                         WHERE `u`.`userid` = $for");
                $d = 30;
                $t = "iq";
            
            else if ($pack == 4)
            
                $db->query(
                        "UPDATE `users` AS `u`
                         LEFT JOIN `userstats` AS `us`
                         ON `u`.`userid` = `us`.`userid`
                         SET `u`.`money` = `u`.`money` + 15000,
                         `u`.`crystals` = `u`.`crystals` + 75,
                         `us`.`IQ` = `us`.`IQ` + 80,
                         `u`.`donatordays` = `u`.`donatordays` + 55
                         WHERE `u`.`userid` = $for");
                $d = 55;
                $t = "fivedollars";
            
            else if ($pack == 5)
            
                $db->query(
                        "UPDATE `users` AS `u`
                         LEFT JOIN `userstats` AS `us`
                         ON `u`.`userid` = `us`.`userid`
                         SET `u`.`money` = `u`.`money` + 35000,
                         `u`.`crystals` = `u`.`crystals` + 160,
                         `us`.`IQ` = `us`.`IQ` + 180,
                         `u`.`donatordays` = `u`.`donatordays` + 115
                         WHERE `u`.`userid` = $for");
                $d = 115;
                $t = "tendollars";
            
            // process payment
            event_add($for,
                    "Your \$$payment_amount Pack $pack Donator Pack has been successfully credited to you.",
                    $c);
            $db->query(
                    "INSERT INTO `dps_accepted`
                     VALUES(NULL, $buyer, $for, '$t', " . time()
                            . ", '$txn_db')");
        
        else if (strcmp($res, "INVALID") == 0)
        
        
    

    fclose($fp);

【问题讨论】:

您为什么要尝试使用fsockopen 发出HTTP 请求?!?为什么这不使用 cURL 或 Guzzle 之类的?在端口80 上连接到www.paypal.com 似乎很不错;我怀疑他们在交换此类关键信息时会想与您的系统使用纯 HTTP 而不是 HTTPS…… 您好,感谢您的评论!在这种情况下,使用哪个 HTTPS 版本是一个好主意,以及哪个 PayPal 端口是一个不错的选择?感谢您的宝贵时间! 除非他们在文档中明确告诉您需要使用特定的非标准端口,否则 HTTPS 应该只是 443。但同样,不要这样做通过fsockopen,这没什么意义。 感谢您的帮助。我会尝试用不同的方法进行测试。 通过使用不同的端口和不同的 HTTPS 链接,我让我的系统按预期工作,买家在购买后立即获得信用!谢谢大家的意见! 【参考方案1】:

对于PayPal收到200的情况,IPN已经发送成功,所以需要调试你的代码。将每个步骤记录到文件中,使用模拟器或沙箱模式事务测试 IPN(检查 test_ipn=1,使用它来确定是否回发到“沙箱”端点,如果测试 IPN 没有,请不要中断'不验证 - 只是记录它) - 从而使用您的日志输出,找出挂断的位置,以成功处理您的业务逻辑。

【讨论】:

以上是关于需要 Paypal IPN 侦听器帮助的主要内容,如果未能解决你的问题,请参考以下文章

Paypal:IPN 侦听器未收到 IPN 消息

Django-Paypal IPN 403 错误

修改 PayPal IPN URL 后,某些通知仍然使用旧的 IPN 侦听器 URL

在 PayPal Sandbox 中没有收到对侦听器的 IPN 响应

IPN 验证返回 INVALID,因为 PayPal 在 POST 中向 IPN 侦听器发送了不正确的字符集

Paypal IPN 沙盒 - IPN 侦听器 - 未验证或无效