1. Go to this page and download the library: Download byjg/wallets 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/ */
byjg / wallets example snippets
use ByJG\Wallets\Service\WalletService;
use ByJG\Wallets\Service\WalletTypeService;
use ByJG\Wallets\Service\TransactionService;
use ByJG\Wallets\Entity\WalletTypeEntity;
use ByJG\Wallets\Repository\WalletRepository;
use ByJG\Wallets\Repository\WalletTypeRepository;
use ByJG\Wallets\Repository\TransactionRepository;
use ByJG\Wallets\DTO\TransactionDTO;
use ByJG\AnyDataset\Db\Factory;
// Initialize database connection
$dbDriver = Factory::getDbInstance('mysql://user:pass@localhost/dbname');
// Initialize repositories
$walletTypeRepo = new WalletTypeRepository($dbDriver);
$transactionRepo = new TransactionRepository($dbDriver);
$walletRepo = new WalletRepository($dbDriver);
// Initialize services
$walletTypeService = new WalletTypeService($walletTypeRepo);
$transactionService = new TransactionService($transactionRepo, $walletRepo);
$walletService = new WalletService($walletRepo, $walletTypeService, $transactionService);
// Create a wallet type
$walletType = new WalletTypeEntity();
$walletType->setWalletTypeId('USD');
$walletType->setName('US Dollar');
$walletTypeService->update($walletType);
// Create a wallet with $100.00 initial balance (10000 cents)
$walletId = $walletService->createWallet('USD', 'user-123', 10000, 2);
// Add funds: $50.00
$transaction = $transactionService->addFunds(
TransactionDTO::create($walletId, 5000)
->setDescription('Deposit from bank account')
);
// Withdraw funds: $30.00
$transaction = $transactionService->withdrawFunds(
TransactionDTO::create($walletId, 3000)
->setDescription('Purchase payment')
);
// Reserve funds for pending withdrawal: $20.00
$reserve = $transactionService->reserveFundsForWithdraw(
TransactionDTO::create($walletId, 2000)
->setDescription('Pre-authorization')
);
// Accept the reservation
$transactionService->acceptFundsById($reserve->getTransactionId());
// Get wallet balance
$wallet = $walletService->getById($walletId);
echo "Available: " . ($wallet->getAvailable() / 100) . " USD\n";
// Reserve funds when order is placed
$reserve = $transactionService->reserveFundsForWithdraw(
TransactionDTO::create($walletId, 9999)
->setDescription('Order #12345')
->setReferenceSource('ecommerce')
->setReferenceId('order-12345')
);
// Capture payment when order ships
$transactionService->acceptFundsById($reserve->getTransactionId());
// Or cancel and release funds if order is cancelled
// $transactionService->rejectFundsById($reserve->getTransactionId());
// Create multiple wallets for same user
$usdWallet = $walletService->createWallet('USD', 'user-123', 100000, 2);
$eurWallet = $walletService->createWallet('EUR', 'user-123', 50000, 2);
$btcWallet = $walletService->createWallet('BTC', 'user-123', 5000000, 8);
// Transfer between wallets (with exchange rate logic in your app)
$walletService->transferFunds($usdWallet, $eurWallet, 10000);
// Reserve bet amount
$betReserve = $transactionService->reserveFundsForWithdraw(
TransactionDTO::create($walletId, 5000)
->setDescription('Bet on Game #789')
);
// User wins - reject withdrawal and add winnings
$transactionService->rejectFundsById($betReserve->getTransactionId());
$transactionService->addFunds(
TransactionDTO::create($walletId, 10000)
->setDescription('Bet winnings')
);
// User loses - accept withdrawal
// $transactionService->acceptFundsById($betReserve->getTransactionId());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.