javascript - 在沙盒帐户中模拟 PayPal 错误

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

我正在将 PayPal 支付集成到我正在开发的 Web 应用程序中。我需要为一笔交易创建授权,在其中锁定一定金额(假设是 20 欧元),然后在交易结束时我完成交易,并且只拿走我需要拿走的钱(因此,如果交易是最终成本是 15 欧元,我回馈给用户 5 欧元)。

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

我找到了此文档 ( https://developer.paypal.com/docs/api/test-values/#invoke-negative-testing ),其中指出要触发 SENDER_EMAIL_UNCONFIRMED 模拟响应,请在 POST v1/payments/payouts 调用中将 items[0]/note 值设置为 ERRPYO002 。 使用以下代码:

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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f5e4fceaf0f1f6a8f6ece8f0e9e4f1eaf7a8f7e0e6e0ecf3e0f7c5f5e4fcf5e4e9abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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

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

最佳答案

好吧,我实际上在发布这个问题后就找到了如何解决这个问题。 我将把我的解决方案放在这里,供将来可能遇到此问题的任何人使用。

我发布的选项对象实际上是正确的,就像现在一样,因此在用户确认他/她想要开始新交易后,我将 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);
    }
);

在请求 header 中,您只需放置一个名为 PayPal-Mock-Response 的 header ,其中包含您要测试的错误代码,仅此而已。

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

关于javascript - 在沙盒帐户中模拟 PayPal 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54882885/

相关文章:

javascript - zombie.js 和 socket.io (node.js)

javascript - isNaN 可以用作命名函数表达式中的标识符吗?

curl - 使用 cURL 执行时 Paypal Invoice api 出现无效 token 错误

asp.net - PayPal API,账户验证

此商家已禁用 PayPal DPRP

Paypal - 沙盒 - 创建的测试账户余额为零

javascript - 是否有用于对 JavaScript 和 HTML 进行单元测试的框架

javascript - 如何在主页中保存模态弹出窗口值

paypal - 如何判断我的账户是否激活了 Paypal Web Payments Pro?

android - 请求失败,服务器响应 DUPLICATE_REQUEST_ID