Skip to main content

Top-up Balance & Credit Management

Sometimes you might want to allow your users to top up their account balance with credits, which they can use to pay for the services provided by your application. This is a common requirement for many SaaS applications.

To implement this, you can use the handy Laravel Wallet package, which provides a simple way to manage user balances and transactions, in combination with the events that come with SaaSykit.

So for example, once a user purchases a product or subscribes to a plan, you can listen to the event that's triggered by SaaSykit and add credits to their account balance.

Unfortunately, the Laravel Wallet package introduces a "transactions" table that conflicts with the one used by SaaSykit. Luckily, the Laravel Wallet package allows us to customize the table names it uses, so we can avoid this conflict.

In the following section, we will guide you through the steps to set up the Laravel Wallet package and configure it to work seamlessly with SaaSykit.

Installationโ€‹

  1. To install the Laravel Wallet package, you can run the following command:
composer require bavix/laravel-wallet
  1. After installing the package, you need to publish its configuration file by running:
php artisan vendor:publish --tag=laravel-wallet-config
  1. Now, create a new model under the app/Models directory called WalletTransaction and extend it from the Bavix\Wallet\Models\Transaction class:
<?php

namespace App\Models;

class WalletTransaction extends \Bavix\Wallet\Models\Transaction
{

}
  1. Next, you need to update the config/wallet.php file to use the new WalletTransaction model and change the table names to avoid conflicts with SaaSykit:
// some other configurations...

'transaction' => [
/**
* The table name for transactions.
*
* This value is used to store transactions in a database.
*
* @see Transaction
*/
'table' => env('WALLET_TRANSACTION_TABLE_NAME', 'wallet_transactions'), // <--- use a different table name

/**
* The model class for transactions.
*
* This value is used to create new transactions.
*
* @see Transaction
*/
'model' => \App\Models\WalletTransaction::class, // <--- use the new model
],
// some other configurations...
  1. Finally, you need to run the migrations to create the necessary tables in your database:
php artisan migrate

Usageโ€‹

Now that you have installed and configured the Laravel Wallet package, you can start using it to manage user balances and transactions.

  1. Add the HasWallet trait to your User model:
<?php

use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Traits\HasWallet;

class User extends Authenticatable implements FilamentUser, MustVerifyEmail, TwoFactorAuthenticatable, Wallet // <--- implement the Wallet interface
{
use HasApiTokens, HasFactory, HasRoles, Notifiable, TwoFactorAuthentication;
use HasWallet; // <--- use the HasWallet trait

// other methods and properties...
}
  1. Now because the Wallet interface uses the transactions table to store transactions, you need to remove the "transactions()" function from the User model because it conflicts with its definition, and revise where it's being used in the application.

  2. That's it! You can now use the deposit() method to add credits to the user's account balance:

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

// Add 100 credits to the user's account balance

$user->deposit(100);
  1. You can also use the withdraw() method to deduct credits from the user's account balance:
// Deduct 50 credits from the user's account balance
$user->withdraw(50);
  1. To check the user's account balance, you can use the balance property:
$balance = $user->balance; // This will return the user's account balance

You can use the deposit() and withdraw() methods in your application wherever you need to add or deduct credits from the user's account balance, such as when they purchase a product or subscribe to a plan in combination with the events that come with SaaSykit.