laraditz/wallet

Laravel的简单虚拟钱包或电子钱包

1.0.3 2023-09-19 20:18 UTC

This package is auto-updated.

Last update: 2024-09-19 22:16:59 UTC


README

Laravel Wallet

Laravel Wallet

Latest Version on Packagist Total Downloads License

Laravel的简单虚拟钱包或电子钱包。

安装

您可以通过composer安装此包。

composer require laraditz/wallet

运行迁移命令以创建必要的数据库表。

php artisan migrate

使用方法

HasWallets特性添加到您的模型中。

use Laraditz\Wallet\Traits\HasWallets;

class User extends Authenticatable
{
    use HasWallets;
    ...
}

创建您的默认钱包。如果您使用不同的名称,您可能需要在.env中设置WALLET_DEFAULT并将值设置为slug值。这是可选的,仅在您想获取您的钱包但不想指定钱包slug时才有用。

// Simply use below code to create a default wallet types
app('wallet')->createWalletType([
    'name' => 'Default',   
    'currency_code' => 'POINTS',
    'currency_symbol' => 'PTS',
]);

在创建钱包类型时,您还可以指定许多其他设置,如下所示:

use Laraditz\Wallet\Enums\ActiveStatus;
use Laraditz\Wallet\Enums\Placement;

app('wallet')->createWalletType([
    'name' => 'New Wallet', // will produce new-wallet slug
    'description' => 'This is my new wallet',
    'currency_code' => 'POINTS',
    'currency_symbol' => 'PTS',
    'default_scale' => 0, // example, EUR and USD have 2 decimal places, while JPY has 0
    'decimal_separator' => '.', // default is using dot (.)
    'thousand_separator' => ',', // default is using comma (,)
    'code_placement' => Placement::Right, // default placement is left
    'symbol_placement' => Placement::Left, // default placement is left
    'status' => ActiveStatus::Active, // default status is active
    'start_at' => now(), // you can also set when the wallet can start be use
    'end_at' => now()->addDays(3), // or when it ends
]);

设置完成。现在您可以使用电子钱包存款、取款或转账。

$userOne = User::find(1);
$walletOne = $userOne->getWallet(); // get default wallet for userOne

// deposit
$deposit = $walletOne->deposit("100"); // deposit amount of 100 into default wallet with processing status
$deposit->markAsCompleted(); // change the status from processing to completed

$walletOne->depositNow("100"); // Use depositNow() so that the transaction completed immediately

// withdraw
$withdraw = $walletOne->withdraw("100"); // withdraw amount of 100 into default wallet with processing status
$withdraw->markAsCompleted(); // change the status from processing to completed

$walletOne->withdrawNow("100"); // Use withdrawNow() so that the transaction completed immediately

$userTwo = User::find(2);
$walletTwo = $userTwo->getWallet(); // get default wallet for userTwo

// transfer amount from userOne to userTwo
$transfer = $walletOne->transfer($walletTwo, "100");
$transfer->markAsCompleted(); // change the status from processing to completed

$walletOne->transferNow($walletTwo, "100"); // Use transferNow() so that the transaction completed immediately

// get user transactions
$allTransactions = $userOne->transactions;

默认情况下,当您调用getWallet时,如果它尚未创建,它将创建用户钱包。您的钱包将被分配一个随机的唯一钱包地址值。但您也可以使用自己的地址值创建钱包。

$userThree = User::find(3);
$userThree->createWallet(
    slug: 'new-wallet', // specify the wallet slug
    values: [
        'address' => '0xf6A32f757196ac753A354F145F408bF88BEacf77',
        'description' => 'This is my crypto wallet', 
    ]
);

// specify the slug name to get the new wallet
$walletThree = $userThree->getWallet('new-wallet');

// to update description or/and metadata to the wallet
$walletThree->update([
    'description' => 'This is a new description', 
    'metadata' => [
        'user' => 'Farhan'
    ]
]);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全

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

致谢

许可证

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