| @@ -542,12 +542,29 @@ | ||
| 542 | 542 | if ( $status < 300 && $status > 199 ) { |
| 543 | 543 | return $body; |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | - // Rate limiting, temporary error | |
| 547 | - if ( $status === 429 ) { | |
| 548 | - $error_msg = __( "We hit Vipps/MobilePay's rate limit, we will retry later.", 'woo-vipps' ); | |
| 549 | - throw new WC_Vipps_Recurring_Temporary_Exception( $error_msg ); | |
| 546 | + // A transport error or a retryable HTTP response does not tell us whether a | |
| 547 | + // state-changing request reached Vipps/MobilePay. Treat it as temporary so | |
| 548 | + // callers can retry with the same idempotency key instead of failing the order. | |
| 549 | + if ( $status === 0 || in_array( $status, [ 408, 425, 429 ], true ) || $status >= 500 ) { | |
| 550 | + $error_msg = trim( (string) $default_error ); | |
| 551 | + if ( ! $error_msg ) { | |
| 552 | + $error_msg = sprintf( 'HTTP %d', $status ); | |
| 553 | + } | |
| 554 | + | |
| 555 | + $localized_msg = __( 'Vipps/MobilePay is temporarily unavailable. Please try again.', 'woo-vipps' ); | |
| 556 | + if ( $status === 429 ) { | |
| 557 | + $localized_msg = __( "We hit Vipps/MobilePay's rate limit. Please try again shortly.", 'woo-vipps' ); | |
| 558 | + } | |
| 559 | + | |
| 560 | + $log_body = is_array( $body ) ? json_encode( $body ) : $body; | |
| 561 | + WC_Vipps_Recurring_Logger::log( sprintf( 'HTTP Response Temporary Error (%s): %s (%s) with request body: %s. The response was: %s', $status, $error_msg, $endpoint, $request_body, $log_body ) ); | |
| 562 | + | |
| 563 | + $exception = new WC_Vipps_Recurring_Temporary_Exception( $error_msg, $localized_msg ); | |
| 564 | + $exception->response_code = $status; | |
| 565 | + | |
| 566 | + throw $exception; | |
| 550 | 567 | } |
| 551 | 568 | |
| 552 | 569 | // error handling |
| 553 | 570 | $error_msg = $default_error ?? ''; |