Skip to content

Commit

Permalink
Fix exchange rate issue with automated invoicing in qbo (#1325)
Browse files Browse the repository at this point in the history
* Update packages to fix local QBO.

* Manually GET the exchange rate and add it to the payload.
  • Loading branch information
renintw committed May 27, 2024
1 parent 7bcb5f4 commit 24557ae
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 33 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@
"wpackagist-theme/twentyten": "*",
"wpackagist-theme/twentytwentytwo": "*",
"wpackagist-theme/twentytwentythree": "*",
"wpackagist-theme/twentytwentyfour": "*"
"wpackagist-theme/twentytwentyfour": "*",
"quickbooks/v3-php-sdk": "*"
},
"scripts": {
"format": "phpcbf -p",
Expand Down
121 changes: 90 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,46 @@ protected static function build_qbo_create_invoice_request( int $invoice_id ) {
* for the first time, so we don't need any code to automatically activate them.
*/
if ( 'USD' != $currency_code ) {
$payload['CurrencyRef'] = array(
/*
* Fetch currency exchange rates from QBO.
* No caching implemented since QBO updates rates every 4 hours and API calls are only triggered on invoice approval.
*/
$response = wp_remote_get(
sprintf(
'%s/v3/company/%d/exchangerate?sourcecurrencycode=%s',
self::$api_base_url,
rawurlencode( $realm_id ),
$currency_code,
),
array(
'timeout' => self::REMOTE_REQUEST_TIMEOUT,
'headers' => array(
'Authorization' => $oauth_header,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
)
);
Logger\log( 'remote_request', compact( 'currency_code', 'response' ) );

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(
'value' => $currency_code,
);
$payload['ExchangeRate'] = $exchange_rate;
}

$request_url = sprintf(
Expand Down

0 comments on commit 24557ae

Please sign in to comment.