@@ -66,6 +66,11 @@ const createOrder = async (cart) => {
66
66
headers : {
67
67
'Content-Type' : 'application/json' ,
68
68
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"}'
69
74
} ,
70
75
method : 'POST' ,
71
76
body : JSON . stringify ( payload ) ,
@@ -91,22 +96,24 @@ const captureOrder = async (orderID) => {
91
96
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
92
97
// "PayPal-Mock-Response": '{"mock_application_codes": "INSTRUMENT_DECLINED"}'
93
98
// "PayPal-Mock-Response": '{"mock_application_codes": "TRANSACTION_REFUSED"}'
99
+ // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
94
100
} ,
95
101
} ) ;
96
102
97
103
return handleResponse ( response ) ;
98
104
} ;
99
105
100
106
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 ) {
102
114
const errorMessage = await response . text ( ) ;
103
115
throw new Error ( errorMessage ) ;
104
116
}
105
-
106
- return {
107
- jsonResponse : await response . json ( ) ,
108
- httpStatusCode : response . status ,
109
- } ;
110
117
}
111
118
112
119
app . post ( '/api/orders' , async ( req , res ) => {
0 commit comments