@@ -236,7 +236,7 @@ public static function request( $request, $api = 'charges', $method = 'POST', $w
236
236
$ request = apply_filters ( 'wc_stripe_request_body ' , $ request , $ api );
237
237
238
238
// Log the request after the filters have been applied.
239
- WC_Stripe_Logger::log ( "{ $ api } request: " . print_r ( $ request, true ) );
239
+ WC_Stripe_Logger::debug ( "Stripe API request: { $ method } { $ api }" , [ ' request ' => $ request ] );
240
240
241
241
$ response = wp_safe_remote_post (
242
242
self ::ENDPOINT . $ api ,
@@ -249,34 +249,33 @@ public static function request( $request, $api = 'charges', $method = 'POST', $w
249
249
);
250
250
251
251
$ response_headers = wp_remote_retrieve_headers ( $ response );
252
- // Log the stripe version in the response headers, if present.
253
- if ( isset ( $ response_headers ['stripe-version ' ] ) ) {
254
- WC_Stripe_Logger::log ( "{$ api } response with stripe-version: " . $ response_headers ['stripe-version ' ] );
255
- }
256
252
257
253
if ( is_wp_error ( $ response ) || empty ( $ response ['body ' ] ) ) {
258
- WC_Stripe_Logger::log (
259
- 'Error Response: ' . print_r ( $ response , true ) . PHP_EOL . PHP_EOL . 'Failed request: ' . print_r (
260
- [
261
- 'api ' => $ api ,
262
- 'request ' => $ request ,
263
- 'idempotency_key ' => $ idempotency_key ,
264
- ],
265
- true
266
- )
254
+ // Stripe redacts API keys in the response.
255
+ WC_Stripe_Logger::error (
256
+ "Stripe API error: {$ method } {$ api }" ,
257
+ [
258
+ 'request ' => $ request ,
259
+ 'idempotency_key ' => $ idempotency_key ,
260
+ 'response ' => $ response ,
261
+ ]
267
262
);
268
263
269
264
throw new WC_Stripe_Exception ( print_r ( $ response , true ), __ ( 'There was a problem connecting to the Stripe API endpoint. ' , 'woocommerce-gateway-stripe ' ) );
270
265
}
271
266
267
+ $ response_body = json_decode ( $ response ['body ' ] );
268
+
269
+ WC_Stripe_Logger::debug ( "Stripe API response: {$ method } {$ api }" , [ 'response ' => $ response_body ] );
270
+
272
271
if ( $ with_headers ) {
273
272
return [
274
273
'headers ' => $ response_headers ,
275
- 'body ' => json_decode ( $ response [ ' body ' ] ) ,
274
+ 'body ' => $ response_body ,
276
275
];
277
276
}
278
277
279
- return json_decode ( $ response [ ' body ' ] ) ;
278
+ return $ response_body ;
280
279
}
281
280
282
281
/**
@@ -301,7 +300,7 @@ public static function retrieve( $api ) {
301
300
return null ;
302
301
}
303
302
304
- WC_Stripe_Logger::log ( "{$ api }" );
303
+ WC_Stripe_Logger::debug ( "Stripe API request: GET {$ api }" );
305
304
306
305
$ response = wp_safe_remote_get (
307
306
self ::ENDPOINT . $ api ,
@@ -315,7 +314,12 @@ public static function retrieve( $api ) {
315
314
// If we get a 401 error, we know the secret key is not valid.
316
315
if ( is_array ( $ response ) && isset ( $ response ['response ' ] ) && is_array ( $ response ['response ' ] ) && isset ( $ response ['response ' ]['code ' ] ) && 401 === $ response ['response ' ]['code ' ] ) {
317
316
// Stripe redacts API keys in the response.
318
- WC_Stripe_Logger::log ( "Error: GET {$ api } returned a 401 " );
317
+ WC_Stripe_Logger::error (
318
+ "Stripe API error: GET {$ api } returned a 401 " ,
319
+ [
320
+ 'response ' => json_decode ( $ response ['body ' ] ),
321
+ ]
322
+ );
319
323
320
324
++$ invalid_api_key_error_count ;
321
325
WC_Stripe_Database_Cache::set ( self ::INVALID_API_KEY_ERROR_COUNT_CACHE_KEY , $ invalid_api_key_error_count , self ::INVALID_API_KEY_ERROR_COUNT_CACHE_TIMEOUT );
@@ -343,11 +347,20 @@ public static function retrieve( $api ) {
343
347
}
344
348
345
349
if ( is_wp_error ( $ response ) || empty ( $ response ['body ' ] ) ) {
346
- WC_Stripe_Logger::log ( 'Error Response: ' . print_r ( $ response , true ) );
350
+ WC_Stripe_Logger::error (
351
+ "Stripe API error: GET {$ api }" ,
352
+ [
353
+ 'response ' => $ response ,
354
+ ]
355
+ );
347
356
return new WP_Error ( 'stripe_error ' , __ ( 'There was a problem connecting to the Stripe API endpoint. ' , 'woocommerce-gateway-stripe ' ) );
348
357
}
349
358
350
- return json_decode ( $ response ['body ' ] );
359
+ $ response_body = json_decode ( $ response ['body ' ] );
360
+
361
+ WC_Stripe_Logger::debug ( "Stripe API response: GET {$ api }" , [ 'response ' => $ response_body ] );
362
+
363
+ return $ response_body ;
351
364
}
352
365
353
366
/**
@@ -421,15 +434,14 @@ public static function request_with_level3_data( $request, $api, $level3_data, $
421
434
set_transient ( 'wc_stripe_level3_not_allowed ' , true , 3 * MONTH_IN_SECONDS );
422
435
} elseif ( $ is_level_3data_incorrect ) {
423
436
// Log the issue so we could debug it.
424
- WC_Stripe_Logger::log (
425
- 'Level3 data sum incorrect: ' . PHP_EOL
426
- . print_r ( $ result ->error ->message , true ) . PHP_EOL
427
- . print_r ( 'Order line items: ' , true ) . PHP_EOL
428
- . print_r ( $ order ->get_items (), true ) . PHP_EOL
429
- . print_r ( 'Order shipping amount: ' , true ) . PHP_EOL
430
- . print_r ( $ order ->get_shipping_total (), true ) . PHP_EOL
431
- . print_r ( 'Order currency: ' , true ) . PHP_EOL
432
- . print_r ( $ order ->get_currency (), true )
437
+ WC_Stripe_Logger::error (
438
+ 'Level3 data sum incorrect ' ,
439
+ [
440
+ 'error ' => $ result ->error ,
441
+ 'order_line_items ' => $ order ->get_items (),
442
+ 'order_shipping_amount ' => $ order ->get_shipping_total (),
443
+ 'order_currency ' => $ order ->get_currency (),
444
+ ]
433
445
);
434
446
}
435
447
0 commit comments