Transfer in our system are two well-known Deposit and Withdraw operations that are performed in one transaction.
The transfer takes place between wallets.
Prepare the model, add the HasWallets trait's and Wallet interface.
use Zotel\Wallet\Traits\HasWallets;
use Zotel\Wallet\Interfaces\Wallet;
class User extends Model implements Wallet
{
use HasWallets;
}
Find user:
$first = User::first();
$last = User::orderBy('id', 'desc')->first(); // last user
$first->getKey() !== $last->getKey(); // true
Create new wallets for users.
$firstWallet = $first->createWallet(['name' => 'First User Wallet']);
$lastWallet = $last->createWallet(['name' => 'Second User Wallet']);
$firstWallet->deposit(100);
$firstWallet->balance; // 100
$lastWallet->balance; // 0
The transfer will be from the first user to the last.
$firstWallet->transfer($lastWallet, 5);
$firstWallet->balance; // 95
$lastWallet->balance; // 5
It's simple!
Check the user's balance.
$firstWallet->balance; // 100
$lastWallet->balance; // 0
The transfer will be from the first user to the second.
$firstWallet->forceTransfer($lastWallet, 500);
$firstWallet->balance; // -400
$lastWallet->balance; // 500
It worked!
Check the user's balance.
$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 0
We will execute the transfer, but without confirmation of the withdrawal of funds.
use Zotel\Wallet\External\Dto\Extra;
use Zotel\Wallet\External\Dto\Option;
/** [@var](https://github.com/var) $firstWallet \Zotel\Wallet\Interfaces\Wallet */
$transfer = $firstWallet->transfer($secondWallet, 500, new Extra(
deposit: ['message' => 'Hello, secondWallet!'],
withdraw: new Option(meta: ['something' => 'anything'], confirmed: false)
));
$transfer->withdraw->meta; // ['something' => 'anything']
$transfer->withdraw->confirmed; // false
$transfer->deposit->meta; // ['message' => 'Hello, secondWallet!']
$transfer->deposit->confirmed; // true
$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 500
It's simple!
How can I help you explore Laravel packages today?