PHP code example of omacy / ledgerbeetle

1. Go to this page and download the library: Download omacy/ledgerbeetle 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/ */

    

omacy / ledgerbeetle example snippets


use LedgerBeetle\Laravel\Enums\AccountType;
use LedgerBeetle\Laravel\Facades\Ledger;
use LedgerBeetle\Laravel\Support\Money;

// Liabilities (e.g. customer wallets) are overdraft-protected by default.
Ledger::createAccount('BANK.USD', 'Bank', AccountType::ASSET, 'USD');
Ledger::createAccount('WALLET.42', 'Wallet #42', AccountType::LIABILITY, 'USD');
Ledger::createAccount('WALLET.77', 'Wallet #77', AccountType::LIABILITY, 'USD');

// Deposit: debit the bank, credit the wallet.
Ledger::transfer('BANK.USD', 'WALLET.42', Money::parse('100.00', 'USD'), idempotencyKey: 'deposit-9f2c');

// Payment. Throws InsufficientFunds if it would overdraw the wallet.
$payment = Ledger::transfer('WALLET.42', 'WALLET.77', 25_00, idempotencyKey: 'order-1001', reference: 'order-1001');

Ledger::balance('WALLET.42');   // Balance { posted: 7500, available: 7500, ... } in minor units

// Hold, then capture part of it. The rest returns to the payer.
$hold = Ledger::hold('WALLET.42', 'WALLET.77', 50_00, timeoutSeconds: 86400, idempotencyKey: 'hotel-7');
Ledger::capture($hold, 30_00);       // or Ledger::release($hold);

// Partial refund.
Ledger::reverse($payment, 10_00, idempotencyKey: 'refund-1001-a', reason: 'damaged item');

// Payment plus fee: both legs post or neither does.
Ledger::transferMany([
    ['from' => 'WALLET.42', 'to' => 'WALLET.77', 'amount' => 20_00],
    ['from' => 'WALLET.42', 'to' => 'REVENUE.FEES.USD', 'amount' => 50],
], idempotencyKey: 'checkout-55');

$conversion = Ledger::convert('WALLET.42', 'WALLET.42.EUR', Money::parse('50.00', 'USD'),
    idempotencyKey: 'fx-881', fee: 1_00, feeAccount: 'REVENUE.FEES.USD');   // rate from ExchangeRates

Ledger::convert('WALLET.42', 'WALLET.42.EUR', 50_00, rate: '0.9215', idempotencyKey: 'fx-882');  // explicit rate

Ledger::reverseConversion($conversion);   // every leg backwards at the original rate, once

Ledger::balanceAt('WALLET.42', now()->subMonth()->endOfMonth());   // Balance as it stood then
Ledger::balanceHistory('WALLET.42', from: now()->subWeek());          // BalanceSnapshot per transfer, newest first

// One TigerBeetle request. Items succeed or fail independently; failures are returned, not thrown.
$results = Ledger::transferBatch([
    ['from' => 'WALLET.42', 'to' => 'WALLET.77', 'amount' => 5_00, 'idempotency_key' => 'payout-1'],
    ['from' => 'WALLET.43', 'to' => 'WALLET.77', 'amount' => 7_00, 'idempotency_key' => 'payout-2'],
]);   // ['payout-1' => LedgerTransfer, 'payout-2' => InsufficientFunds, ...]

// Or off the request path: queues PostTransferBatch, then fires TransferBatchPosted.
$batchId = Ledger::queueBatch($items);

// config/database.php
'connections' => [
    'mysql' => [/* ... */],
    'ledger' => [/* same database as mysql */],
],
bash
composer vendor:publish --tag=ledger-config
php artisan migrate