Skip to content

Commit

Permalink
Add error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw committed May 24, 2024
1 parent 8181f66 commit b500574
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,25 @@ protected static function build_qbo_create_invoice_request( int $invoice_id ) {
),
)
);
Logger\log( 'remote_request', compact( 'currency_code', 'response' ) );

$body = json_decode( wp_remote_retrieve_body( $response ), true );
$exchange_rate = $body['ExchangeRate']['Rate'];
if ( is_wp_error( $response ) ) {
return $response;
} elseif ( 200 != wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'invalid_http_code', 'Invalid HTTP response code', $response );
} else {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $body['ExchangeRate']['Rate'] ) ) {
$exchange_rate = $body['ExchangeRate']['Rate'];
} else {
return new WP_Error( 'error', 'Could not extract exchange rate from the response.', $body );
}
}

$payload['CurrencyRef'] = array(
$payload['CurrencyRef'] = array(
'value' => $currency_code,
);

$payload['ExchangeRate'] = $exchange_rate;
}

Expand Down

0 comments on commit b500574

Please sign in to comment.