hpwebdeveloper/laravel-pay-pocket
Laravel Pay Pocket adds virtual wallets to Laravel using simple wallets and wallet_logs tables. Manage multiple wallet types, deposit, withdraw/pay, check balances, and record logs with clear APIs and exceptions—built for in-app money, not payment gateways.
Laravel Pay Pocket is a package designed for Laravel applications, offering the flexibility to manage multiple wallet types within two dedicated database tables, wallets and wallets_logs.
Demo: Link to the demo in Laravel 12
Videos:
Note: This package does not handle payments from payment platforms, but instead offers the concept of virtual money, deposit, and withdrawal.
2.xcomposer require hpwebdeveloper/laravel-pay-pocket| Package Version | Laravel Versions | PHP Versions | Status |
|---|---|---|---|
| 1.x | 10.x | 8.1+ | Security fixes only |
| 2.x | 10.x, 11.x, 12.x | 8.1+ | Active support |
Note: The package follows Laravel's philosophy of being as permissive as possible with PHP versions. Your application's composer.json will enforce the minimum PHP version required by your Laravel version (10.x requires PHP 8.1+, 11.x and 12.x require PHP 8.2+).
composer require hpwebdeveloper/laravel-pay-pocket
php artisan vendor:publish --tag="pay-pocket-migrations"
php artisan migrate
You have successfully added two dedicated database tables, wallets and wallets_logs, without making any modifications to the users table.
php artisan vendor:publish --tag="pay-pocket-wallets"
php artisan vendor:publish --tag="config"
This command will automatically publish the pay-pocket.php config file and also WalletEnums.php file into your application's config and app/Enums directories respectively.
If updating to version ^2.0.0, new migration and config files have been added to support the new Transaction Notes Feature
Run the following commands to publish the updated migrations and config:
php artisan vendor:publish --tag="pay-pocket-migrations"
php artisan migrate
php artisan vendor:publish --tag="pay-pocket-wallets"
php artisan vendor:publish --tag="config"
To use this package you need to implement the WalletOperations into User model and utilize the ManagesWallet trait.
use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;
use HPWebdeveloper\LaravelPayPocket\Traits\ManagesWallet;
class User extends Authenticatable implements WalletOperations
{
use ManagesWallet;
}
In Laravel Pay Pocket, you have the flexibility to define the order in which wallets are prioritized for payments through the use of Enums. The order of wallets in the Enum file determines their priority level. The first wallet listed has the highest priority and will be used first for deducting order values.
For example, consider the following wallet types defined in the Enum class (published in step 3 of installation):
namespace App\Enums;
enum WalletEnums: string
{
case WALLET1 = 'wallet_1';
case WALLET2 = 'wallet_2';
}
You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.
In this particular setup, wallet_1 (WALLET1) is given the highest priority. When an order payment is processed, the system will first attempt to use wallet_1 to cover the cost. If wallet_1 does not have sufficient funds, wallet_2 (WALLET2) will be used next.
If the balance in wallet_1 is 10 and the balance in wallet_2 is 20, and you need to pay an order value of 15, the payment process will first utilize the entire balance of wallet_1. Since wallet_1's balance is insufficient to cover the full amount, the remaining 5 will be deducted from wallet_2. After the payment, wallet_2 will have a remaining balance of 15."
deposit(type: 'wallet_1', amount: 123.45, notes: null)
Deposit funds into wallet_1
$user = auth()->user();
$user->deposit('wallet_1', 123.45);
Deposit funds into wallet_2
$user = auth()->user();
$user->deposit('wallet_2', 67.89);
Or using provided facade
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;
$user = auth()->user();
LaravelPayPocket::deposit($user, 'wallet_1', 123.45);
Note: wallet_1 and wallet_2 must already be defined in the WalletEnums.
When you need to add descriptions for a specific transaction, the $notes parameter enables you to provide details explaining the reason behind the transaction.
$user = auth()->user();
$user->deposit('wallet_1', 67.89, 'You ordered pizza.');
pay(amount: 12.34, notes: null)
Pay the value using the total combined balance available across all allowed wallets
$user = auth()->user();
$user->pay(12.34);
Or using provided facade
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;
$user = auth()->user();
LaravelPayPocket::pay($user, 12.34);
The pay() method returns a collection of WalletsLog instances representing all wallet transactions that occurred during the payment. This enables you to track exactly which wallets were used and access detailed transaction information.
Return Value:
@return \Illuminate\Database\Eloquent\Collection<WalletsLog>
Basic Usage:
$user = auth()->user();
$logs = $user->pay(120.00, 'Order #1234');
// Access transaction details
foreach ($logs as $log) {
echo "Wallet: {$log->wallet_name}, Amount: {$log->value}";
}
Practical Examples:
// Get the number of wallets used in the payment
$walletCount = $logs->count();
// Calculate total amount deducted (verification)
$totalDeducted = $logs->sum('value');
// Get all wallet names used in the transaction
$walletsUsed = $logs->pluck('wallet_name');
// Access specific log details
$firstLog = $logs->first();
echo "From: {$firstLog->from}, To: {$firstLog->to}";
echo "Reference: {$firstLog->reference}";
Use Cases:
Example: Multi-Wallet Payment
$user = auth()->user();
// User has: wallet_1 = $100, wallet_2 = $50
$logs = $user->pay(120.00, 'Premium subscription');
// Returns collection with 2 logs:
// Log 1: wallet_1 deducted $100 (100.00 -> 0.00)
// Log 2: wallet_2 deducted $20 (50.00 -> 30.00)
echo "Payment completed using {$logs->count()} wallet(s)";
// Output: Payment completed using 2 wallet(s)
Note: This feature is backward compatible. Existing code that doesn't capture the return value will continue to work without any modifications.
$user->walletBalance // Total combined balance available across all wallets
// Or using provided facade
LaravelPayPocket::checkBalance($user);
$user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1
$user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2
// Or using provided facade
LaravelPayPocket::walletBalanceByType($user, 'wallet_1');
Upon examining the src/Exceptions directory within the source code,
you will discover a variety of exceptions tailored to address each scenario of invalid entry. Review the demo that accounts for some of the exceptions.
A typical wallets_logs table.
composer install
composer test
# Or
./vendor/bin/pest
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?