PHP code example of nishant / wallet

1. Go to this page and download the library: Download nishant/wallet 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/ */

    

nishant / wallet example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Nishant\Wallet\Traits\HasWallets;

class User extends Authenticatable
{
    use HasWallets;

    // ... rest of your User model
}

use App\Models\User;

$user = User::find(1);

// Create a new wallet
$wallet = $user->createWallet('Main Wallet', 'Primary wallet for transactions');

// Create another wallet
$savingsWallet = $user->createWallet('Savings', 'Savings account');

use Nishant\Wallet\Services\WalletService;

$walletService = app(WalletService::class);

$wallet = $walletService->createWallet(
    userId: 1,
    name: 'Main Wallet',
    description: 'Primary wallet'
);

$wallet = $user->getWalletByName('Main Wallet');

// Immediate deposit (confirmed by default)
$transaction = $wallet->deposit(
    amount: 100.00,
    reference: 'DEP-001',
    description: 'Initial deposit',
    meta: ['source' => 'bank_transfer']
);

// Pending deposit (not confirmed - balance won't change until confirmed)
$pendingTransaction = $wallet->deposit(
    amount: 100.00,
    reference: 'DEP-002',
    description: 'Pending deposit',
    meta: null,
    confirmed: false
);

// By wallet ID - confirmed deposit
$transaction = $walletService->deposit(
    walletId: $wallet->id,
    amount: 100.00,
    reference: 'DEP-001',
    description: 'Initial deposit',
    meta: null,
    confirmed: true  // default is true
);

// By wallet ID - pending deposit
$pendingTransaction = $walletService->deposit(
    walletId: $wallet->id,
    amount: 100.00,
    reference: 'DEP-002',
    description: 'Pending deposit',
    meta: null,
    confirmed: false  // balance won't change
);

// By wallet name
$transaction = $walletService->depositByName(
    userId: 1,
    walletName: 'Main Wallet',
    amount: 100.00,
    reference: null,
    description: null,
    meta: null,
    confirmed: true
);

$wallet = $user->getWalletByName('Main Wallet');

try {
    // Immediate withdrawal (confirmed by default)
    $transaction = $wallet->withdraw(
        amount: 50.00,
        reference: 'WD-001',
        description: 'Payment for service',
        meta: ['recipient' => 'vendor-123']
    );

    // Pending withdrawal (not confirmed - balance won't change until confirmed)
    $pendingTransaction = $wallet->withdraw(
        amount: 50.00,
        reference: 'WD-002',
        description: 'Pending withdrawal',
        meta: null,
        confirmed: false
    );
} catch (\Exception $e) {
    // Handle insufficient balance or other errors
    echo $e->getMessage();
}

// By wallet ID - confirmed withdrawal
$transaction = $walletService->withdraw(
    walletId: $wallet->id,
    amount: 50.00,
    reference: null,
    description: null,
    meta: null,
    confirmed: true  // default is true
);

// By wallet ID - pending withdrawal
$pendingTransaction = $walletService->withdraw(
    walletId: $wallet->id,
    amount: 50.00,
    reference: 'WD-002',
    description: 'Pending withdrawal',
    meta: null,
    confirmed: false  // balance won't change
);

// By wallet name
$transaction = $walletService->withdrawByName(
    userId: 1,
    walletName: 'Main Wallet',
    amount: 50.00,
    reference: null,
    description: null,
    meta: null,
    confirmed: true
);

$wallet = $user->getWalletByName('Main Wallet');
$transactions = $wallet->transactions;

// Using the trait
$transactions = $user->getTransactionsByWalletName('Main Wallet');

// Using the service
$transactions = $walletService->getTransactionsByWalletName(
    userId: 1,
    walletName: 'Main Wallet'
);

$deposits = $wallet->getTransactionsByType('deposit');
$withdrawals = $wallet->getTransactionsByType('withdraw');

use Nishant\Wallet\Services\WalletService;

$walletService = app(WalletService::class);

// Confirm a pending transaction
$confirmedTransaction = $walletService->confirmTransaction($transactionId);

// The wallet balance will now be updated based on the transaction type
// - For deposits: balance increases
// - For withdrawals: balance decreases (if sufficient balance exists)

$transaction = Transaction::find($transactionId);

if ($transaction->confirmed) {
    // Transaction is confirmed, balance has been updated
} else {
    // Transaction is pending, balance has not been updated
}

// Get all wallets
$wallets = $user->wallets;

// Get active wallets only
$activeWallets = $user->activeWallets();

// Get wallet by name
$wallet = $user->getWalletByName('Main Wallet');

// Get wallet balance
$balance = $wallet->getBalance();

// Check if wallet has sufficient balance
if ($wallet->hasBalance(100.00)) {
    // Proceed with transaction
}

// Get total balance across all wallets
$totalBalance = $user->getTotalBalance();

use Nishant\Wallet\Contracts\WalletInterface;

interface WalletInterface
{
    public function getBalance(): float;
    public function deposit(float $amount, ?string $reference = null, ?string $description = null, ?array $meta = null, bool $confirmed = true);
    public function withdraw(float $amount, ?string $reference = null, ?string $description = null, ?array $meta = null, bool $confirmed = true);
    public function hasBalance(float $amount): bool;
    public function transactions();
    public function getTransactionsByType(string $type);
}

use Nishant\Wallet\Contracts\TransactionInterface;

interface TransactionInterface
{
    public function getType(): string;
    public function getAmount(): float;
    public function getBalanceBefore(): float;
    public function getBalanceAfter(): float;
    public function getReference(): ?string;
    public function getDescription(): ?string;
    public function getMeta(): ?array;
    public function isConfirmed(): bool;
}

use Nishant\Wallet\Traits\HasWallets;

class User extends Model
{
    use HasWallets;
}

try {
    $transaction = $wallet->withdraw(100.00);
} catch (\Exception $e) {
    // Handle error
    logger()->error('Wallet operation failed: ' . $e->getMessage());
}

// Confirming transactions
try {
    $confirmedTransaction = $walletService->confirmTransaction($transactionId);
} catch (\Exception $e) {
    // Handle error (e.g., insufficient balance, already confirmed, etc.)
    logger()->error('Transaction confirmation failed: ' . $e->getMessage());
}

use Tests\TestCase;
use App\Models\User;
use Nishant\Wallet\Traits\HasWallets;

class WalletTest extends TestCase
{
    public function test_can_create_wallet()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        
        $this->assertNotNull($wallet);
        $this->assertEquals('Test Wallet', $wallet->name);
    }

    public function test_can_deposit_amount()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        
        $transaction = $wallet->deposit(100.00);
        
        $this->assertEquals(100.00, $wallet->fresh()->balance);
        $this->assertEquals('deposit', $transaction->type);
    }

    public function test_can_withdraw_amount()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        $wallet->deposit(100.00);
        
        $transaction = $wallet->withdraw(50.00);
        
        $this->assertEquals(50.00, $wallet->fresh()->balance);
        $this->assertEquals('withdraw', $transaction->type);
    }

    public function test_cannot_withdraw_insufficient_balance()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        $wallet->deposit(50.00);
        
        $this->expectException(\Exception::class);
        $wallet->withdraw(100.00);
    }

    public function test_can_create_pending_transaction()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        $wallet->deposit(100.00);
        
        // Create pending withdrawal - balance should not change
        $pendingTransaction = $wallet->withdraw(50.00, null, null, null, false);
        
        $this->assertFalse($pendingTransaction->confirmed);
        $this->assertEquals(100.00, $wallet->fresh()->balance); // Balance unchanged
    }

    public function test_can_confirm_pending_transaction()
    {
        $user = User::factory()->create();
        $wallet = $user->createWallet('Test Wallet');
        $wallet->deposit(100.00);
        
        // Create pending withdrawal
        $pendingTransaction = $wallet->withdraw(50.00, null, null, null, false);
        $this->assertEquals(100.00, $wallet->fresh()->balance);
        
        // Confirm the transaction
        $walletService = app(\Nishant\Wallet\Services\WalletService::class);
        $confirmedTransaction = $walletService->confirmTransaction($pendingTransaction->id);
        
        $this->assertTrue($confirmedTransaction->confirmed);
        $this->assertEquals(50.00, $wallet->fresh()->balance); // Balance updated
    }
}
bash
php artisan vendor:publish --tag=wallet-config
bash
php artisan vendor:publish --tag=wallet-migrations
bash
php artisan migrate
bash
POST /api/wallets/1/withdraw
Content-Type: application/json
Authorization: Bearer {token}

{
    "amount": 50.00,
    "reference": "WD-001",
    "description": "Payment for service",
    "confirmed": true
}