Skip to content

Commit d3f6b41

Browse files
author
Babichev Maxim
committed
add docs exchange
1 parent a59a112 commit d3f6b41

File tree

5 files changed

+251
-5
lines changed

5 files changed

+251
-5
lines changed

docs/exchange.md

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,124 @@
11
### Exchange
22

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+

docs/laravel-wallet-swap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ $rub = $user->getWallet('rub');
3434
$usd->balance; // int(200)
3535
$rub->balance; // int(0)
3636

37-
$usd->exchange(10, $rub);
37+
$usd->exchange($rub, 10);
3838
$usd->balance; // int(190)
3939
$rub->balance; // int(622)
4040
```

docs/ru/_sidebar.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
- [Перевод](transfer)
1515
- [Пересчёт баланса](refresh)
1616
- [Подтверждение](confirm)
17-
- [Exchange](exchange)
17+
- [Обмен](exchange)
1818
- [Налог на списание](taxing)
1919

2020
- Покупки
2121

2222
- [Платеж](payment)
2323
- [Купить бесплатно](pay-free)
2424
- [Возврат](refund)
25-
- [Gift](gift)
25+
- [Подарок](gift)
2626
- [Корзина](cart)
2727

2828
- Additions

docs/ru/exchange.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
### Обмен
2+
3+
Задачи у всех разные и с помощью данной функциональности
4+
можно добавить курсы валют в ваши кошельки.
5+
6+
Конфигурация валют происходит в общем файле конфигурации `config/wallet.php`.
7+
8+
```php
9+
'currencies' => [
10+
'xbtc' => 'BTC',
11+
'dollar' => 'USD',
12+
'ruble' => 'RUB',
13+
],
14+
```
15+
16+
Ключ в конфигурации это `slug` вашего кошелька.
17+
Значение, это валюта вашего кошелька.
18+
19+
Сервис для работы с валютами вам нужно написать самому или
20+
использовать [готовую библиотеку](https://github.com/bavix/laravel-wallet-swap).
21+
22+
#### Сервис для работы с валютой
23+
24+
Напишем простой сервис. Данные будем брать из массива, а не из базы данных.
25+
26+
```php
27+
use Bavix\Wallet\Interfaces\Wallet;
28+
use Bavix\Wallet\Services\WalletService;
29+
use Illuminate\Support\Arr;
30+
31+
class MyRateService extends \Bavix\Wallet\Simple\Rate
32+
{
33+
34+
// список курса валют (берёте из базы данных)
35+
protected $rates = [
36+
'USD' => [
37+
'RUB' => 67.61,
38+
],
39+
'RUB' => [
40+
'USD' => 0.0147907114,
41+
],
42+
];
43+
44+
protected function rate(Wallet $wallet): float
45+
{
46+
$from = app(WalletService::class)->getWallet($this->withCurrency);
47+
$to = app(WalletService::class)->getWallet($wallet);
48+
49+
return Arr::get(
50+
Arr::get($this->rates, $from->currency, []),
51+
$to->currency,
52+
1
53+
);
54+
}
55+
56+
public function convertTo(Wallet $wallet): float
57+
{
58+
return parent::convertTo($wallet) * $this->rate($wallet);
59+
}
60+
61+
}
62+
63+
```
64+
65+
#### Регистрация сервиса
66+
67+
Написанный вами сервис необходимо зарегистрировать, делается это в файле `config/wallet.php`.
68+
69+
```php
70+
return [
71+
// ...
72+
'package' => [
73+
'rateable' => MyRateService::class,
74+
// ...
75+
],
76+
// ...
77+
];
78+
```
79+
80+
#### Процесс обмена
81+
82+
Создадим два кошелька.
83+
84+
```php
85+
$usd = $user->createWallet([
86+
'name' => 'My Dollars',
87+
'slug' => 'dollar',
88+
]);
89+
90+
$rub = $user->createWallet([
91+
'name' => 'Мои рубли',
92+
'slug' => 'ruble',
93+
]);
94+
```
95+
96+
Пополним рублевый кошелёк на 100 рублей.
97+
98+
```php
99+
$rub->deposit(10000);
100+
```
101+
102+
Переведём рубли в доллары.
103+
104+
```php
105+
$transfer = $rub->exchange($usd, 10000);
106+
$rub->balance; // int(0)
107+
$usd->balance; // int(147), это $1.47
108+
```
109+
110+
К сожалению, мир не идеален. Вы не получите обратно свои 100 рублей.
111+
112+
```php
113+
$transfer = $usd->exchange($rub, $usd->balance);
114+
$usd->balance; int(0)
115+
$rub->balance; int(9938)
116+
```
117+
118+
За счёт конвертации и математических округлений вы потеряли 62 копейки.
119+
У вас осталось 99 рублей 38 копеек.
120+
121+
---
122+
Это работает!

docs/ru/upgrade-guide.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ class Item extends Model implements Product
5959

6060
}
6161
```
62+
63+
## 3.0.x → 3.1.x
64+
65+
Замените `Taxing` на `Taxable`.

0 commit comments

Comments
 (0)