PHP code example of illuminatech / balance

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

    

illuminatech / balance example snippets




use Illuminate\Container\Container;
use App\Http\Controllers\Controller;
use Illuminatech\Balance\BalanceContract;

class BalanceController extends Controller
{
    public function increase(BalanceContract $balance, $accountId, $amount)
    {
        $balance->increase($accountId, $amount);
        
        // ...
    }
    
    public function decrease($accountId, $amount)
    {
        $balance = Container::getInstance()->get(BalanceContract::class);
        
        $balance->decrease($accountId, $amount);
        
        // ...
    }
    
    // ...
}



use Illuminatech\Balance\Facades\Balance;

Balance::increase($accountId, $amount);



use Illuminatech\Balance\Facades\Balance;

Balance::increase($accountId, 500); // add 500 credits to account



use Illuminatech\Balance\Facades\Balance;

Balance::decrease($accountId, 100); // remove 100 credits from account



use Illuminatech\Balance\Facades\Balance;

$fromId = 1;
$toId = 2;
Balance::transfer($fromId, $toId, 100); // remove 100 credits from account 1 and add 100 credits to account 2



use Illuminatech\Balance\Facades\Balance;

Balance::revert($transactionId);



use Illuminatech\Balance\Facades\Balance;

$user = request()->user();

Balance::transfer(
    [
        'userId' => $user->id,
        'type' => 'virtual-money',
    ],
    [
        'userId' => $user->id,
        'type' => 'purchases',
    ],
    500
);



use Illuminatech\Balance\Facades\Balance;

Balance::transfer($fromAccount, $toAccount, 100); // assume this is first time accounts are affected

echo Balance::calculateBalance($fromAccount); // outputs: -100
echo Balance::calculateBalance($toAccount); // outputs: 100



use Illuminate\Support\Facades\DB;
use Illuminatech\Balance\Facades\Balance;

Balance::transfer($fromAccountId, $toAccountId, 100); // assume this is first time accounts are affected

$currentBalance = DB::table('balance_accounts')
    ->select(['balance'])
    ->where(['id' => $fromAccountId])
    ->value('balance');

echo $currentBalance; // outputs: -100



use Illuminatech\Balance\Facades\Balance;

$user = request()->user();

// simple increase :
Balance::increase(
    [
        'userId' => $user->id,
        'type' => 'virtual-money',
    ],
    100,
    // extra data associated with transaction :
    [
        'paymentGateway' => 'PayPal',
        'paymentId' => 'abcxyzerft',
    ]
);

// transfer :
Balance::transfer(
    [
        'userId' => $user->id,
        'type' => 'payment-gateway',
    ],
    [
        'userId' => $user->id,
        'type' => 'virtual-money',
    ],
    100,
    // extra data associated with transaction :
    [
        'paymentGateway' => 'PayPal',
        'paymentId' => 'abcxyzerft',
    ]
);



use Illuminate\Support\Facades\DB;
use Illuminatech\Balance\Facades\Balance;

$accountId = 1;

$lastTransactionQuery = DB::table('balance_transactions')
    ->where(['account_id' => $accountId])
    ->orderBy('id', 'DESC');

Balance::increase($accountId, 50); // assume this is first time accounts is affected
$lastTransaction = $lastTransactionQuery->first();
echo $lastTransaction->new_balance; // outputs: 50

Balance::increase($accountId, 25);
$lastTransaction = $lastTransactionQuery->first();
echo $lastTransaction->new_balance; // outputs: 75

Balance::decrease($accountId, 50);
$lastTransaction = $lastTransactionQuery->first();
echo $lastTransaction->new_balance; // outputs: 25



use Illuminate\Support\Facades\Event;
use Illuminatech\Balance\Facades\Balance;
use Illuminatech\Balance\Events\TransactionCreated;
use Illuminatech\Balance\Events\CreatingTransaction;

Event::listen(CreatingTransaction::class, function (CreatingTransaction $event) {
    $event->data['amount'] += 10; // you may adjust transaction data to be saved, including transaction amount
    $event->data['comment'] = 'adjusted by event handler';
});

Event::listen(TransactionCreated::class, function (TransactionCreated $event) {
    echo 'new transaction: '.$event->transactionId; // you may get newly created transaction ID
});

Balance::increase(1, 100); // outputs: 'new transaction: 1'
echo Balance::calculateBalance(1); // outputs: 110

php composer.phar 

php artisan vendor:publish --provider="Illuminatech\Balance\BalanceServiceProvider" --tag=config

php artisan vendor:publish --provider="Illuminatech\Balance\BalanceServiceProvider" --tag=migrations