From 412dfbb7cfef0f9a8dfd0353fd62e7599c8f01c8 Mon Sep 17 00:00:00 2001 From: emilicastells Date: Wed, 21 Jul 2021 15:20:35 +0200 Subject: [PATCH] Do not send decimals when currency does not support them --- modules/ppcp-api-client/src/Entity/class-amount.php | 11 ++++++++++- modules/ppcp-api-client/src/Entity/class-money.php | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-api-client/src/Entity/class-amount.php b/modules/ppcp-api-client/src/Entity/class-amount.php index 4867c230b..27ec10581 100644 --- a/modules/ppcp-api-client/src/Entity/class-amount.php +++ b/modules/ppcp-api-client/src/Entity/class-amount.php @@ -28,6 +28,13 @@ class Amount { */ private $breakdown; + /** + * Currencies that does not support decimals. + * + * @var array + */ + private $currencies_without_decimals = array( 'HUF', 'JPY', 'TWD' ); + /** * Amount constructor. * @@ -74,7 +81,9 @@ class Amount { public function to_array(): array { $amount = array( 'currency_code' => $this->currency_code(), - 'value' => number_format( $this->value(), 2, '.', '' ), + 'value' => in_array( $this->currency_code(), $this->currencies_without_decimals, true ) + ? round( $this->value(), 0 ) + : number_format( $this->value(), 2, '.', '' ), ); if ( $this->breakdown() && count( $this->breakdown()->to_array() ) ) { $amount['breakdown'] = $this->breakdown()->to_array(); diff --git a/modules/ppcp-api-client/src/Entity/class-money.php b/modules/ppcp-api-client/src/Entity/class-money.php index d8fc8ad79..f1f3f848f 100644 --- a/modules/ppcp-api-client/src/Entity/class-money.php +++ b/modules/ppcp-api-client/src/Entity/class-money.php @@ -28,6 +28,13 @@ class Money { */ private $value; + /** + * Currencies that does not support decimals. + * + * @var array + */ + private $currencies_without_decimals = array( 'HUF', 'JPY', 'TWD' ); + /** * Money constructor. * @@ -65,7 +72,9 @@ class Money { public function to_array(): array { return array( 'currency_code' => $this->currency_code(), - 'value' => number_format( $this->value(), 2, '.', '' ), + 'value' => in_array( $this->currency_code(), $this->currencies_without_decimals, true ) + ? round( $this->value(), 0 ) + : number_format( $this->value(), 2, '.', '' ), ); } }