Skip to content

Commit

Permalink
Merge pull request #14 from bavix/dev
Browse files Browse the repository at this point in the history
The release of version 2.0
  • Loading branch information
rez1dent3 committed Nov 21, 2018
2 parents d27bd5f + 1770103 commit 494f2fd
Show file tree
Hide file tree
Showing 36 changed files with 1,703 additions and 63 deletions.
34 changes: 33 additions & 1 deletion README.md
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
@@ -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
@@ -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());
}

}
@@ -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');
});
}

}
@@ -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
@@ -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
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
@@ -0,0 +1,8 @@
<?php

namespace Bavix\Wallet\Exceptions;

class InsufficientFunds extends \LogicException
{

}
20 changes: 20 additions & 0 deletions src/Interfaces/Taxing.php
@@ -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
@@ -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;

}

0 comments on commit 494f2fd

Please sign in to comment.