diff --git a/src/events/PaymentCurrencyRateEvent.php b/src/events/PaymentCurrencyRateEvent.php new file mode 100644 index 0000000000..2346fd38dc --- /dev/null +++ b/src/events/PaymentCurrencyRateEvent.php @@ -0,0 +1,27 @@ +_currency = $currency; } + /** + * @param Transaction|null $transaction + * @return float + */ + public function getRate(Transaction $transaction = null) : float + { + $event = new PaymentCurrencyRateEvent([ + 'rate' => $this->rate, + 'transaction' => $transaction, + ]); + + $this->trigger(self::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $event); + + return $this->rate; + } + /** * @inheritdoc */ diff --git a/src/services/PaymentCurrencies.php b/src/services/PaymentCurrencies.php index 1a33604fa7..ebc8d379cb 100644 --- a/src/services/PaymentCurrencies.php +++ b/src/services/PaymentCurrencies.php @@ -197,10 +197,10 @@ public function convertCurrency(float $amount, string $fromCurrency, string $toC if ($this->getPrimaryPaymentCurrency()->iso != $fromCurrency) { // now the amount is in the primary currency - $amount /= $fromCurrency->rate; + $amount /= $fromCurrency->getRate(); } - $result = $amount * $toCurrency->rate; + $result = $amount * $toCurrency->getRate(); if ($round) { return CurrencyHelper::round($result, $toCurrency); @@ -239,7 +239,7 @@ public function savePaymentCurrency(PaymentCurrency $model, bool $runValidation $record->iso = strtoupper($model->iso); $record->primary = $model->primary; // If this rate is primary, the rate must be 1 since it is now the rate all prices are enter in as. - $record->rate = $model->primary ? 1 : $model->rate; + $record->rate = $model->primary ? 1 : $model->getRate(); $record->save(false); diff --git a/src/services/Transactions.php b/src/services/Transactions.php index 8c528813da..3d66d5d31d 100644 --- a/src/services/Transactions.php +++ b/src/services/Transactions.php @@ -216,7 +216,7 @@ public function createTransaction(Order $order = null, Transaction $parentTransa $transaction->amount = Currency::round($amount, $currency); // Capture historical rate - $transaction->paymentRate = $paymentCurrency->rate; + $transaction->paymentRate = $paymentCurrency->getRate($transaction); $transaction->setOrder($order); }