Skip to main content

ArtinTech Solution

Prepaid wallet ledger in Linkhive

A balance column can tell you what a customer has, but not why. In Linkhive, our ISP billing platform, customers and resellers both hold prepaid money, so we built a small wallet ledger in Laravel that keeps every figure explainable.

One class changes money

No controller touches a balance. Everything goes through a Wallet class with credit and debit methods for customers and resellers, so the rules exist in one place.

Lock, calculate, write

Each change runs in a transaction, locks the owner’s row and works out the new balance from the locked value:

return DB::transaction(function () use ($customer, $type, $amount) {
    $locked = Customer::withTrashed()->lockForUpdate()->findOrFail($customer->id);
    $balance = round((float) $locked->balance + ($type === 'credit' ? $amount : -$amount), 2);

    if ($balance < 0) {
        throw new InsufficientBalance(__('Not enough balance.'));
    }

    $locked->forceFill(['balance' => $balance])->save();
    // ...write the ledger line with balance_after
});

The lock means two renewals at once cannot both spend the same money. Reading the balance from the locked row matters: a value loaded earlier in the request may already be stale.

Linkhive reseller wallet with balance, commission, customer count and a ledger of dated renewal charges
Every line shows the amount and the balance after it.

The ledger line is the audit trail

Each change writes one row with type, amount, balance after, description, source and who did it. Storing balance_after makes the ledger easy to read and easy to check: any line can be compared with the one before it. The source link connects a charge to the invoice that caused it.

Key takeaways

  • Put every balance change behind one class.
  • Lock the row, and calculate from the locked value.
  • Refuse negative balances inside the transaction.
  • Store the balance after each change.

See a reseller wallet in the Linkhive live demo or get Linkhive on Vibe96. Building something with stored balances? Let’s Talk, or see our Laravel development service.

Share this article