Skip to content

Commit 73fd6fe

Browse files
committed
WIP
1 parent 21ee794 commit 73fd6fe

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\events;
9+
10+
use craft\commerce\models\Transaction;
11+
use yii\base\Event;
12+
13+
/**
14+
* Payment Currency Rate Event
15+
*
16+
* @property ?Transaction $transaction
17+
* @since 2.0
18+
*/
19+
class PaymentCurrencyRateEvent extends Event
20+
{
21+
/**
22+
* @var float The rate the event
23+
*/
24+
public float $rate;
25+
26+
public ?Transaction $transaction = null;
27+
}

src/models/PaymentCurrency.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace craft\commerce\models;
99

1010
use craft\commerce\base\Model;
11+
use craft\commerce\events\PaymentCurrencyRateEvent;
1112
use craft\commerce\records\PaymentCurrency as PaymentCurrencyRecord;
1213
use craft\helpers\UrlHelper;
1314
use craft\validators\UniqueValidator;
@@ -28,7 +29,13 @@
2829
*/
2930
class PaymentCurrency extends Model
3031
{
31-
/**
32+
33+
/**
34+
* @event PaymentCurrencyRateEvent The event that is triggered when the payment currency rate is defined
35+
*/
36+
const EVENT_DEFINE_PAYMENT_CURRENCY_RATE = 'definePaymentCurrencyRate';
37+
38+
/**
3239
* @var int|null ID
3340
*/
3441
public ?int $id = null;
@@ -150,6 +157,22 @@ public function setCurrency(Currency $currency): void
150157
$this->_currency = $currency;
151158
}
152159

160+
/**
161+
* @param Transaction|null $transaction
162+
* @return float
163+
*/
164+
public function getRate(Transaction $transaction = null) : float
165+
{
166+
$event = new PaymentCurrencyRateEvent([
167+
'rate' => $this->rate,
168+
'transaction' => $transaction,
169+
]);
170+
171+
$this->trigger(self::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $event);
172+
173+
return $this->rate;
174+
}
175+
153176
/**
154177
* @inheritdoc
155178
*/

src/services/PaymentCurrencies.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ public function convertCurrency(float $amount, string $fromCurrency, string $toC
197197

198198
if ($this->getPrimaryPaymentCurrency()->iso != $fromCurrency) {
199199
// now the amount is in the primary currency
200-
$amount /= $fromCurrency->rate;
200+
$amount /= $fromCurrency->getRate();
201201
}
202202

203-
$result = $amount * $toCurrency->rate;
203+
$result = $amount * $toCurrency->getRate();
204204

205205
if ($round) {
206206
return CurrencyHelper::round($result, $toCurrency);
@@ -239,7 +239,7 @@ public function savePaymentCurrency(PaymentCurrency $model, bool $runValidation
239239
$record->iso = strtoupper($model->iso);
240240
$record->primary = $model->primary;
241241
// If this rate is primary, the rate must be 1 since it is now the rate all prices are enter in as.
242-
$record->rate = $model->primary ? 1 : $model->rate;
242+
$record->rate = $model->primary ? 1 : $model->getRate();
243243

244244
$record->save(false);
245245

src/services/Transactions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function createTransaction(Order $order = null, Transaction $parentTransa
216216
$transaction->amount = Currency::round($amount, $currency);
217217

218218
// Capture historical rate
219-
$transaction->paymentRate = $paymentCurrency->rate;
219+
$transaction->paymentRate = $paymentCurrency->getRate($transaction);
220220

221221
$transaction->setOrder($order);
222222
}

0 commit comments

Comments
 (0)