amamdouh1939/laravel-wallet

适用于您应用的简单易用虚拟钱包

1.10.2 2022-03-01 10:26 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:33 UTC


README

在几个项目中,我需要实现虚拟货币。用户会通过Stripe购买积分包,然后在应用中使用这些积分来换取服务或商品。本包是这个概念的小型简单实现,并提供了自定义的空间。

安装

使用composer安装包

composer require depsimon/laravel-wallet

运行迁移

使用以下Artisan命令发布迁移

php artisan vendor:publish --provider="Depsimon\Wallet\WalletServiceProvider" --tag=migrations

配置

可以使用以下Artisan命令发布配置文件

php artisan vendor:publish --provider="Depsimon\Wallet\WalletServiceProvider" --tag=config

这将合并wallet.php配置文件,您可以在其中指定自定义的用户、钱包和交易类。

使用方法

HasWallet特质添加到您的用户模型中。

use Depsimon\Wallet\HasWallet;

class User extends Model
{
    use HasWallet;

    ...
}

在执行交易之前,创建用户的钱包。

$user->wallet()->create();

然后您可以从用户模型中轻松地进行交易。

$user = User::find(1);
$user->balance; // 0

$user->deposit(100);
$user->balance; // 100

$user->withdraw(50);
$user->balance; // 50

$user->forceWithdraw(200);
$user->balance; // -150

您可以轻松添加元信息到交易以适应您的需求。

$user = User::find(1);
$user->deposit(100, 'deposit', ['stripe_source' => 'ch_BEV2Iih1yzbf4G3HNsfOQ07h', 'description' => 'Deposit of 100 credits from Stripe Payment']);
$user->withdraw(10, 'withdraw', ['description' => 'Purchase of Item #1234']);

安全

如果您发现任何与安全相关的问题,请通过电子邮件simon@webartisan.be联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件