Skip to content

Commit 68ffd0e

Browse files
authored
Merge pull request #16 from gregjopa/negative-testing-improvements
Improve support for negative testing
2 parents 6c402a7 + 4020b07 commit 68ffd0e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@
8888
throw new Error(
8989
`${errorDetail.description} (${orderData.debug_id})`,
9090
);
91-
} else {
91+
} else if (!orderData.purchase_units) {
92+
throw new Error(JSON.stringify(orderData));
93+
}
94+
else {
9295
// (3) Successful transaction -> Show confirmation or thank you message
9396
// Or go to another URL: actions.redirect('thank_you.html');
9497
const transaction =

server.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ const createOrder = async (cart) => {
6666
headers: {
6767
'Content-Type': 'application/json',
6868
Authorization: `Bearer ${accessToken}`,
69+
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
70+
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
71+
// "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
72+
// "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
73+
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
6974
},
7075
method: 'POST',
7176
body: JSON.stringify(payload),
@@ -91,22 +96,24 @@ const captureOrder = async (orderID) => {
9196
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
9297
// "PayPal-Mock-Response": '{"mock_application_codes": "INSTRUMENT_DECLINED"}'
9398
// "PayPal-Mock-Response": '{"mock_application_codes": "TRANSACTION_REFUSED"}'
99+
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
94100
},
95101
});
96102

97103
return handleResponse(response);
98104
};
99105

100106
async function handleResponse(response) {
101-
if (response.status === 500 || response.status === 503) {
107+
try {
108+
const jsonResponse = await response.json();
109+
return {
110+
jsonResponse,
111+
httpStatusCode: response.status,
112+
};
113+
} catch (err) {
102114
const errorMessage = await response.text();
103115
throw new Error(errorMessage);
104116
}
105-
106-
return {
107-
jsonResponse: await response.json(),
108-
httpStatusCode: response.status,
109-
};
110117
}
111118

112119
app.post('/api/orders', async (req, res) => {

0 commit comments

Comments
 (0)