bavix/laravel-wallet
最新稳定版本:12.0.1
Composer 安装命令:
composer require bavix/laravel-wallet
包简介
It's easy to work with a virtual wallet.
关键字:
README 文档
README
laravel-wallet - It's easy to work with a virtual wallet.
[Benchmark] [Documentation] [Get Started]
- Vendor: bavix
- Package: laravel-wallet
- Composer:
composer require bavix/laravel-wallet
Support Policy
| Version | Laravel | PHP | Release date | End of improvements | End of support |
|---|---|---|---|---|---|
| 12.x | ^13.0 | 8.3,8.4,8.5 | Apr 18, 2026 | May 1, 2028 | Sep 6, 2028 |
| 11.x | ^11.0, ^12.0, ^13.0 | 8.3,8.4,8.5 | Mar 14, 2024 | May 1, 2026 | Sep 6, 2026 |
Upgrade Guide
To perform the migration, you will be helped by the instruction.
Community
I want to create a cozy place for developers using the wallet package. This will help you find bugs faster, get feedback and discuss ideas.
Telegram: @laravel_wallet
Extensions
| Extension | Description |
|---|---|
| Swap | Addition to the laravel-wallet library for quick setting of exchange rates |
| uuid | Addition to laravel-wallet to support model uuid keys |
| Warm Up | Addition to the laravel-wallet library for refresh balance wallets |
Usage
Add the HasWallet trait and Wallet interface to model.
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet { use HasWallet; }
Now we make transactions.
$user = User::first(); $user->balanceInt; // 0 $user->deposit(10); $user->balance; // 10 $user->balanceInt; // int(10) $user->withdraw(1); $user->balance; // 9 $user->forceWithdraw(200, ['description' => 'payment of taxes']); $user->balance; // -191
Purchases
Add the CanPay trait and Customer interface to your User model.
use Bavix\Wallet\Traits\CanPay; use Bavix\Wallet\Interfaces\Customer; class User extends Model implements Customer { use CanPay; }
Add the HasWallet trait and interface to Item model.
Starting from version 9.x there are two product interfaces:
- For an unlimited number of products (
ProductInterface); - For a limited number of products (
ProductLimitedInterface);
An example with an unlimited number of products:
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Interfaces\Customer; use Bavix\Wallet\Interfaces\ProductInterface; class Item extends Model implements ProductInterface { use HasWallet; public function getAmountProduct(Customer $customer): int|string { return 100; } public function getMetaProduct(): ?array { return [ 'title' => $this->title, 'description' => 'Purchase of Product #' . $this->id, ]; } }
Example with a limited number of products:
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Interfaces\Customer; use Bavix\Wallet\Interfaces\ProductLimitedInterface; use Bavix\Wallet\External\Api\PurchaseQuery; use Bavix\Wallet\External\Api\PurchaseQueryHandlerInterface; class Item extends Model implements ProductLimitedInterface { use HasWallet; public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool { /** * This is where you implement the constraint logic. * * If the service can be purchased once, then * return ! app(PurchaseQueryHandlerInterface::class)->one(PurchaseQuery::create($customer, $this)); */ return true; } public function getAmountProduct(Customer $customer): int|string { return 100; } public function getMetaProduct(): ?array { return [ 'title' => $this->title, 'description' => 'Purchase of Product #' . $this->id, ]; } }
I do not recommend using the limited interface when working with a shopping cart.
If you are working with a shopping cart, then you should override the PurchaseServiceInterface interface.
With it, you can check the availability of all products with one request, there will be no N-queries in the database.
Proceed to purchase.
$user = User::first(); $user->balance; // 100 $item = Item::first(); $user->pay($item); // If you do not have enough money, throw an exception var_dump($user->balance); // 0 if ($user->safePay($item)) { // try to buy again } var_dump((bool) app(PurchaseQueryHandlerInterface::class)->one(PurchaseQuery::create($user, $item))); // bool(true) var_dump($user->refund($item)); // bool(true) var_dump((bool) app(PurchaseQueryHandlerInterface::class)->one(PurchaseQuery::create($user, $item))); // bool(false)
Eager Loading
// When working with one wallet User::with('wallet'); // When using the multi-wallet functionality User::with('wallets');
How to work with fractional numbers?
Add the HasWalletFloat trait and WalletFloat interface to model.
use Bavix\Wallet\Traits\HasWalletFloat; use Bavix\Wallet\Interfaces\WalletFloat; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet, WalletFloat { use HasWalletFloat; }
Now we make transactions.
$user = User::first(); $user->balance; // 100 $user->balanceFloat; // 1.00 $user->depositFloat(1.37); $user->balance; // 237 $user->balanceFloat; // 2.37
Contributors
Code Contributors
This project exists thanks to all the people who contribute. [Contribute].
Financial Contributors
Become a financial contributor and help us sustain our community. [Contribute]
Individuals
Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
统计信息
- 总下载量: 1.2M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1324
- 点击次数: 7
- 依赖项目数: 13
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-07