Example #1
0
function user_self_balance()
{
    lets_use('billing_balance', 'billing_account');
    $userId = user_self_id();
    core_log('user_id: ' . $userId, __FUNCTION__);
    if (!$userId) {
        return 0;
    }
    $account = billing_account_get_user_main_account($userId);
    return billing_balance_get_account_amount($account);
}
Example #2
0
function billing_balance_process_move($accountFrom, $accountTo, $sum, $trId)
{
    $incomingFromAccountAmount = billing_balance_get_account_amount($accountFrom);
    $incomingToAccountAmount = billing_balance_get_account_amount($accountTo);
    $accountFromUpdate = billing_balance_set_account_amount($accountFrom, $incomingFromAccountAmount - $sum);
    if (!$accountFromUpdate) {
        return false;
    }
    $accountToUpdate = billing_balance_set_account_amount($accountTo, $incomingToAccountAmount + $sum);
    if (!$accountToUpdate) {
        return false;
    }
    return true;
}
Example #3
0
function web_controller_billing_refill()
{
    lets_use('web_router', 'billing_balance', 'billing_account', 'billing_transaction', 'billing_locks', 'user_self');
    $incomingSource = 1;
    // благотоврительный фонд
    $sum = (double) web_router_get_param('sum');
    $sum = round($sum, 2);
    $accountFrom = billing_account_get_income_account($incomingSource);
    $accountTo = billing_account_get_user_main_account(user_self_id());
    $trId = billing_transaction_register($accountFrom, $accountTo, $sum);
    if (!$trId) {
        // cant register transaction
        billing_transaction_fail($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'Ошибка сервера, повторите позже.']);
        return;
    }
    $lockRes = billing_locks_lock_transaction($trId, [$accountFrom, $accountTo]);
    if (!$lockRes) {
        // cant lock transaction
        billing_transaction_fail($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'В данный момент операция невозможна, повторите позже']);
        return;
    }
    $movementPossible = billing_balance_check_sum_available($accountFrom, $sum);
    if (!$movementPossible) {
        // not enough money
        billing_transaction_fail($trId);
        billing_locks_unlock_transaction($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'На исходящем счете недостаточно денег']);
        return;
    }
    $dbTransactionLock = billing_balance_storage_transaction_start();
    if (!$dbTransactionLock) {
        // cant begin db transaction
        billing_transaction_fail($trId);
        billing_locks_unlock_transaction($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'Не удалось начать транзакцию']);
        return;
    }
    $moveRes = billing_balance_process_move($accountFrom, $accountTo, $sum, $trId);
    if (!$moveRes) {
        // cant move money
        billing_balance_storage_transaction_rollback();
        billing_transaction_fail($trId);
        billing_locks_unlock_transaction($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'Не удалось перевести деньги']);
        return;
    }
    $transactionCommit = billing_balance_storage_transaction_commit();
    if ($transactionCommit) {
        // cant commit db transaction
        billing_transaction_fail($trId);
        billing_locks_unlock_transaction($trId);
        web_router_render_page('billing', 'refill', ['result' => false, 'msg' => 'Не удалось завершить транзакцию']);
        return;
    }
    billing_transaction_success($trId);
    billing_locks_unlock_transaction($trId);
    $moneyNow = billing_balance_get_account_amount($accountTo);
    web_router_render_page('billing', 'refill', ['result' => true, 'msg' => $sum . ' денежных единиц успешно переведены вам на счет. На вашем счету теперь: ' . $moneyNow]);
}