Package Data | |
---|---|
Maintainer Username: | REZ1DENT3 |
Maintainer Contact: | info@babichev.net (Babichev Maxim) |
Package Create Date: | 2018-11-07 |
Package Last Update: | 2024-12-14 |
Home Page: | https://bavix.github.io/laravel-wallet/ |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-15 15:20:34 |
Package Statistics | |
---|---|
Total Downloads: | 576,611 |
Monthly Downloads: | 21,789 |
Daily Downloads: | 265 |
Total Stars: | 1,153 |
Total Watchers: | 43 |
Total Forks: | 233 |
Total Open Issues: | 6 |
laravel-wallet - Easy work with virtual wallet.
5.5
, 5.6
, 5.7
, 5.8
composer require bavix/laravel-wallet
To perform the migration, you will be helped by the instruction.
Add the HasWallet
trait and Wallet
interface to model.
use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Wallet;
class User extends Model implements Wallet
{
use HasWallet;
}
Now we make transactions.
$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)
Add the CanPay
trait and Customer
interface to your User
model.
use Bavix\Wallet\Traits\CanPay;
use Bavix\Wallet\Interfaces\Customer;
class User extends Model implements Customer
{
use CanPay;
}
Add the HasWallet
trait and Product
interface to Item
model.
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(): int
{
return 100;
}
public function getMetaProduct(): ?array
{
return [
'title' => $this->title,
'description' => 'Purchase of Product #' . $this->id,
'price' => $this->getAmountProduct(),
];
}
public function getUniqueId(): string
{
return (string)$this->getKey();
}
}
Proceed to purchase.
$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');
Add the HasWalletFloat
trait and WalletFloat
interface to model.
use Bavix\Wallet\Traits\HasWalletFloat;
use Bavix\Wallet\Interfaces\WalletFloat;
use Bavix\Wallet\Interfaces\Wallet;
class User extends Model implements Wallet, WalletFloat
{
use HasWalletFloat;
}
Now we make transactions.
$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