-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from bavix/dev
The release of version 2.0
- Loading branch information
Showing
36 changed files
with
1,703 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
71 changes: 71 additions & 0 deletions
71
database/migrations_v2/2018_11_15_124230_create_wallets_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
database/migrations_v2/2018_11_19_164609_update_transactions_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
database/migrations_v2/2018_11_20_133759_add_fee_transfers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Bavix\Wallet\Exceptions; | ||
|
||
class InsufficientFunds extends \LogicException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
Oops, something went wrong.