diff --git a/src/controllers/PaymentsController.php b/src/controllers/PaymentsController.php index 5740c39114..3ae60a89f8 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; @@ -425,7 +426,7 @@ 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 && $paymentAmountInPrimaryCurrency < $order->getOutstandingBalance()) { $error = Craft::t('commerce', 'Partial payment not allowed.'); 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; }