evinkuraga / laravel-wallet
轻松处理虚拟钱包。
6.3.0
2021-11-10 16:52 UTC
Requires
- php: ^7.3|^8.0
- ext-pdo: *
- brick/math: ~0.8
- doctrine/dbal: ^2.8|^3.0
- illuminate/database: ^6.0|^7.0|^8.0
- ramsey/uuid: ^3.0|^4.0
Requires (Dev)
- brianium/paratest: ^6.2
- cknow/laravel-money: ^6.1
- infection/infection: ~0.17
- laravel/cashier: ^12.5
- nunomaduro/collision: ^5.4
- orchestra/testbench: ^6.4
- phpmetrics/phpmetrics: ^v2.7
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.4
- psalm/plugin-laravel: ^1.4
- symplify/easy-coding-standard: ^9.4
- vimeo/psalm: ^4.1
Suggests
- bavix/laravel-wallet-swap: Addition to the laravel-wallet library for quick setting of exchange rates
- dev-master
- 6.3.0
- 6.2.3
- 6.2.2
- 6.2.1
- 6.2.0
- 6.1.0
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.5.1
- 5.5.0
- 5.4.0
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.1
- 5.2.0
- 5.1.0
- 5.0.2
- 5.0.1
- 5.0.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.1
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- 0.0.1
- dev-develop
- dev-bugfix
This package is auto-updated.
Last update: 2024-09-10 22:49:53 UTC
README
laravel-wallet - 轻松处理虚拟钱包。
- 供应商: bavix
- 包: laravel-wallet
- 版本:
- PHP 版本: 7.3+ (如果使用 5.x 版本,则 7.2+)
- Laravel 版本:
5.5,5.6,5.7,5.8,6.x,7.x,8.x - Composer:
composer require bavix/laravel-wallet
升级指南
从 5.x 版本开始,不再支持 Laravel 5。更新 Laravel 或使用 4.x 版本。
执行迁移时,您将获得指导。
扩展
从 6.2 版本开始,Vacuum 包已内置,无需额外步骤。
用法
将 HasWallet 特性和 Wallet 接口添加到模型中。
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet { use HasWallet; }
现在我们进行交易。
$user = User::first(); $user->balance; // 0 $user->deposit(10); $user->balance; // 10 $user->withdraw(1); $user->balance; // 9 $user->forceWithdraw(200, ['description' => 'payment of taxes']); $user->balance; // -191
购买
将 CanPay 特性和 Customer 接口添加到您的 User 模型中。
use Bavix\Wallet\Traits\CanPay; use Bavix\Wallet\Interfaces\Customer; class User extends Model implements Customer { use CanPay; }
将 HasWallet 特性和 Product 接口添加到 Item 模型中。
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Interfaces\Product; use Bavix\Wallet\Interfaces\Customer; class Item extends Model implements Product { use HasWallet; public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool { /** * If the service can be purchased once, then * return !$customer->paid($this); */ return true; } public function getAmountProduct(Customer $customer) { return 100; } public function getMetaProduct(): ?array { return [ 'title' => $this->title, 'description' => 'Purchase of Product #' . $this->id, ]; } public function getUniqueId(): string { return (string)$this->getKey(); } }
进行购买。
$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)$user->paid($item)); // bool(true) var_dump($user->refund($item)); // bool(true) var_dump((bool)$user->paid($item)); // bool(false)
预加载
User::with('wallet');
如何处理分数数字?
将 HasWalletFloat 特性和 WalletFloat 接口添加到模型中。
use Bavix\Wallet\Traits\HasWalletFloat; use Bavix\Wallet\Interfaces\WalletFloat; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet, WalletFloat { use HasWalletFloat; }
现在我们进行交易。
$user = User::first(); $user->balance; // 100 $user->balanceFloat; // 1.00 $user->depositFloat(1.37); $user->balance; // 237 $user->balanceFloat; // 2.37
支持者
贡献者
代码贡献者
此项目存在得益于所有贡献者。[贡献].
财务贡献者
成为财务贡献者,帮助我们维持社区。[贡献]
个人
组织
用您的组织支持此项目。您的徽标将在此处显示,并带有指向您网站的链接。[贡献]
