PHP code example of ssbityukov / laravel-balance-engine

1. Go to this page and download the library: Download ssbityukov/laravel-balance-engine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ssbityukov / laravel-balance-engine example snippets


$user->balance += 100;
$user->save();

Balance::deposit(to: $user, amount: 10_000);
Balance::withdraw(from: $user, amount: 5_000);
Balance::transfer(from: $alice, to: $bob, amount: 10_000);
$reservation = Balance::reserve(from: $buyer, amount: 10_000);

Relation::enforceMorphMap([
    'user' => User::class,
    'team' => Team::class,
]);

use Bityukov\BalanceEngine\Concerns\HasBalance;

class User extends Model
{
    use HasBalance;
}

$user->balanceAmount();     // available, in minor units
$user->balanceReserved();   // held by open reservations
$user->balanceAccount();    // the Account model itself

Balance::deposit(to: $user, amount: 10_000);
Balance::withdraw(from: $user, amount: 5_000);

Balance::transfer(from: $alice, to: $bob, amount: 10_000);

$reservation = Balance::reserve(
    from: $buyer,
    amount: 6_000,
    expiresAt: now()->addMinutes(30),
);

$reservation->capture(to: $seller);              // all of it
$reservation->capture(to: $seller, amount: 1_000); // part of it, stays open
$reservation->release();                          // hand the rest back
$reservation->release(amount: 2_000);             // hand part of it back

$reservation->remaining();  // int
$reservation->captured();   // int
$reservation->isOpen();     // bool
$reservation->status();     // ReservationStatus

$uuid = Balance::reserve(from: $buyer, amount: 6_000)->uuid();

// Later, in another request:
Balance::reservation($uuid)->capture(to: $seller);

Schedule::command('balance:expire-reservations')->everyFiveMinutes();

Balance::reverse($transaction, meta: ['reason' => 'chargeback']);

Balance::freeze($user, reason: 'aml-review');
Balance::unfreeze($user);

Balance::deposit(
    to: $user,
    amount: 10_000,
    idempotencyKey: "stripe:{$event->id}",
);

$user->balanceAccount('bonus');
$user->balanceAccount('main', 'EUR');

Balance::deposit(to: $user->balanceAccount('bonus'), amount: 2_000);

Schedule::command('balance:expire-reservations')->everyFiveMinutes();
Schedule::command('balance:verify')->daily();
bash
php artisan balance:verify
bash
composer balance:install
php artisan migrate