From 0cdecee2d4d263c6816584de690a2f66c6dac6cb Mon Sep 17 00:00:00 2001 From: Joost Waaijer Date: Thu, 24 Jun 2021 08:33:25 +0200 Subject: [PATCH 1/4] round payment amount to 2 digits to fix rounding issue --- src/controllers/PaymentsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/PaymentsController.php b/src/controllers/PaymentsController.php index 5740c39114..0021bb447c 100644 --- a/src/controllers/PaymentsController.php +++ b/src/controllers/PaymentsController.php @@ -425,7 +425,7 @@ public function actionPay() $order->setPaymentAmount($paymentAmount); } - $paymentAmountInPrimaryCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency); + $paymentAmountInPrimaryCurrency = round(Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency), 2); if (!$partialAllowed && $paymentAmountInPrimaryCurrency < $order->getOutstandingBalance()) { $error = Craft::t('commerce', 'Partial payment not allowed.'); From d34254311c819dbb82c8f6a555c80b20bdb7ff21 Mon Sep 17 00:00:00 2001 From: Joost Waaijer Date: Thu, 24 Jun 2021 09:50:56 +0200 Subject: [PATCH 2/4] Keep paymentAmountInPrimaryCurrency as the original --- src/controllers/PaymentsController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/PaymentsController.php b/src/controllers/PaymentsController.php index 0021bb447c..356aa4644e 100644 --- a/src/controllers/PaymentsController.php +++ b/src/controllers/PaymentsController.php @@ -425,9 +425,9 @@ public function actionPay() $order->setPaymentAmount($paymentAmount); } - $paymentAmountInPrimaryCurrency = round(Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency), 2); + $paymentAmountInPrimaryCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency); - if (!$partialAllowed && $paymentAmountInPrimaryCurrency < $order->getOutstandingBalance()) { + if (!$partialAllowed && round($paymentAmountInPrimaryCurrency, 2) < $order->getOutstandingBalance()) { $error = Craft::t('commerce', 'Partial payment not allowed.'); $this->setFailFlash($error); Craft::$app->getUrlManager()->setRouteParams(['paymentForm' => $paymentForm, $this->_cartVariableName => $order]); From 0a863c3eaad52c2636680b3153ef26d70aa99ab5 Mon Sep 17 00:00:00 2001 From: Joost Waaijer Date: Thu, 24 Jun 2021 11:29:45 +0200 Subject: [PATCH 3/4] Use Currency helper --- src/controllers/PaymentsController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/PaymentsController.php b/src/controllers/PaymentsController.php index 356aa4644e..3739be8190 100644 --- a/src/controllers/PaymentsController.php +++ b/src/controllers/PaymentsController.php @@ -12,6 +12,7 @@ use craft\commerce\errors\CurrencyException; use craft\commerce\errors\PaymentException; use craft\commerce\errors\PaymentSourceException; +use craft\commerce\helpers\Currency; use craft\commerce\models\PaymentSource; use craft\commerce\models\Transaction; use craft\commerce\Plugin; @@ -427,7 +428,7 @@ public function actionPay() $paymentAmountInPrimaryCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency); - if (!$partialAllowed && round($paymentAmountInPrimaryCurrency, 2) < $order->getOutstandingBalance()) { + if (!$partialAllowed && Currency::round($paymentAmountInPrimaryCurrency) < $order->getOutstandingBalance()) { $error = Craft::t('commerce', 'Partial payment not allowed.'); $this->setFailFlash($error); Craft::$app->getUrlManager()->setRouteParams(['paymentForm' => $paymentForm, $this->_cartVariableName => $order]); From b5d89a3995be9cee78583380345c75af38da716d Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Fri, 25 Jun 2021 16:02:50 +0800 Subject: [PATCH 4/4] Add rounding option to converCurrency --- src/controllers/PaymentsController.php | 4 ++-- src/services/PaymentCurrencies.php | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/controllers/PaymentsController.php b/src/controllers/PaymentsController.php index 3739be8190..3ae60a89f8 100644 --- a/src/controllers/PaymentsController.php +++ b/src/controllers/PaymentsController.php @@ -426,9 +426,9 @@ public function actionPay() $order->setPaymentAmount($paymentAmount); } - $paymentAmountInPrimaryCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency); + $paymentAmountInPrimaryCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertCurrency($order->getPaymentAmount(), $order->paymentCurrency, $order->currency, true); - if (!$partialAllowed && Currency::round($paymentAmountInPrimaryCurrency) < $order->getOutstandingBalance()) { + if (!$partialAllowed && $paymentAmountInPrimaryCurrency < $order->getOutstandingBalance()) { $error = Craft::t('commerce', 'Partial payment not allowed.'); $this->setFailFlash($error); Craft::$app->getUrlManager()->setRouteParams(['paymentForm' => $paymentForm, $this->_cartVariableName => $order]); diff --git a/src/services/PaymentCurrencies.php b/src/services/PaymentCurrencies.php index 70296e7b69..2888611ef6 100644 --- a/src/services/PaymentCurrencies.php +++ b/src/services/PaymentCurrencies.php @@ -19,8 +19,7 @@ use yii\base\Component; use yii\base\Exception; use yii\base\InvalidConfigException; -use Money\Converter; -use Money\Currency; +use craft\commerce\helpers\Currency as CurrencyHelper; use Money\Exchange\FixedExchange; use Money\Exchange\ReversedCurrenciesExchange; @@ -195,10 +194,11 @@ public function convert(float $amount, string $currency): float * @param float $amount * @param string $fromCurrency * @param string $toCurrency + * @param bool $round * @return float * @throws CurrencyException if currency not found by its ISO code */ - public function convertCurrency(float $amount, string $fromCurrency, string $toCurrency): float + public function convertCurrency(float $amount, string $fromCurrency, string $toCurrency, $round = false): float { $fromCurrency = $this->getPaymentCurrencyByIso($fromCurrency); $toCurrency = $this->getPaymentCurrencyByIso($toCurrency); @@ -213,10 +213,16 @@ public function convertCurrency(float $amount, string $fromCurrency, string $toC if ($this->getPrimaryPaymentCurrency()->iso != $fromCurrency) { // now the amount is in the primary currency - $amount = $amount / $fromCurrency->rate; + $amount /= $fromCurrency->rate; } - return $amount * $toCurrency->rate; + $result = $amount * $toCurrency->rate; + + if ($round) { + return CurrencyHelper::round($result, $toCurrency); + } + + return $result; }