我正在将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": "[email protected]",
"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);
}
);
在请求标头中,您只需要放置一个名为PayPal-Mock-Response
的标头,其中包含您要测试的错误代码,就是这样。
希望这会帮助别人!