77
88namespace Two \Gateway \Service \Order ;
99
10+ use Magento \Directory \Model \CurrencyFactory ;
11+ use Magento \Framework \Exception \LocalizedException ;
1012use Two \Gateway \Api \Config \RepositoryInterface as ConfigRepository ;
1113use Two \Gateway \Api \Log \RepositoryInterface as LogRepository ;
1214use Two \Gateway \Model \Config \Source \SurchargeType ;
@@ -38,14 +40,21 @@ class SurchargeCalculator
3840 */
3941 private $ logRepository ;
4042
43+ /**
44+ * @var CurrencyFactory
45+ */
46+ private $ currencyFactory ;
47+
4148 public function __construct (
4249 ConfigRepository $ configRepository ,
4350 Adapter $ apiAdapter ,
44- LogRepository $ logRepository
51+ LogRepository $ logRepository ,
52+ CurrencyFactory $ currencyFactory
4553 ) {
4654 $ this ->configRepository = $ configRepository ;
4755 $ this ->apiAdapter = $ apiAdapter ;
4856 $ this ->logRepository = $ logRepository ;
57+ $ this ->currencyFactory = $ currencyFactory ;
4958 }
5059
5160 /**
@@ -54,14 +63,17 @@ public function __construct(
5463 * @param float $grossAmount Order gross amount
5564 * @param int $selectedTermDays The term the buyer selected
5665 * @param string $buyerCountry ISO Alpha-2 country code
66+ * @param string $orderCurrency ISO currency code of the order
5767 * @param int|null $storeId
5868 *
5969 * @return array{amount: float, tax_rate: float, description: string}
70+ * @throws LocalizedException if fixed fee currency conversion fails
6071 */
6172 public function calculate (
6273 float $ grossAmount ,
6374 int $ selectedTermDays ,
6475 string $ buyerCountry ,
76+ string $ orderCurrency ,
6577 ?int $ storeId = null
6678 ): array {
6779 $ surchargeType = $ this ->configRepository ->getSurchargeType ($ storeId );
@@ -72,6 +84,7 @@ public function calculate(
7284
7385 $ feeBase = $ this ->getFeeBase ($ grossAmount , $ selectedTermDays , $ buyerCountry , $ storeId );
7486 $ config = $ this ->configRepository ->getSurchargeConfig ($ selectedTermDays , $ storeId );
87+ $ fixedCurrency = $ this ->configRepository ->getSurchargeFixedCurrency ($ storeId );
7588
7689 $ surcharge = 0.0 ;
7790
@@ -82,12 +95,17 @@ public function calculate(
8295 $ surcharge += $ feeBase * ($ config ['percentage ' ] / 100 );
8396 }
8497 if ($ hasFixed ) {
85- $ surcharge += $ config ['fixed ' ];
98+ $ fixedAmount = (float )$ config ['fixed ' ];
99+ $ surcharge += $ this ->convertAmount ($ fixedAmount , $ fixedCurrency , $ orderCurrency );
86100 }
87101
88- // Apply limit cap
89- if ($ config ['limit ' ] > 0 && $ surcharge > $ config ['limit ' ]) {
90- $ surcharge = $ config ['limit ' ];
102+ // Apply limit cap (null = not set = no cap; 0 = explicit zero cap)
103+ $ limit = $ config ['limit ' ];
104+ if ($ limit !== null ) {
105+ $ limit = $ this ->convertAmount ($ limit , $ fixedCurrency , $ orderCurrency );
106+ if ($ surcharge > $ limit ) {
107+ $ surcharge = $ limit ;
108+ }
91109 }
92110
93111 // Round up to next cent
@@ -98,6 +116,8 @@ public function calculate(
98116 'fee_base ' => $ feeBase ,
99117 'surcharge_type ' => $ surchargeType ,
100118 'config ' => $ config ,
119+ 'fixed_currency ' => $ fixedCurrency ,
120+ 'order_currency ' => $ orderCurrency ,
101121 'result ' => $ surcharge ,
102122 ]);
103123
@@ -160,4 +180,30 @@ private function fetchMerchantFee(
160180
161181 return (float )($ response ['total_fee ' ] ?? 0 );
162182 }
183+
184+ /**
185+ * Convert an amount between currencies if needed.
186+ *
187+ * @throws LocalizedException if Magento has no exchange rate for the pair
188+ */
189+ private function convertAmount (float $ amount , string $ fromCurrency , string $ toCurrency ): float
190+ {
191+ if ($ amount == 0 || $ fromCurrency === '' || $ fromCurrency === $ toCurrency ) {
192+ return $ amount ;
193+ }
194+
195+ try {
196+ $ currency = $ this ->currencyFactory ->create ()->load ($ fromCurrency );
197+ return (float )$ currency ->convert ($ amount , $ toCurrency );
198+ } catch (\Exception $ e ) {
199+ throw new LocalizedException (
200+ __ (
201+ 'Cannot convert surcharge from %1 to %2. '
202+ . 'Please configure currency exchange rates under Stores > Currency Rates. ' ,
203+ $ fromCurrency ,
204+ $ toCurrency
205+ )
206+ );
207+ }
208+ }
163209}
0 commit comments