Wallet logo Wallet

Transfer between wallets

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

Prepare the model, add the HasWallet, HasWallets trait's and Wallet interface.

use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Interfaces\Wallet;

class User extends Model implements Wallet
{
    use HasWallet, HasWallets;
}

Make a Transfer

Find user:

$first = User::first(); 
$last = User::orderBy('id', 'desc')->first(); // last user
$first->getKey() !== $last->getKey(); // true

Create new wallets for users.

$name = 'New Wallet';
$firstWallet = $first->createWallet(compact('name'));
$secondWallet = $second->createWallet(compact('name'));

$firstWallet->deposit(100);
$firstWallet->balance; // int(100)
$secondWallet->balance; // int(0)

The transfer will be from the first user to the second.

$firstWallet->transfer($secondWallet, 5); 
$firstWallet->balance; // int(95)
$secondWallet->balance; // int(5)

It worked!

Force Transfer

Check the user's balance.

$firstWallet->balance; // int(100)
$lastWallet->balance; // int(0)

The transfer will be from the first user to the second.

$firstWallet->forceTransfer($secondWallet, 500); 
$firstWallet->balance; // int(-400)
$secondWallet->balance; // int(500)

It worked!