1. Go to this page and download the library: Download yii2tech/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/ */
Yii::$app->balanceManager->transfer($fromAccount, $toAccount, 100); // assume this is first time accounts are affected
echo Yii::$app->balanceManager->calculateBalance($fromAccount); // outputs: -100
echo Yii::$app->balanceManager->calculateBalance($toAccount); // outputs: 100
use yii\db\Query;
Yii::$app->balanceManager->transfer($fromAccountId, $toAccountId, 100); // assume this is first time accounts are affected
$currentBalance = (new Query())
->select(['balance'])
->from('BalanceAccount')
->andWhere(['id' => $fromAccountId])
->scalar();
echo $currentBalance; // outputs: -100
// simple increase :
Yii::$app->balanceManager->increase(
[
'userId' => Yii::$app->user->id,
'type' => 'virtual-money',
],
100,
// extra data associated with transaction :
[
'paymentGateway' => 'PayPal',
'paymentId' => 'abcxyzerft',
]
);
// transfer :
Yii::$app->balanceManager->transfer(
[
'userId' => Yii::$app->user->id,
'type' => 'payment-gateway',
],
[
'userId' => Yii::$app->user->id,
'type' => 'virtual-money',
],
100,
// extra data associated with transaction :
[
'paymentGateway' => 'PayPal',
'paymentId' => 'abcxyzerft',
]
);
use yii2tech\balance\Manager;
use yii2tech\balance\ManagerDb;
$manager = new ManagerDb();
$manager->on(Manager::EVENT_BEFORE_CREATE_TRANSACTION, function ($event) {
$event->transactionData['amount'] += 10; // you may adjust transaction data to be saved, including transaction amount
$event->transactionData['comment'] = 'adjusted by event handler';
});
$manager->on(Manager::EVENT_AFTER_CREATE_TRANSACTION, function ($event) {
echo 'new transaction: ' $event->transactionId; // you may get newly created transaction ID
});
$manager->increase(1, 100); // outputs: 'new transaction: 1'
echo Yii::$app->balanceManager->calculateBalance(1); // outputs: 110
php composer.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.