walterjrp/laravel-wallet-mongodb

轻松处理虚拟钱包。

4.1.0 2019-12-15 08:19 UTC

README

Laravel Wallet

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status Mutation testing badge

Package Rank Latest Stable Version Latest Unstable Version License composer.lock

laravel-wallet - 轻松处理虚拟钱包。

[文档] [开始使用]

[文档] [如何开始]

  • 供应商: bavix
  • 包名: laravel-wallet
  • 版本: 最新稳定版本
  • PHP 版本: 7.2+
  • Laravel 版本: 5.5, 5.6, 5.7, 5.8, 6.x
  • Composer: composer require bavix/laravel-wallet

升级指南

执行迁移时,您将 获得说明的帮助

扩展

用法

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; // int(0)

$user->deposit(10);
$user->balance; // int(10)

$user->withdraw(1);
$user->balance; // int(9)

$user->forceWithdraw(200, ['description' => 'payment of taxes']);
$user->balance; // int(-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 = null): bool
    {
        /**
         * If the service can be purchased once, then
         *  return !$customer->paid($this);
         */
        return true; 
    }
    
    public function getAmountProduct(Customer $customer): int
    {
        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; // int(100)

$item = Item::first();
$user->pay($item); // If you do not have enough money, throw an exception
var_dump($user->balance); // int(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; // int(100)
$user->balanceFloat; // float(1.00)

$user->depositFloat(1.37);
$user->balance; // int(237)
$user->balanceFloat; // float(2.37)

支持者

Supported by JetBrains