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
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::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);