1. Go to this page and download the library: Download moecasts/laravel-wallet library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
moecasts / laravel-wallet example snippets
composer
Moecasts\Laravel\Wallet\WalletServiceProvider,
use Moecasts\Laravel\Wallet\Traits\HasWallets;
class User extends Model
{
use HasWallets;
}
$wallet = $user->getWallet($currency)
$wallet->balance
// return the user transfers of all his wallets
$user->transfers
// return the wallet transfers
$wallet->transfers
// return the user transactions of all his wallets
$user->transactions
// return the wallet transactions
$wallet->transactions
$wallet->withdraw($amount, $meta = [], $confirmed = true)
$wallet->withdraw(233)
$wallet->withdraw(233, ['description' => 'withdraw Testing'])
// forceWithdraw though balance is not enough
$wallet->forceWithdraw(233)
return [
'exchange' => [
// To be exchanged cuurency
'COI' => [
// target currency => (one to be exchanged cuurency = ? target currency)
'POI' => 100,
'CNY' => 1
],
]
];
use Illuminate\Database\Eloquent\Model;
use Moecasts\Laravel\Wallet\Interfaces\Exchangeable;
use Moecasts\Laravel\Wallet\Traits\HasWallets;
class User extends Model implements Exchangeable
{
use HasWallets;
}
$wallet = $userWallet->getWallet('COI')
// $wallet->exchange(string $currency, float $mouant)
$wallet->exchange('POI', 10)
// This will return null but not exception when it failed.
$wallet->safeExchange('POI', 10)
// This will exchange though balance is not enough
$wallet->forceExchange('POI', 10)
use Moecasts\Laravel\Wallet\Interfaces\Transferable;
use Moecasts\Laravel\Wallet\Traits\HasWallets;
class User extends Model implements Transferable
{
use HasWallets;
}
$user = User::find(1);
$transferable = User::find(2);
$wallet = $user->getWallet($currency);
// $wallet->transfer(Transferable $transferable,float $amount,?array $meta = [], string $action = 'transfer'))
$wallet->transfer($transferable, 233);
// This will return null but not exception when it failed.
$wallet->safeTransfer($transferable, 233);
use Moecasts\Laravel\Wallet\Interfaces\Product;
class Item extends Model implements Product
{
use HasWallets;
public function canBePaid(string $action = 'paid'): bool
{
return true;
}
public function getProductAmount(string $action = 'paid'): float
{
switch ($action) {
case 'paid':
return 10;
break;
default:
return 23.3;
break;
}
}
public function getProductMeta(string $action = 'PAID'): ?array
{
return [
'title' => 'Paid for #' . $this->id
];
}
public function getReceiptWallet(string $currency): Wallet
{
return $this->getWallet($currency);
}
}
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Moecasts\Laravel\Wallet\Interfaces\Product;
class Item extends Model implements Product
{
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function canBePaid(string $action = 'paid'): bool
{
return true;
}
public function getProductAmount(string $action = 'paid'): float
{
switch ($action) {
case 'paid':
return 10;
break;
default:
return 23.3;
break;
}
}
public function getProductMeta(string $action = 'paid'): ?array
{
return [
'title' => 'Paid for #' . $this->id
];
}
public function getReceiptWallet(string $currency): Wallet
{
return $this->author->getWallet($currency);
}
}
$user = User::first();
$product = Item::first();
$wallet = $user->getWallet($currency);
// $wallet->pay(Product $item, string $action = 'paid', bool $force = false)
$wallet->pay($item)
$wallet->pay($item, 'read')
// This will return null but not exception when it failed.
$wallet->safePay($item, 'paid')
// return bool
$wallet->paid($item, $action = 'paid')
use Moecasts\Laravel\Wallet\Interfaces\Product;
use Moecasts\Laravel\Wallet\Interfaces\Refundable;
class Item extends Model implements Product, Refundable
{
use HasWallets;
public function canBePaid(string $action = 'paid'): bool
{
return true;
}
public function getProductAmount(string $action = 'paid'): float
{
switch ($action) {
case 'paid':
return 10;
break;
default:
return 23.3;
break;
}
}
public function getProductMeta(string $action = 'PAID'): ?array
{
return [
'title' => 'Paid for #' . $this->id
];
}
public function getReceiptWallet(string $currency): Wallet
{
return $this->getWallet($currency);
}
}
// $wallet->refund(Refundable $item, string $action = 'paid')
$wallet->refund($item)
$wallet->refund($item, 'read')
// This will return null but not exception when it failed.
$wallet->safeRefund($item, 'read')