好吧,我在发布这个问题后,实际上已经找到了解决这个问题的方法。 我只是将我的解决方案提供给将来可能遇到此问题的任何人。
我发布的选项对象实际上是正确的,所以在用户确认他/她想要开始一个新的交易后,我得到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 它包含您要测试的错误代码,就是这样。
PayPal-Mock-Response
希望这会帮助别人!