Skip to content
Merged

Docs #113

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ matrix:
- php: 'nightly'

before_script:
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d
- pecl install pcov
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- bash <(curl -s https://raw.githubusercontent.com/php-cache/cache/master/build/php/7.2/Memcached.sh)
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ laravel-wallet - Easy work with virtual wallet.
* **Vendor**: bavix
* **Package**: laravel-wallet
* **Version**: [![Latest Stable Version](https://poser.pugx.org/bavix/laravel-wallet/v/stable)](https://packagist.org/packages/bavix/laravel-wallet)
* **PHP Version**: 7.1+
* **PHP Version**: 7.2+
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.0`
* **[Composer](https://getcomposer.org/):** `composer require bavix/laravel-wallet`

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

### Usage
Add the `HasWallet` trait and `Wallet` interface to model.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"infection/infection": "^0.14",
"orchestra/testbench": "^4.0",
"phpstan/phpstan": "^0.11",
"phpunit/phpunit": "^8.2",
"phpunit/phpunit": "^8.3",
"laravel/cashier": "^7.0|^8.0|^9.0|^10.0"
},
"suggest": {
Expand Down
12 changes: 7 additions & 5 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [Transfer](transfer)
- [Refresh balance](refresh)
- [Confirm](confirm)
<!-- - [Exchange](exchange)-->
- [Exchange](exchange)
- [Withdraw taxing](taxing)

- Purchases
Expand All @@ -34,8 +34,10 @@
- [New Wallet](new-wallet)
- [Transfer](wallet-transfer)

<!--- Currencies-->
<!--
- Currencies

<!-- - [Rate Service](rate)-->
<!-- - [Create Wallet](rate-wallet)-->
<!-- - [Taxing](rate-taxing)-->
- [Rate Service](rate)
- [Create Wallet](rate-wallet)
- [Taxing](rate-taxing)
-->
124 changes: 122 additions & 2 deletions docs/exchange.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,124 @@
### Exchange

A description of this feature will appear later. Wait!
For now you can study to see the code in the unit test.
Everyone’s tasks are different and with the help of this functionality
you can add exchange rates to your wallets.

Currencies are configured in the general configuration file `config/wallet.php`.

```php
'currencies' => [
'xbtc' => 'BTC',
'dollar' => 'USD',
'ruble' => 'RUB',
],
```

The key in the configuration is the `slug` of your wallet.
Value, this is the currency of your wallet.

Service for working with currencies you need to write yourself or
use [library](https://github.com/bavix/laravel-wallet-swap).

#### Service for working with currency

We will write a simple service.
We will take the data from the array, and not from the database.

```php
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Services\WalletService;
use Illuminate\Support\Arr;

class MyRateService extends \Bavix\Wallet\Simple\Rate
{

// list of exchange rates (take from the database)
protected $rates = [
'USD' => [
'RUB' => 67.61,
],
'RUB' => [
'USD' => 0.0147907114,
],
];

protected function rate(Wallet $wallet): float
{
$from = app(WalletService::class)->getWallet($this->withCurrency);
$to = app(WalletService::class)->getWallet($wallet);

return Arr::get(
Arr::get($this->rates, $from->currency, []),
$to->currency,
1
);
}

public function convertTo(Wallet $wallet): float
{
return parent::convertTo($wallet) * $this->rate($wallet);
}

}

```

#### Service Registration

The service you wrote must be registered, this is done in the file `config/wallet.php`.

```php
return [
// ...
'package' => [
'rateable' => MyRateService::class,
// ...
],
// ...
];
```

#### Exchange process

Create two wallets.

```php
$usd = $user->createWallet([
'name' => 'My Dollars',
'slug' => 'dollar',
]);

$rub = $user->createWallet([
'name' => 'My Rub',
'slug' => 'ruble',
]);
```

We replenish the ruble wallet with 100 rubles.

```php
$rub->deposit(10000);
```

We will exchange rubles into dollars.

```php
$transfer = $rub->exchange($usd, 10000);
$rub->balance; // int(0)
$usd->balance; // int(147), это $1.47
```

Unfortunately, the world is not perfect. You will not get back your 100 rubles.

```php
$transfer = $usd->exchange($rub, $usd->balance);
$usd->balance; int(0)
$rub->balance; int(9938)
```

Due to conversion and mathematical rounding, you lost 62 kopecks.
You have 99 rubles 38 kopecks left.

---
It worked!

2 changes: 2 additions & 0 deletions docs/gift.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ $item->balance; // int(0)

The first user buys the product and gives it.

> If the product uses the `Taxable` interface, then Santa will pay tax

```php
$first->gift($last, $item);
(bool)$last->paid($item, true); // bool(true)
Expand Down
4 changes: 2 additions & 2 deletions docs/laravel-wallet-swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class User extends Model implements Wallet
```

### Simple example
Find wallets and translate from one to another.
Find wallets and exchange from one to another.

```php
$usd = $user->getWallet('usd');
Expand All @@ -34,7 +34,7 @@ $rub = $user->getWallet('rub');
$usd->balance; // int(200)
$rub->balance; // int(0)

$usd->exchange(10, $rub);
$usd->exchange($rub, 10);
$usd->balance; // int(190)
$rub->balance; // int(622)
```
Expand Down
1 change: 0 additions & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## PHP
The following versions of PHP are supported:

- PHP 7.1
- PHP 7.2
- PHP 7.3

Expand Down
19 changes: 18 additions & 1 deletion docs/ru/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Введение](README)
- [Требования](requirements)
- [Установка](installation)
- [Установка в Lumen](lumen)
- [Как использовать](basic-usage)
- [Обновление](upgrade-guide)

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

- Покупки

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

- Дополнения

- [Wallet Swap](laravel-wallet-swap)

- Кошельки

- [Создать кошелек](new-wallet)
- [Перевод](wallet-transfer)

<!--
- Currencies

- [Rate Service](rate)
- [Create Wallet](rate-wallet)
- [Taxing](rate-taxing)
-->
34 changes: 34 additions & 0 deletions docs/ru/confirm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## User Model

Добавьте `CanConfirm` trait и `Confirmable` interface в модель User.

```php
use Bavix\Wallet\Interfaces\Confirmable;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Traits\CanConfirm;
use Bavix\Wallet\Traits\HasWallet;

class UserConfirm extends Model implements Wallet, Confirmable
{
use HasWallet, CanConfirm;
}
```

### Example:

Иногда, необходимо подтвердить операцию и пересчитать баланс.
Теперь это доступно в библиотеке из коробки. Вот пример:

```php
$user->balance; // int(0)
$transaction = $user->deposit(100, null, false); // не подтверждена
$transaction->confirmed; // bool(false)
$user->balance; // int(0)

$user->confirm($transaction); // bool(true)
$transaction->confirmed; // bool(true)

$user->balance; // int(100)
```

Это работает!
Loading