【问题标题】:Simulate PayPal errors in a sandbox account在沙盒帐户中模拟 PayPal 错误
【发布时间】:2019-07-19 20:12:01
【问题描述】:

我正在将 PayPal 支付集成到我正在开发的 Web 应用程序中。我需要为一笔交易创建授权,其中我锁定了一笔钱(比如说 20 欧元),然后在交易结束时我完成了交易,我只拿了我需要拿的钱(所以如果交易最终成本为 15 欧元,我将 5 欧元返还给用户)。

此工作流目前在沙盒帐户上运行,但现在我想测试在开始新交易时可能发生的一些错误,例如当用户没有足够的钱(20 欧元)时我需要锁定才能开始新的交易。

我找到了这个文档 (https://developer.paypal.com/docs/api/test-values/#invoke-negative-testing),其中声明了 To trigger the SENDER_EMAIL_UNCONFIRMED simulation response, set the items[0]/note value to ERRPYO002 in the POST v1/payments/payouts call.,代码如下:

curl -X POST https://api.sandbox.paypal.com/v1/payments/payouts \
  -H "content-type: application/json" \
  -H "Authorization: Bearer Access-Token" \
  -d '{
  "sender_batch_header": {
    "sender_batch_id": "1524086406556",
    "email_subject": "This email is related to simulation"
  },
  "items": [
  {
    "recipient_type": "EMAIL",
    "receiver": "payouts-simulator-receiver@paypal.com",
    "note": "ERRPYO002",
    "sender_item_id": "15240864065560",
    "amount": {
      "currency": "USD",
      "value": "1.00"
    }
  }]
}'

所以我想我需要将错误代码(如 ERRPYO002)传递给我的请求正文中的 note 字段。

我正在使用 checkout sdk,我的 js 代码目前如下所示:

const buttonOpts = {
    env: 'sandbox',
    client: { production: $scope.key, sandbox: $scope.key },
    style: {
        label: 'paypal',
        size: 'medium',
        shape: 'rect',
        color: 'blue',
        tagline: false,
    },

    validate: actions => {
        // stuff
    },
    payment: (data, actions) => {
        return actions.payment.create({
            intent: 'authorize',
            payer: { payment_method: 'paypal' },
            transactions: [
                {
                    amount: {
                        total: '20.00',
                        currency: 'EUR',
                    },
                    description: 'My description',
                },
            ],
        });
    },
    onAuthorize: data => {
        // Sending data.paymentID and data.payerID to my backend to confirm the new transaction
    },
    onCancel: () => {
        // stuff
    },
    onError: err => {
        console.log(err);
        // stuff
    },
};

Paypal.Button.render(buttonOpts, '#paypal-button');

我想我需要将模拟错误所需的代码传递给我的 actions.payment.create 对象参数,但我没有找到确切的位置,因为我的工作流程与文档中的工作流程不同。

以下是 PayPal 允许您用于错误测试的代码: https://developer.paypal.com/docs/payouts/integrate/test-payouts/#test-values

感谢任何帮助。 非常感谢。

【问题讨论】:

    标签: javascript paypal paypal-sandbox paypal-rest-sdk


    【解决方案1】:

    好的,在我发布这个问题后,我实际上已经找到了解决这个问题的方法。 我只是将我的解决方案放在这里,以供将来可能遇到此问题的任何人使用。

    我发布的选项对象实际上是正确的,因为它现在是正确的,所以在用户确认他/她想要开始新交易后,我会获取 payerID 和 paymentID 以发送到我的后端。 在我的后端函数中,我更改了代码,如下所示:

    const paypal = require('paypal-rest-sdk');
    const paymentId = event.paymentID;
    const payerId = { payer_id: event.payerID };
    paypal.configure({
        mode: process.env.PAYPAL_ENVIRONMENT, //sandbox or live
        client_id: '<MY_CLIENT_ID>',
        client_secret: '<MY_CLIENT_SECRET>',
    });
    
    paypal.payment.execute(
        paymentId,
        payerId,
        // START NEW CODE
        {
            headers: {
                'Content-Type': 'application/json',
                'PayPal-Mock-Response': '{"mock_application_codes": "INSUFFICIENT_FUNDS"}',
            },
        },
        // END NEW CODE
        (error, payment) => {
            console.error(JSON.stringify(error));
            console.error(JSON.stringify(payment));
            if (error) {
                /*
                    {
                        "response": {
                            "name": "INSUFFICIENT_FUNDS",
                            "message": "Buyer cannot pay - insufficient funds.",
                            "information_link": "https://developer.paypal.com/docs/api/payments/#errors",
                            "debug_id": "a1b2c3d4e5f6g",
                            "details": [
                                {
                                    "issue": "The buyer must add a valid funding instrument, such as a credit card or bank account, to their PayPal account."
                                }
                            ],
                            "httpStatusCode": 400
                        },
                        "httpStatusCode": 400
                    }
                */
                return callback('unhandled_error', null);
            }
            if (payment.state === 'approved' && payment.transactions && payment.transactions[0].related_resources && payment.transactions[0].related_resources[0].authorization) {
                return callback(null, payment.transactions[0].related_resources[0].authorization.id);
            }
            console.log('payment not successful');
            return callback('unhandled_error', null);
        }
    );
    

    在请求标头中,您只需放置一个名为 PayPal-Mock-Response 的标头,其中包含您要测试的错误代码,就是这样。

    希望这会对某人有所帮助!

    【讨论】:

    • 您是否使用请求测试值进行了否定测试? IE。 items[0]/note value 到 ERRPYO002。我已经尝试了几天,但我无法让它工作。
    • 没有错误。付款总是成功的。我正在使用 nodejs sdk。
    • 嗯,没有看到你的代码就很难帮助你......我不是 PayPal API 的专家(这是我的第一个集成)但如果你想在这里粘贴你的代码我可以看看吧。
    猜你喜欢
    • 2013-05-19
    • 2018-06-25
    • 2013-06-26
    • 2013-01-22
    • 2016-03-06
    • 2017-12-11
    • 2013-07-02
    • 2017-01-12
    • 2016-07-28
    相关资源
    最近更新 更多