|
1 | 1 | ### Exchange |
2 | 2 |
|
3 | | -A description of this feature will appear later. Wait! |
4 | | -For now you can study to see the code in the unit test. |
| 3 | +Everyone’s tasks are different and with the help of this functionality |
| 4 | +you can add exchange rates to your wallets. |
| 5 | + |
| 6 | +Currencies are configured in the general configuration file `config/wallet.php`. |
| 7 | + |
| 8 | +```php |
| 9 | + 'currencies' => [ |
| 10 | + 'xbtc' => 'BTC', |
| 11 | + 'dollar' => 'USD', |
| 12 | + 'ruble' => 'RUB', |
| 13 | + ], |
| 14 | +``` |
| 15 | + |
| 16 | +The key in the configuration is the `slug` of your wallet. |
| 17 | +Value, this is the currency of your wallet. |
| 18 | + |
| 19 | +Service for working with currencies you need to write yourself or |
| 20 | +use [library](https://github.com/bavix/laravel-wallet-swap). |
| 21 | + |
| 22 | +#### Service for working with currency |
| 23 | + |
| 24 | +We will write a simple service. |
| 25 | +We will take the data from the array, and not from the database. |
| 26 | + |
| 27 | +```php |
| 28 | +use Bavix\Wallet\Interfaces\Wallet; |
| 29 | +use Bavix\Wallet\Services\WalletService; |
| 30 | +use Illuminate\Support\Arr; |
| 31 | + |
| 32 | +class MyRateService extends \Bavix\Wallet\Simple\Rate |
| 33 | +{ |
| 34 | + |
| 35 | + // list of exchange rates (take from the database) |
| 36 | + protected $rates = [ |
| 37 | + 'USD' => [ |
| 38 | + 'RUB' => 67.61, |
| 39 | + ], |
| 40 | + 'RUB' => [ |
| 41 | + 'USD' => 0.0147907114, |
| 42 | + ], |
| 43 | + ]; |
| 44 | + |
| 45 | + protected function rate(Wallet $wallet): float |
| 46 | + { |
| 47 | + $from = app(WalletService::class)->getWallet($this->withCurrency); |
| 48 | + $to = app(WalletService::class)->getWallet($wallet); |
| 49 | + |
| 50 | + return Arr::get( |
| 51 | + Arr::get($this->rates, $from->currency, []), |
| 52 | + $to->currency, |
| 53 | + 1 |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function convertTo(Wallet $wallet): float |
| 58 | + { |
| 59 | + return parent::convertTo($wallet) * $this->rate($wallet); |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +#### Service Registration |
| 67 | + |
| 68 | +The service you wrote must be registered, this is done in the file `config/wallet.php`. |
| 69 | + |
| 70 | +```php |
| 71 | +return [ |
| 72 | + // ... |
| 73 | + 'package' => [ |
| 74 | + 'rateable' => MyRateService::class, |
| 75 | + // ... |
| 76 | + ], |
| 77 | + // ... |
| 78 | +]; |
| 79 | +``` |
| 80 | + |
| 81 | +#### Exchange process |
| 82 | + |
| 83 | +Create two wallets. |
| 84 | + |
| 85 | +```php |
| 86 | +$usd = $user->createWallet([ |
| 87 | + 'name' => 'My Dollars', |
| 88 | + 'slug' => 'dollar', |
| 89 | +]); |
| 90 | + |
| 91 | +$rub = $user->createWallet([ |
| 92 | + 'name' => 'My Rub', |
| 93 | + 'slug' => 'ruble', |
| 94 | +]); |
| 95 | +``` |
| 96 | + |
| 97 | +We replenish the ruble wallet with 100 rubles. |
| 98 | + |
| 99 | +```php |
| 100 | +$rub->deposit(10000); |
| 101 | +``` |
| 102 | + |
| 103 | +We will exchange rubles into dollars. |
| 104 | + |
| 105 | +```php |
| 106 | +$transfer = $rub->exchange($usd, 10000); |
| 107 | +$rub->balance; // int(0) |
| 108 | +$usd->balance; // int(147), это $1.47 |
| 109 | +``` |
| 110 | + |
| 111 | +Unfortunately, the world is not perfect. You will not get back your 100 rubles. |
| 112 | + |
| 113 | +```php |
| 114 | +$transfer = $usd->exchange($rub, $usd->balance); |
| 115 | +$usd->balance; int(0) |
| 116 | +$rub->balance; int(9938) |
| 117 | +``` |
| 118 | + |
| 119 | +Due to conversion and mathematical rounding, you lost 62 kopecks. |
| 120 | +You have 99 rubles 38 kopecks left. |
| 121 | + |
| 122 | +--- |
| 123 | +It worked! |
| 124 | + |
0 commit comments