PHP code example of towoju5 / laravel-wallet
1. Go to this page and download the library: Download towoju5/laravel-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/ */
towoju5 / laravel-wallet example snippets
// config/wallet.php
return [
'default_currency' => 'usd',
];
$user = User::find(1); // Retrieve a user instance
$wallet = $user->getWallet('usd'); // Retrieve or create a wallet for USD currency
$wallet->deposit(100, ['description' => 'Task completed', 'meta' => ['task_id' => 123]]);
$wallet->withdraw(50, ['description' => 'Purchase of domain', 'meta' => ['order_id' => 456]]);
echo $wallet->balance; // E.g., "10.50" for 1050 stored in cents
$user = User::find(1);
$wallet = $user->getWallet('usd');
$wallet->deposit(1000, ['description' => 'Initial deposit']); // Adds $10.00 in USD
$wallet->withdraw(250, ['description' => 'Payment for service']); // Deducts $2.50
echo "Current Balance: " . $wallet->balance; // Output balance as a decimal value
// For currency swap or conversion
use Towoju5\LaravelWallet\Models\Wallet;
use Towoju5\LaravelWallet\Services\WalletService;
use Towoju5\LaravelWallet\Services\CurrencyExchangeService;
// Assuming dependency injection or manual instantiation
$walletService = new WalletService(new CurrencyExchangeService());
// Create wallets
$usdWallet = Wallet::create(['user_id' => $user->id, 'currency' => 'usd']);
$eurWallet = Wallet::create(['user_id' => $user->id, 'currency' => 'eur']);
// Deposit in USD wallet
$usdWallet->deposit(1000, ['description' => 'Initial deposit in USD']);
// Transfer funds from USD wallet to EUR wallet
$walletService->transferBetweenCurrencies($usdWallet, $eurWallet, 500);
bash
php artisan vendor:publish --provider="Towoju5\\Wallet\\Providers\\WalletServiceProvider"
bash
php artisan migrate