Skip to content

Commit d498299

Browse files
authored
Improve Stripe API Connector logging (#4486)
* Add default log context
1 parent 75c9245 commit d498299

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Update - Improve Stripe API connector logging to include request/response context
1919
* Fix - Fix fatal when processing setup intents for free subscriptions via webhooks
2020
* Fix - Prevent Stripe API calls after several consecutive 401 (Unauthorized) responses
21+
* Update - Improve Stripe API connector logging to include request/response context
2122

2223
= 9.7.0 - 2025-07-21 =
2324
* Update - Removes BNPL payment methods (Klarna and Affirm) when other official plugins are active

includes/class-wc-stripe-api.php

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public static function request( $request, $api = 'charges', $method = 'POST', $w
236236
$request = apply_filters( 'wc_stripe_request_body', $request, $api );
237237

238238
// 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 ] );
240240

241241
$response = wp_safe_remote_post(
242242
self::ENDPOINT . $api,
@@ -249,34 +249,33 @@ public static function request( $request, $api = 'charges', $method = 'POST', $w
249249
);
250250

251251
$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-
}
256252

257253
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+
]
267262
);
268263

269264
throw new WC_Stripe_Exception( print_r( $response, true ), __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
270265
}
271266

267+
$response_body = json_decode( $response['body'] );
268+
269+
WC_Stripe_Logger::debug( "Stripe API response: {$method} {$api}", [ 'response' => $response_body ] );
270+
272271
if ( $with_headers ) {
273272
return [
274273
'headers' => $response_headers,
275-
'body' => json_decode( $response['body'] ),
274+
'body' => $response_body,
276275
];
277276
}
278277

279-
return json_decode( $response['body'] );
278+
return $response_body;
280279
}
281280

282281
/**
@@ -301,7 +300,7 @@ public static function retrieve( $api ) {
301300
return null;
302301
}
303302

304-
WC_Stripe_Logger::log( "{$api}" );
303+
WC_Stripe_Logger::debug( "Stripe API request: GET {$api}" );
305304

306305
$response = wp_safe_remote_get(
307306
self::ENDPOINT . $api,
@@ -315,7 +314,12 @@ public static function retrieve( $api ) {
315314
// If we get a 401 error, we know the secret key is not valid.
316315
if ( is_array( $response ) && isset( $response['response'] ) && is_array( $response['response'] ) && isset( $response['response']['code'] ) && 401 === $response['response']['code'] ) {
317316
// 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+
);
319323

320324
++$invalid_api_key_error_count;
321325
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 ) {
343347
}
344348

345349
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+
);
347356
return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
348357
}
349358

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;
351364
}
352365

353366
/**
@@ -421,15 +434,14 @@ public static function request_with_level3_data( $request, $api, $level3_data, $
421434
set_transient( 'wc_stripe_level3_not_allowed', true, 3 * MONTH_IN_SECONDS );
422435
} elseif ( $is_level_3data_incorrect ) {
423436
// 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+
]
433445
);
434446
}
435447

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
127127
* Fix - Fix required field error message and PHP warning for custom checkout fields that don't have a label
128128
* Fix - Fix fatal when processing Boleto setup intents via webhooks
129129
* Fix - Prevent Stripe API calls after several consecutive 401 (Unauthorized) responses
130+
* Update - Improve Stripe API connector logging to include request/response context
130131

131132
[See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

0 commit comments

Comments
 (0)