@@ -12,23 +12,6 @@ $user->createWallet([
1212]);
1313```
1414
15- > Management via config file is deprecated.
16- > Create wallets with parameter in meta.currency.
17- > In the 7.x release, the ability to specify the currency through wallet.php will be removed.
18-
19- Currencies are configured in the general configuration file ` config/wallet.php ` .
20-
21- ``` php
22- 'currencies' => [
23- 'xbtc' => 'BTC',
24- 'dollar' => 'USD',
25- 'ruble' => 'RUB',
26- ],
27- ```
28-
29- The key in the configuration is the ` slug ` of your wallet.
30- Value, this is the currency of your wallet.
31-
3215Service for working with currencies you need to write yourself or
3316use [ library] ( https://github.com/bavix/laravel-wallet-swap ) .
3417
@@ -38,42 +21,38 @@ We will write a simple service.
3821We will take the data from the array, and not from the database.
3922
4023``` php
41- use Bavix\Wallet\Interfaces\Wallet;
42- use Bavix\Wallet\Services\WalletServiceLegacy;
43- use Illuminate\Support\Arr;
24+ use Bavix\Wallet\Internal\Service\MathServiceInterface;
25+ use Bavix\Wallet\Services\ExchangeServiceInterface;
4426
45- class MyRateService extends \Bavix\Wallet\Simple\Rate
27+ class MyExchangeService implements ExchangeServiceInterface
4628{
47-
48- // list of exchange rates (take from the database)
49- protected $rates = [
29+ private array $rates = [
5030 'USD' => [
5131 'RUB' => 67.61,
5232 ],
53- 'RUB' => [
54- 'USD' => 0.0147907114,
55- ],
5633 ];
5734
58- protected function rate(Wallet $wallet)
35+ private MathServiceInterface $mathService;
36+
37+ public function __construct(MathServiceInterface $mathService)
5938 {
60- $from = app(WalletServiceLegacy::class)->getWallet($this->withCurrency);
61- $to = app(WalletServiceLegacy::class)->getWallet($wallet);
62-
63- return Arr::get(
64- Arr::get($this->rates, $from->currency, []),
65- $to->currency,
66- 1
67- );
39+ $this->mathService = $mathService;
40+
41+ foreach ($this->rates as $from => $rates) {
42+ foreach ($rates as $to => $rate) {
43+ if (empty($this->rates[$to][$from])) {
44+ $this->rates[$to][$from] = $this->mathService->div(1, $rate);
45+ }
46+ }
47+ }
6848 }
6949
70- public function convertTo(Wallet $wallet)
50+ /** @param float|int|string $amount */
51+ public function convertTo(string $fromCurrency, string $toCurrency, $amount): string
7152 {
72- return parent::convertTo($wallet) * $this->rate($wallet );
53+ return $this->mathService->mul($amount, $this->rates[$fromCurrency][$toCurrency] ?? 1 );
7354 }
74-
7555}
76-
7756```
7857
7958#### Service Registration
@@ -83,8 +62,8 @@ The service you wrote must be registered, this is done in the file `config/walle
8362``` php
8463return [
8564 // ...
86- 'package ' => [
87- 'rateable ' => MyRateService ::class,
65+ 'services ' => [
66+ 'exchange ' => MyExchangeService ::class,
8867 // ...
8968 ],
9069 // ...
@@ -98,12 +77,12 @@ Create two wallets.
9877``` php
9978$usd = $user->createWallet([
10079 'name' => 'My Dollars',
101- 'slug ' => 'dollar' ,
80+ 'meta ' => ['currency' => 'USD'] ,
10281]);
10382
10483$rub = $user->createWallet([
105- 'name' => 'My Rub ',
106- 'slug ' => 'ruble' ,
84+ 'name' => 'My Ruble ',
85+ 'meta ' => ['currency' => 'RUB'] ,
10786]);
10887```
10988
0 commit comments