Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d82d381
init v2
rez1dent3 Nov 12, 2018
327ed17
add tests
rez1dent3 Nov 12, 2018
9b8f36c
add coefficient method
rez1dent3 Nov 14, 2018
b42cd2d
update readme
rez1dent3 Nov 14, 2018
dce16bd
patch
rez1dent3 Nov 15, 2018
894aa93
param coefficient to config
rez1dent3 Nov 15, 2018
906a517
add wallet migrations + upgrade tests
rez1dent3 Nov 19, 2018
a569e64
Merge pull request #15 from bavix/master
rez1dent3 Nov 19, 2018
6d2a174
Merge branches 'dev' and 'v1.x' of https://github.com/bavix/laravel-w…
rez1dent3 Nov 19, 2018
dd705ea
update config
rez1dent3 Nov 19, 2018
d70de1b
global upgrade wallet's
rez1dent3 Nov 19, 2018
2de69cd
add canBePaidFloat
rez1dent3 Nov 19, 2018
c62cb27
update transactions
rez1dent3 Nov 19, 2018
c71d2ab
patch HasWallet
rez1dent3 Nov 19, 2018
89454af
add wallet proxy
rez1dent3 Nov 19, 2018
255b7f5
add migrate.md
rez1dent3 Nov 19, 2018
627614b
remove getBalanceAttribute
rez1dent3 Nov 19, 2018
c38a107
Scrutinizer Auto-Fixes
scrutinizer-auto-fixer Nov 19, 2018
0075685
Merge pull request #17 from bavix/scrutinizer-patch-1
rez1dent3 Nov 19, 2018
2cbc69b
add phpDocs
rez1dent3 Nov 20, 2018
e9087cb
remove migrate.md
rez1dent3 Nov 20, 2018
7bc05e1
add taxing
rez1dent3 Nov 20, 2018
d71841d
add payFree & trans
rez1dent3 Nov 20, 2018
c954623
update method "add balance"
rez1dent3 Nov 20, 2018
f212db4
fixed a bug with which you can earn by buying and selling goods
rez1dent3 Nov 20, 2018
ac50440
shared multi wallet with wallet
rez1dent3 Nov 20, 2018
56d8606
global upgrade; add test for multi-wallet
rez1dent3 Nov 21, 2018
69f31cc
add test WalletUnique
rez1dent3 Nov 21, 2018
d3cf380
patch calculateBalance + unit's
rez1dent3 Nov 21, 2018
3865c76
bug fixed; up coverage
rez1dent3 Nov 21, 2018
ebdb041
refactor code
rez1dent3 Nov 21, 2018
8b20340
fix bugs
rez1dent3 Nov 21, 2018
1770103
patch unit
rez1dent3 Nov 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@

laravel-wallet - Easy work with virtual wallet.

[[Documentation](https://laravel-wallet.bavix.ru/)]
[[Get Started](https://laravel-wallet.bavix.ru/2.0/basic-usage)]

* **Vendor**: bavix
* **Package**: laravel-wallet
* **Version**: [![Latest Stable Version](https://poser.pugx.org/bavix/laravel-wallet/v/stable)](https://packagist.org/packages/bavix/laravel-wallet)
* **PHP Version**: 7.1+
* **Laravel Version**: `5.5`, `5.6`, `5.7`
* **[Composer](https://getcomposer.org/):** `composer require bavix/laravel-wallet`

### Switching from 1.x.x → 2.x.x

To perform the migration, you will be [helped by the instruction](https://laravel-wallet.bavix.ru/2.0/upgrade-guide).

### Run Migrations
Publish the migrations with this artisan command:
```bash
Expand Down Expand Up @@ -130,7 +137,32 @@ var_dump((bool)$user->paid($item)); // bool(false)
### Eager Loading

```php
User::with('balance');
User::with('wallet');
```

### How to work with fractional numbers?
Add the `HasWalletFloat` trait and `WalletFloat` interface to model.
```php
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.

```php
$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)
```

---
Expand Down
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

return [
'package' => [
'coefficient' => 100.,
],
'transaction' => [
'table' => 'transactions',
'model' => \Bavix\Wallet\Models\Transaction::class,
Expand All @@ -9,4 +12,12 @@
'table' => 'transfers',
'model' => \Bavix\Wallet\Models\Transfer::class,
],
'wallet' => [
'table' => 'wallets',
'model' => \Bavix\Wallet\Models\Wallet::class,
'default' => [
'name' => 'Default Wallet',
'slug' => 'default',
],
],
];
71 changes: 71 additions & 0 deletions database/migrations_v2/2018_11_15_124230_create_wallets_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Wallet;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Collection;

class CreateWalletsTable extends Migration
{

/**
* @return string
*/
protected function table(): string
{
return (new Wallet())->getTable();
}

/**
* @return void
*/
public function up(): void
{
Schema::create($this->table(), function(Blueprint $table) {
$table->increments('id');
$table->morphs('holder');
$table->string('name');
$table->string('slug')->index();
$table->string('description')->nullable();
$table->bigInteger('balance')->default(0);
$table->timestamps();

$table->unique(['holder_type', 'holder_id', 'slug']);
});

/**
* migrate v1 to v2
*/
$default = config('wallet.wallet.default.name', 'Default Wallet');
$slug = config('wallet.wallet.default.slug', 'default');
$query = Transaction::query()->distinct()
->selectRaw('payable_type as holder_type')
->selectRaw('payable_id as holder_id')
->selectRaw('? as name', [$default])
->selectRaw('? as slug', [$slug])
->selectRaw('sum(amount) as balance')
->selectRaw('? as created_at', [\Carbon\Carbon::now()])
->selectRaw('? as updated_at', [\Carbon\Carbon::now()])
->groupBy('holder_type', 'holder_id')
->orderBy('holder_type');

DB::transaction(function () use ($query) {
$query->chunk(1000, function (Collection $transactions) {
DB::table((new Wallet())->getTable())
->insert($transactions->toArray());
});
});
}

/**
* @return void
*/
public function down(): void
{
Schema::drop($this->table());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Wallet;

class UpdateTransactionsTable extends Migration
{

/**
* @return string
*/
protected function table(): string
{
return (new Transaction())->getTable();
}

/**
* @return string
*/
protected function walletTable(): string
{
return (new Wallet())->getTable();
}

/**
* @return void
*/
public function up(): void
{
Schema::table($this->table(), function(Blueprint $table) {
$table->unsignedInteger('wallet_id')
->nullable()
->after('payable_id');

$table->foreign('wallet_id')
->references('id')
->on($this->walletTable())
->onDelete('cascade');
});

$slug = config('wallet.wallet.default.slug', 'default');
DB::transaction(function () use ($slug) {
Wallet::where('slug', $slug)->each(function (Wallet $wallet) {
Transaction::query()
->where('payable_type', $wallet->holder_type)
->where('payable_id', $wallet->holder_id)
->update(['wallet_id' => $wallet->id]);
});
});
}

/**
* @return void
*/
public function down(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('wallet_id');
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Bavix\Wallet\Models\Transfer;

class AddFeeTransfersTable extends Migration
{

/**
* @return string
*/
protected function table(): string
{
return (new Transfer())->getTable();
}

/**
* @return void
*/
public function up(): void
{
Schema::table($this->table(), function(Blueprint $table) {
$table->bigInteger('fee')
->default(0)
->after('withdraw_id');
});
}

/**
* @return void
*/
public function down(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('fee');
});
}

}
8 changes: 8 additions & 0 deletions resources/lang/en/errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'price_positive' => 'The price should be positive',
'product_stock' => 'The product is out of stock',
'wallet_empty' => 'Wallet is empty',
'insufficient_funds' => 'Insufficient funds',
];
2 changes: 1 addition & 1 deletion src/Exceptions/BalanceIsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bavix\Wallet\Exceptions;

class BalanceIsEmpty extends \LogicException
class BalanceIsEmpty extends InsufficientFunds
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/InsufficientFunds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Bavix\Wallet\Exceptions;

class InsufficientFunds extends \LogicException
{

}
20 changes: 20 additions & 0 deletions src/Interfaces/Taxing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bavix\Wallet\Interfaces;

interface Taxing
{

/**
* Specify the percentage of the amount.
* For example, the product costs $100, the equivalent of 15%.
* That's $115.
*
* Minimum 0; Maximum 100
* Example: return 7.5; // 7.5%
*
* @return float
*/
public function getFeePercent(): float;

}
69 changes: 69 additions & 0 deletions src/Interfaces/WalletFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Bavix\Wallet\Interfaces;

use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;

interface WalletFloat
{
/**
* @param float $amount
* @param array|null $meta
* @param bool $confirmed
* @return Transaction
*/
public function depositFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction;

/**
* @param float $amount
* @param array|null $meta
* @param bool $confirmed
* @return Transaction
*/
public function withdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction;

/**
* @param float $amount
* @param array|null $meta
* @param bool $confirmed
* @return Transaction
*/
public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction;

/**
* @param Wallet $wallet
* @param float $amount
* @param array|null $meta
* @return Transfer
*/
public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer;

/**
* @param Wallet $wallet
* @param float $amount
* @param array|null $meta
* @return null|Transfer
*/
public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): ?Transfer;

/**
* @param Wallet $wallet
* @param float $amount
* @param array|null $meta
* @return Transfer
*/
public function forceTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer;

/**
* @param float $amount
* @return bool
*/
public function canWithdrawFloat(float $amount): bool;

/**
* @return float
*/
public function getBalanceFloatAttribute(): float;

}
Loading