Skip to content

Commit 7343111

Browse files
authored
Merge pull request #113 from bavix/docs
Docs
2 parents 3bcef98 + ed5c09e commit 7343111

18 files changed

+684
-16
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ matrix:
1414
- php: 'nightly'
1515

1616
before_script:
17-
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d
17+
- pecl install pcov
18+
- phpenv config-rm xdebug.ini || echo "xdebug not available"
1819
- bash <(curl -s https://raw.githubusercontent.com/php-cache/cache/master/build/php/7.2/Memcached.sh)
1920
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
2021
- chmod +x ./cc-test-reporter

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ laravel-wallet - Easy work with virtual wallet.
2323
* **Vendor**: bavix
2424
* **Package**: laravel-wallet
2525
* **Version**: [![Latest Stable Version](https://poser.pugx.org/bavix/laravel-wallet/v/stable)](https://packagist.org/packages/bavix/laravel-wallet)
26-
* **PHP Version**: 7.1+
26+
* **PHP Version**: 7.2+
2727
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.0`
2828
* **[Composer](https://getcomposer.org/):** `composer require bavix/laravel-wallet`
2929

@@ -36,6 +36,7 @@ To perform the migration, you will be [helped by the instruction](https://bavix.
3636
| Extension | Description |
3737
| ----- | ----- |
3838
| [Swap](https://github.com/bavix/laravel-wallet-swap) | Addition to the laravel-wallet library for quick setting of exchange rates |
39+
| [Vacuum](https://github.com/bavix/laravel-wallet-vacuum) | Addition to the laravel-wallet library for quick fix race condition |
3940

4041
### Usage
4142
Add the `HasWallet` trait and `Wallet` interface to model.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"infection/infection": "^0.14",
3535
"orchestra/testbench": "^4.0",
3636
"phpstan/phpstan": "^0.11",
37-
"phpunit/phpunit": "^8.2",
37+
"phpunit/phpunit": "^8.3",
3838
"laravel/cashier": "^7.0|^8.0|^9.0|^10.0"
3939
},
4040
"suggest": {

docs/_sidebar.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- [Transfer](transfer)
1515
- [Refresh balance](refresh)
1616
- [Confirm](confirm)
17-
<!-- - [Exchange](exchange)-->
17+
- [Exchange](exchange)
1818
- [Withdraw taxing](taxing)
1919

2020
- Purchases
@@ -34,8 +34,10 @@
3434
- [New Wallet](new-wallet)
3535
- [Transfer](wallet-transfer)
3636

37-
<!--- Currencies-->
37+
<!--
38+
- Currencies
3839
39-
<!-- - [Rate Service](rate)-->
40-
<!-- - [Create Wallet](rate-wallet)-->
41-
<!-- - [Taxing](rate-taxing)-->
40+
- [Rate Service](rate)
41+
- [Create Wallet](rate-wallet)
42+
- [Taxing](rate-taxing)
43+
-->

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/gift.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ $item->balance; // int(0)
8181

8282
The first user buys the product and gives it.
8383

84+
> If the product uses the `Taxable` interface, then Santa will pay tax
85+
8486
```php
8587
$first->gift($last, $item);
8688
(bool)$last->paid($item, true); // bool(true)

docs/laravel-wallet-swap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class User extends Model implements Wallet
2525
```
2626

2727
### Simple example
28-
Find wallets and translate from one to another.
28+
Find wallets and exchange from one to another.
2929

3030
```php
3131
$usd = $user->getWallet('usd');
@@ -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/requirements.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## PHP
22
The following versions of PHP are supported:
33

4-
- PHP 7.1
54
- PHP 7.2
65
- PHP 7.3
76

docs/ru/_sidebar.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [Введение](README)
44
- [Требования](requirements)
55
- [Установка](installation)
6+
- [Установка в Lumen](lumen)
67
- [Как использовать](basic-usage)
78
- [Обновление](upgrade-guide)
89

@@ -12,15 +13,31 @@
1213
- [Вывод](withdraw)
1314
- [Перевод](transfer)
1415
- [Пересчёт баланса](refresh)
16+
- [Подтверждение](confirm)
17+
- [Обмен](exchange)
18+
- [Налог на списание](taxing)
1519

1620
- Покупки
1721

1822
- [Платеж](payment)
19-
- [Корзина](cart)
2023
- [Купить бесплатно](pay-free)
2124
- [Возврат](refund)
25+
- [Подарок](gift)
26+
- [Корзина](cart)
27+
28+
- Дополнения
29+
30+
- [Wallet Swap](laravel-wallet-swap)
2231

2332
- Кошельки
2433

2534
- [Создать кошелек](new-wallet)
2635
- [Перевод](wallet-transfer)
36+
37+
<!--
38+
- Currencies
39+
40+
- [Rate Service](rate)
41+
- [Create Wallet](rate-wallet)
42+
- [Taxing](rate-taxing)
43+
-->

docs/ru/confirm.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## User Model
2+
3+
Добавьте `CanConfirm` trait и `Confirmable` interface в модель User.
4+
5+
```php
6+
use Bavix\Wallet\Interfaces\Confirmable;
7+
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Traits\CanConfirm;
9+
use Bavix\Wallet\Traits\HasWallet;
10+
11+
class UserConfirm extends Model implements Wallet, Confirmable
12+
{
13+
use HasWallet, CanConfirm;
14+
}
15+
```
16+
17+
### Example:
18+
19+
Иногда, необходимо подтвердить операцию и пересчитать баланс.
20+
Теперь это доступно в библиотеке из коробки. Вот пример:
21+
22+
```php
23+
$user->balance; // int(0)
24+
$transaction = $user->deposit(100, null, false); // не подтверждена
25+
$transaction->confirmed; // bool(false)
26+
$user->balance; // int(0)
27+
28+
$user->confirm($transaction); // bool(true)
29+
$transaction->confirmed; // bool(true)
30+
31+
$user->balance; // int(100)
32+
```
33+
34+
Это работает!

0 commit comments

Comments
 (0)