Transfer
Transfer in our system are two well-known Deposit and Withdraw operations that are performed in one transaction.
The transfer takes place between wallets.
User Model
It is necessary to expand the model that will have the wallet. This is done in two stages:
- Add
Walletinterface; - Add the
HasWallettrait;
Let's get started.
php
use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Wallet;
class User extends Model implements Wallet
{
use HasWallet;
}The model is prepared to work with a wallet.
Example contract
php
$transfer = $user1->transfer(
$user2,
511,
new Extra(
deposit: [
'type' => 'extra-deposit',
],
withdraw: new Option(
[
'type' => 'extra-withdraw',
],
false // confirmed
),
extra: [
'msg' => 'hello world',
],
)
);Make a Transfer
Find user:
php
$first = User::first();
$last = User::orderBy('id', 'desc')->first(); // last user
$first->getKey() !== $last->getKey(); // trueAs the user uses HasWallet, he will have balance property. Check the user's balance.
php
$first->balance; // 100
$last->balance; // 0The transfer will be from the first user to the second.
php
$first->transfer($last, 5);
$first->balance; // 95
$last->balance; // 5It's simple!
Force Transfer
Check the user's balance.
php
$first->balance; // 100
$last->balance; // 0The transfer will be from the first user to the second.
php
$first->forceTransfer($last, 500);
$first->balance; // -400
$last->balance; // 500It's simple!