【问题标题】:PHP Paypal Auth/Capture NVP Integration TroublesPHP Paypal Auth/Capture NVP 集成问题
【发布时间】:2016-12-18 15:08:21
【问题描述】:

背景:

我们使用 NVP 集成和 php-curl 实现了 Paypal 授权和捕获流程。
完整的流程在 PayPal 开发者网站上有描述:https://developer.paypal.com/webapps/developer/docs/classic/express-checkout/ht_ec-singleAuthPayment-curl-etc/

在我们的网站上,目前的支付场景是:
- 首先,用户单击按钮启动支付授权,将他重定向到 PayPal 网站(SetExpressCheckout with paymentaction=Authorization)
- 如果用户在 PayPal 网站上成功确认付款,他会在特定的成功页面上重定向到我们的网站
- 此“成功页面”从 PayPal 网站获取 tokenPayerID,然后我们调用 GetExpressCheckoutDetails 来检查此授权的状态和金额
- 如果一切正常,我们会告诉 PayPal 确认此授权(DoExpressCheckoutPayment with paymentaction=Authorization),我们会获得一个授权 ID 以存储到我们的数据库中
- 稍后,其他人可以使用我们存储的授权 ID(DoCapture)通过单击按钮来结算交易

附加信息:

根据 PayPal 文档:

PayPal 在三天内兑现 100% 的授权资金
买家和商家的账户不能关闭,如果有 待定(未解决)授权
https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/authcapture/

在我们的网站上,如果未在 24 小时内解决,授权将自动失效。 (使用 crontab)

问题:

最后一部分出现问题(当我们调用“确认”函数时):当用户点击“确认”按钮时,似乎有时 curl 请求需要时间从 PayPal 获取交易 ID .
发生这种情况时,用户通常会关闭网页,PayPal 确认授权(并因此转账)但我们的网站没有收到通知,因为以下代码(来自下面的“源代码”部分)尚未执行或达到:

if ($transaction_id) {
    /*
     * [...]
     * Everything is ok, payment has been performed
     * so we do everything to give our user what he asked for
     */
} else {
    // Error : No transaction id
}

因为脚本在得到 curl 响应之前就停止了。
此外,如果我们再次尝试单击该按钮,PayPal 会告诉我们授权 ID 不存在(因为已执行)。

但有时一切都运行良好,没有任何问题或滞后。

源代码:

/*
 * This is our main function, called when
 * we have to settle our transaction 
 * when an user click on a "confirm" button
**/
public function confirm($cart_id) {
    /*
     * [...]
     * We check lot of stuff to be sure this user 
     * can perform this action
     */

    // We get theses values from the database
    authorization_id = "lorem ipsum";
    $amount = 10; 

    // We tell PayPal to settle the transaction
    $transaction_id = $this->settle_transaction($authorization_id, $amount);
    if ($transaction_id) {
        /*
         * [...]
         * Everything is ok, payment has been performed
         * so we do everything to give our user what he asked for
         */
    } else {
        // Error : No transaction id
    }
}

private function settle_transaction($authorization_id, $amount) {
    // Our credentials
    $params = array(
        "USER" => $this->paypal_user,
        "PWD" => $this->paypal_pwd,
        "SIGNATURE" => $this->paypal_signature,
        "VERSION" => 95
    );
    $params["METHOD"] = "DoCapture";
    $params["AUTHORIZATIONID"] = $authorization_id;
    $params["AMT"] = $amount;
    $params["CURRENCYCODE"] = "EUR";
    $params["COMPLETETYPE"] = "Complete";

    $result = $this->curl($params);
    if ($result) {
        // We check that this PayPal request has been successful
        if ($result["ACK"] == "Success") {
            $transaction_id = $result["TRANSACTIONID"];
            if ($result["PAYMENTSTATUS"] == "Completed") {
                return $transaction_id;
            }
        }
    }
    return NULL;
}


private function curl($params) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->paypal_endpoint);
    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    parse_str(curl_exec($ch), $result);
    curl_close($ch);
    return $result;
}

你有什么办法解决这个问题吗?
我正在考虑在脚本结尾处结算交易,因为 PayPal 将 100% 的授权资金兑现三天,我只需要将它们保留 1 天,但无论如何我都不确定......

编辑 1:

当这个问题发生时,我的 apache2 error.log 报告了这个:

[Mon Aug 08 20:42:55.959330 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:42:56.960453 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:42:57.961188 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:42:58.962230 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:42:59.963297 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:00.964384 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:01.965476 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:02.966478 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:03.967595 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:04.968713 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:05.969783 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:06.970877 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:07.972002 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:08.972749 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:09.973847 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:10.974926 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:11.976080 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:12.977168 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:13.978244 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:14.979320 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:15.980414 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:16.981493 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:17.982578 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:18.983673 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:19.984762 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:20.985841 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:21.986650 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:22.987725 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:23.988826 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:24.989939 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:25.991061 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:26.992181 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:27.993305 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:28.994422 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:29.995556 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:30.996661 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:31.997774 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:32.998905 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:34.000089 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:35.001202 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:36.002326 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:37.003424 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:38.004551 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:39.005677 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:40.006799 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:41.007902 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:42.009021 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:43.010132 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:44.011245 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:45.012361 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:46.013479 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:47.014577 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:48.015685 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:49.016801 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:50.017906 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:51.018980 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:52.020049 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:53.021158 2016] [mpm_event:error] [pid 141:tid 3779940374400] AH00485: scoreboard is full, not at MaxRequestWorkers
[Mon Aug 08 20:43:53.391316 2016] [:error] [pid 980:tid 3779386513152] (104)Connection reset by peer: [client MY-IP:55236] FastCGI: failed to read from backend server, referer: http://####
[Mon Aug 08 21:18:04.748237 2016] [:error] [pid 1287:tid 3779782977280] (104)Connection reset by peer: [client MY-IP:37196] FastCGI: failed to read from backend server

编辑 2:

我发现this topic 似乎有类似的问题:

特别奇怪的是付款被正确处理了。

现在我似乎无法重现此错误。
您认为这可能是 PayPal 问题或类似问题吗?
即使是这样,我也不想确保这个问题不会再次发生,但是如果我不能重现这个问题,我该如何测试呢?

【问题讨论】:

    标签: php curl paypal payment express-checkout


    【解决方案1】:

    你需要了解ignore_user_abort(true);(可能还有set_time_limit(0);),用它来避免脚本在代码中途退出的问题。其次,我可以建议一个最近确认的令牌数据库,在 curl 调用之前更新,这样如果用户退出,然后再次尝试按“确认”,你就会知道它已经是一个确认的令牌,并且不要重新- 运行 curl 代码,并可以立即通知用户? -- http://php.net/manual/en/function.ignore-user-abort.php

    • 并且警告,某些共享主机提供不允许允许在运行时修改 ignore_user_abort / set_time_limit

    【讨论】:

    • 感谢您的帮助,我无法尝试您的建议,因为我似乎无法再重现此问题。我在我的问题中添加了一些信息。
    【解决方案2】:

    注意:并非所有付款都是即时的。如果买方只有银行 与他们的 PayPal 账户关联的账户,转账不会 立即的。因此,如果想要自动通知所有付款和相关活动,最佳做法是使用IPN

    根据 PayPal 官方文档:

    Instant Payment Notification (IPN) 是一种消息服务,用于通知 与 PayPal 交易相关的事件。您可以使用 IPN 消息 自动化后台和管理功能,例如 履行订单、跟踪客户或提供状态和其他 交易相关信息。

    最佳做法是在您的 IPN Listener 中设置事务脚本。对于集成指南,您可以参考这里:https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNImplementation/

    我已经为 PayPal IPN Listener 延长了几个月的 PHP class。希望它可以作为起点有所帮助。随意分叉:https://github.com/datumradix/PayPal-IPN-PHP-Class-

    编辑:(PayPal Documentation is not clear at many places and seems confusing to many first time readers)

    IPN 可以作为辅助机制来确认 DoCapture 是否成功。 txn_typetxn_idauth_idauth_amountpayer_id 等 IPN 变量都通过 IPN 得到通知。请在此处参考完整列表:https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/

    注意:我们可以在每个调用中指定NOTIFYURL,也可以 从贝宝后端设置相同。有关设置相同的步骤 贝宝配置文件设置,参考 :https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNSetup/

    【讨论】:

    • 感谢您的回答,我查看了您的代码(以及官方 paypal 示例中的代码)。在第 99 行的“payment.php”中,我是否必须使用 IPN 回调检查 DoCapture 的结果?
    • 在这个link 上,他们说我们可以使用DoExpressCheckoutPayment 中的NOTIFYURL 来使用IPN。但是我的DoExpressCheckoutPayment是设置了paymentaction=Authorization稍后才能完成支付,所以我收到通知的那一刻并不是我要进行支付的那一刻。
    • NOTIFYURL 不能与DoCapture 一起使用,因为根据documentation 没有这样的字段。您对稍后执行DoCapture 的正确工作流程有任何想法吗?
    • 从您的问题来看,我认为您的问题是当有人成功执行DoCapture 时,您的网站有时不会收到通知。 IPN 可以作为辅助机制来确认DoCapture 是否成功。 txn_typetxn_idauth_idauth_amountpayer_id 等 IPN 变量都会通过 IPN 得到通知
    猜你喜欢
    • 2013-02-06
    • 2014-08-03
    • 2017-02-19
    • 1970-01-01
    • 2016-04-17
    • 2012-04-08
    • 2020-11-12
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多