Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/PaymentProvider.php
Show First 20 Lines • Show All 193 Lines • ▼ Show 20 Lines | abstract class PaymentProvider | ||||
* @param string $sourceCurrency Currency from which to convert | * @param string $sourceCurrency Currency from which to convert | ||||
* @param string $targetCurrency Currency to convert to | * @param string $targetCurrency Currency to convert to | ||||
* | * | ||||
* @return float Exchange rate | * @return float Exchange rate | ||||
*/ | */ | ||||
protected function exchangeRate(string $sourceCurrency, string $targetCurrency): float | protected function exchangeRate(string $sourceCurrency, string $targetCurrency): float | ||||
{ | { | ||||
if (strcasecmp($sourceCurrency, $targetCurrency)) { | if (strcasecmp($sourceCurrency, $targetCurrency)) { | ||||
throw new \Exception("Currency conversion is not yet implemented."); | return \App\Utils::exchangeRate($sourceCurrency, $targetCurrency); | ||||
//FIXME Not yet implemented | |||||
} | } | ||||
return 1.0; | return 1.0; | ||||
} | } | ||||
/** | /** | ||||
* Convert a value from $sourceCurrency to $targetCurrency | * Convert a value from $sourceCurrency to $targetCurrency | ||||
* | * | ||||
* @param int $amount Amount in cents of $sourceCurrency | * @param int $amount Amount in cents of $sourceCurrency | ||||
Show All 23 Lines | protected function storeRefund(Wallet $wallet, array $refund): void | ||||
} | } | ||||
// Preserve originally refunded amount | // Preserve originally refunded amount | ||||
$refund['currency_amount'] = $refund['amount']; | $refund['currency_amount'] = $refund['amount']; | ||||
// Convert amount to wallet currency | // Convert amount to wallet currency | ||||
// TODO We should possibly be using the same exchange rate as for the original payment? | // TODO We should possibly be using the same exchange rate as for the original payment? | ||||
$amount = $this->exchange($refund['amount'], $refund['currency'], $wallet->currency); | $amount = $this->exchange($refund['amount'], $refund['currency'], $wallet->currency); | ||||
machniak: This code does not look nice. We should make exchange() to be able to work with any exchange… | |||||
$wallet->balance -= $amount; | $wallet->balance -= $amount; | ||||
$wallet->save(); | $wallet->save(); | ||||
if ($refund['type'] == self::TYPE_CHARGEBACK) { | if ($refund['type'] == self::TYPE_CHARGEBACK) { | ||||
$transaction_type = Transaction::WALLET_CHARGEBACK; | $transaction_type = Transaction::WALLET_CHARGEBACK; | ||||
} else { | } else { | ||||
$transaction_type = Transaction::WALLET_REFUND; | $transaction_type = Transaction::WALLET_REFUND; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | protected static function paymentMethodsWhitelist($type): array | ||||
self::METHOD_CREDITCARD => [ | self::METHOD_CREDITCARD => [ | ||||
'id' => self::METHOD_CREDITCARD, | 'id' => self::METHOD_CREDITCARD, | ||||
'icon' => self::$paymentMethodIcons[self::METHOD_CREDITCARD] | 'icon' => self::$paymentMethodIcons[self::METHOD_CREDITCARD] | ||||
], | ], | ||||
self::METHOD_PAYPAL => [ | self::METHOD_PAYPAL => [ | ||||
'id' => self::METHOD_PAYPAL, | 'id' => self::METHOD_PAYPAL, | ||||
'icon' => self::$paymentMethodIcons[self::METHOD_PAYPAL] | 'icon' => self::$paymentMethodIcons[self::METHOD_PAYPAL] | ||||
], | ], | ||||
// TODO Enable once we're ready to offer them | self::METHOD_BANKTRANSFER => [ | ||||
// self::METHOD_BANKTRANSFER => [ | 'id' => self::METHOD_BANKTRANSFER, | ||||
// 'id' => self::METHOD_BANKTRANSFER, | 'icon' => self::$paymentMethodIcons[self::METHOD_BANKTRANSFER] | ||||
// 'icon' => self::$paymentMethodIcons[self::METHOD_BANKTRANSFER] | ] | ||||
// ] | |||||
]; | ]; | ||||
case PaymentProvider::TYPE_RECURRING: | case PaymentProvider::TYPE_RECURRING: | ||||
return [ | return [ | ||||
self::METHOD_CREDITCARD => [ | self::METHOD_CREDITCARD => [ | ||||
'id' => self::METHOD_CREDITCARD, | 'id' => self::METHOD_CREDITCARD, | ||||
'icon' => self::$paymentMethodIcons[self::METHOD_CREDITCARD] | 'icon' => self::$paymentMethodIcons[self::METHOD_CREDITCARD] | ||||
] | ] | ||||
]; | ]; | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |
This code does not look nice. We should make exchange() to be able to work with any exchange direction. Actually exchangeRate() should.
Also, how about adding a couple of simple tests for exchange() and/or exchangeRate() itself? Also maybe remove exchangeRate() from the payment provider and use the one from Utils directly, the wrapper should not be needed.