定制 algeriany/wallet 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

algeriany/wallet

最新稳定版本:v1.0.0

Composer 安装命令:

composer require algeriany/wallet

包简介

wallet

README 文档

README

This package provides a simple wallet management system for Laravel applications. It allows users to create wallets, manage funds, and transfer money between wallets.

Table of Contents

Installation

To install the package, run the following command:

composer require algeriany/wallet

Configuration

After installing, you may want to publish the configuration files using the following commands:

php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=models
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=migrations

Usage

Creating a Wallet

To create a wallet for a user or model that uses the Walletable trait, you can do the following:

use algeriany\wallet\Models\Wallet;
use algeriany\wallet\Services\WalletService;

// Assuming $user is an instance of a model that uses the Walletable trait
$walletService = new WalletService();
$wallet = $walletService->createWallet($user, 100.00); // Initialize with $100

Adding Funds

You can add funds to an existing wallet:

$walletService->addFunds($wallet, 50.00); // Add $50

Deducting Funds

To deduct funds from a wallet:

try {
    $walletService->deductFunds($wallet, 20.00); // Deduct $20
} catch (\Exception $e) {
    echo $e->getMessage(); // Handle insufficient funds exception
}

Transferring Funds

To transfer funds from one wallet to another:

$fromWallet = Wallet::find($fromWalletId);
$toWallet = Wallet::find($toWalletId);

try {
    $walletService->transferFunds($fromWallet, $toWallet, 30.00); // Transfer $30
} catch (\Exception $e) {
    echo $e->getMessage(); // Handle exceptions
}

Database Migrations

This package includes migrations for creating the wallets and wallet_transfers tables. To run the migrations, execute:

php artisan migrate

Migrations Structure

Wallets Table

  • id: Unique identifier for the wallet.
  • wallet_id: Unique String identifier for the wallet.
  • walletable_id: The ID of the wallet owner (user or model).
  • walletable_type: The type of the wallet owner (morph relation).
  • balance: The balance of the wallet.
  • created_at: Timestamp for wallet creation.
  • updated_at: Timestamp for last update.

Wallet Transfers Table

  • id: Unique identifier for the transfer.
  • from_wallet_id: ID of the wallet from which funds are transferred.
  • to_wallet_id: ID of the wallet to which funds are transferred.
  • amount: Amount transferred.
  • created_at: Timestamp for the transfer creation.
  • updated_at: Timestamp for last update.

Publishing Assets

To publish the models and migrations, run:

php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=models
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=migrations

License

This package is licensed under the MIT License. See the LICENSE file for details.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-24