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');
// Using the trait
$transactions = $user->getTransactionsByWalletName('Main Wallet');
// Using the service
$transactions = $walletService->getTransactionsByWalletName(
userId: 1,
walletName: 'Main Wallet'
);
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;
}