Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/payment currency convert #2226

Merged
merged 4 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/controllers/PaymentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
Expand Down
16 changes: 11 additions & 5 deletions src/services/PaymentCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}


Expand Down