[英]Simulate PayPal errors in a sandbox account
我正在將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
任何幫助表示贊賞。 非常感謝。
好的,發布此問題后,我實際上已經找到了解決該問題的方法。 我只是將解決方案放在這里,供將來可能遇到此問題的任何人使用。
我發布的期權對象實際上是正確的,因此,在用戶確認他/她想要開始新交易之后,我得到了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
的標頭,其中包含您要測試的錯誤代碼,僅此而已。
希望這會對別人有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.