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

[4.x] Allow payment currency rate override #3371

Draft
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions src/events/PaymentCurrencyRateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\events;

use craft\commerce\models\Transaction;
use yii\base\Event;

/**
* Payment Currency Rate Event
*
* @property ?Transaction $transaction
* @since 2.0
*/
class PaymentCurrencyRateEvent extends Event
{
/**
* @var float The rate the event
*/
public float $rate;

public ?Transaction $transaction = null;
}
25 changes: 24 additions & 1 deletion src/models/PaymentCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\commerce\models;

use craft\commerce\base\Model;
use craft\commerce\events\PaymentCurrencyRateEvent;
use craft\commerce\records\PaymentCurrency as PaymentCurrencyRecord;
use craft\helpers\UrlHelper;
use craft\validators\UniqueValidator;
Expand All @@ -28,7 +29,13 @@
*/
class PaymentCurrency extends Model
{
/**

/**
* @event PaymentCurrencyRateEvent The event that is triggered when the payment currency rate is defined
*/
const EVENT_DEFINE_PAYMENT_CURRENCY_RATE = 'definePaymentCurrencyRate';

/**
* @var int|null ID
*/
public ?int $id = null;
Expand Down Expand Up @@ -150,6 +157,22 @@ public function setCurrency(Currency $currency): void
$this->_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
*/
Expand Down
6 changes: 3 additions & 3 deletions src/services/PaymentCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/services/Transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading