【问题标题】:PayPal API Not Working All The Time AnymorePayPal API 不再一直工作
【发布时间】:2021-01-14 20:15:55
【问题描述】:

我们的大多数 API 调用都能成功运行并存储到我们的数据库中,但也有少数没有。我收集了一些日志,我们收到了“错误 400 错误请求”。

--

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:HTTP/1.1 400 错误请求

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:连接:关闭

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:内容长度:11

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:内容类型:文本/纯文本;字符集=utf-8

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:

[2020 年 9 月 29 日 08:04:48 UTC] 未验证错误 101:错误请求

--

日志:https://pastebin.com/hRjrgWGd

代码:

    $header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "HOST: www.paypal.com:443\r\n";
$header .= "Connection close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

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

if (!$fp)
{
    // HTTP ERROR
    echo "error";
    error_log("FAILED line 53");

} else {
    // NO HTTP ERROR

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


    while (!feof($fp))
    {
        $res = fgets($fp, 1024);

        if (strcmp (trim($res), "VERIFIED") == 0) {
            //Setup Required Variables
            $txn_id = $_POST['txn_id'];
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $cust_name = $first_name .' '. $last_name;
            $cust_email = $_POST['payer_email'];
            $cust_id = $_POST['auction_buyer_id'];
            $cust_status = $_POST['payer_status'];
            $payment_date = $_POST['payment_date'];
            $currency = $_POST['mc_currency'];

            $address_street = $_POST['address_street'];
            $address_city = $_POST['address_city'];
            $address_state = $_POST['address_state'];
            $address_zip = $_POST['address_zip'];
            $address_country = $_POST['address_country'];
            $address_status = $_POST['address_status'];

            $g_ref = "";

            if (isset($_POST['fulfillment_order_reference_number'])) {
                $g_ref = $_POST['fulfillment_order_reference_number'];
                $address_street = $_POST['fulfillment_address_line1'];
                $address_city = $_POST['fulfillment_address_city'];
                $address_state= $_POST['fulfillment_address_state'];
                $address_zip = $_POST['fulfillment_address_zip'];
            }

            $item_count = $_POST['num_cart_items'];

            //Misc Variables Requried for Authentication
            $payment_status = $_POST['payment_status'];
            $receiver_email = $_POST['receiver_email'];
            $our_email = '###'; #PP EMAIL HERE
            $our_email_2 = '###'; #PP EMAIL HERE

            if (($payment_status == 'Completed') && (($receiver_email == $our_email) || ($receiver_email == $our_email_2)))
            {
                //Check Transaction Not Already Processed
                //  $check = $db_query->duplicate_check($txn_id);
                //  if ($check != 0) { exit(); }

                //Distinguish between accounts
                if ($receiver_email == $our_email) {
                    $pp_acc = "CB";
                } else if ($receiver_email == $our_email_2) {
                    $pp_acc = "IK";
                }

                for ($n=1;$n<=$item_count;$n++) {
                    $item_name = $_POST['item_name'. $n];
                    $item_quantity = $_POST['quantity'. $n];
                    $item_number = $_POST['item_number'. $n];
                    $total_paid = $_POST['mc_gross_'. $n];
                    error_log("HIT ADD ORDER tx: $txn_id");

                    //Add Order to DB (Removed)
                }
            }
        }
        else if (strcmp ($res, "INVALID") == 0) {
            error_log("Invalid Error 102");

        }
        else {
            // This is where it seems to be hitting
            error_log("not verified Error 101: " . $res);
        }
    }
}

fclose ($fp);

似乎 feof($fp) 读取到请求数据的末尾,但未保存到数据库的那些没有命中已验证的“if”语句,而是“else”。

我检查了 PayPal IPN 历史记录,所有请求都已成功发送,所以这肯定是我们的服务器问题。这在 2016 年到 2018 年都不是问题,但从那时起我们就有一些没有保存到我们的数据库中。

有什么想法吗?

【问题讨论】:

    标签: php api paypal


    【解决方案1】:

    IPN Integration Guide 中所述,请确保在请求标头中包含User-Agent: 字段,其值是任何超过7 个字符左右的字符串

    您还应该使用到 ipnpb.paypal.com 的 HTTPS/SSL 连接(标准端口 443),不确定为什么您的代码使用旧的 www.paypal.com 端点作为 IPN。

    【讨论】:

    • 好的,那我该如何改变呢? $header .= "主机:www.paypal.com:443\r\n"; $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
    • 显然你需要在两个地方更改www,并且还要单独添加新的user-agent header
    猜你喜欢
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2017-08-09
    • 2015-01-23
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多