Skip to content

Commit 4b40971

Browse files
author
Babichev Maxim
committed
init project
1 parent d181a99 commit 4b40971

15 files changed

+754
-4
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
composer.phar
22
/vendor/
3-
4-
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
3+
composer.lock
4+
.idea/

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "bavix/laravel-wallet",
3+
"description": "Easy work with virtual wallet.",
4+
"minimum-stability": "stable",
5+
"homepage": "https://github.com/bavix/laravel-wallet",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Babichev Maxim",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.1",
15+
"laravel/framework": "~5.5.0|~5.6.0|~5.7.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Bavix\\Wallet\\": "src/"
20+
}
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"Bavix\\Wallet\\WalletServiceProvider"
26+
]
27+
}
28+
},
29+
"config": {
30+
"sort-packages": true
31+
}
32+
}

config/config.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'transaction' => [
5+
'table' => 'transactions',
6+
'model' => \Bavix\Wallet\Models\Transaction::class,
7+
],
8+
'transfer' => [
9+
'table' => 'transfers',
10+
'model' => \Bavix\Wallet\Models\Transfer::class,
11+
],
12+
];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Bavix\Wallet\Models\Transaction;
7+
8+
class CreateTransactionsTable extends Migration
9+
{
10+
11+
/**
12+
* @return string
13+
*/
14+
protected function table(): string
15+
{
16+
return (new Transaction())->getTable();
17+
}
18+
19+
/**
20+
* @return void
21+
*/
22+
public function up(): void
23+
{
24+
Schema::create($this->table(), function (Blueprint $table) {
25+
$table->increments('id');
26+
$table->morphs('payable');
27+
$table->enum('type', ['deposit', 'withdraw']);
28+
$table->bigInteger('amount');
29+
$table->boolean('confirmed');
30+
$table->json('meta')->nullable();
31+
$table->uuid('uuid')->index();
32+
$table->timestamps();
33+
34+
$table->index(['payable_type', 'payable_id', 'type']);
35+
$table->index(['payable_type', 'payable_id', 'confirmed']);
36+
$table->index(['payable_type', 'payable_id', 'type', 'confirmed']);
37+
});
38+
}
39+
40+
/**
41+
* @return void
42+
*/
43+
public function down(): void
44+
{
45+
Schema::drop($this->table());
46+
}
47+
48+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Bavix\Wallet\Models\Transaction;
7+
use Bavix\Wallet\Models\Transfer;
8+
9+
class CreateTransfersTable extends Migration
10+
{
11+
12+
/**
13+
* @return string
14+
*/
15+
protected function transactionTable(): string
16+
{
17+
return (new Transaction())->getTable();
18+
}
19+
20+
/**
21+
* @return string
22+
*/
23+
protected function table(): string
24+
{
25+
return (new Transfer())->getTable();
26+
}
27+
28+
/**
29+
* @return void
30+
*/
31+
public function up(): void
32+
{
33+
Schema::create($this->table(), function (Blueprint $table) {
34+
$table->increments('id');
35+
$table->morphs('from');
36+
$table->morphs('to');
37+
$table->unsignedInteger('deposit_id');
38+
$table->unsignedInteger('withdraw_id');
39+
$table->uuid('uuid')->index();
40+
$table->timestamps();
41+
42+
$table->foreign('deposit_id')
43+
->references('id')
44+
->on($this->transactionTable())
45+
->onDelete('cascade');
46+
47+
$table->foreign('withdraw_id')
48+
->references('id')
49+
->on($this->transactionTable())
50+
->onDelete('cascade');
51+
});
52+
}
53+
54+
/**
55+
* @return void
56+
*/
57+
public function down(): void
58+
{
59+
Schema::drop($this->table());
60+
}
61+
62+
}

src/Exceptions/AmountInvalid.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Exceptions;
4+
5+
class AmountInvalid extends \InvalidArgumentException
6+
{
7+
8+
}

src/Exceptions/BalanceIsEmpty.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Exceptions;
4+
5+
class BalanceIsEmpty extends \LogicException
6+
{
7+
8+
}

src/Interfaces/Customer.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
use Bavix\Wallet\Models\Transfer;
6+
7+
interface Customer extends Wallet
8+
{
9+
10+
/**
11+
* @param Product $product
12+
* @return Transfer
13+
* @throws
14+
*/
15+
public function pay(Product $product): Transfer;
16+
17+
/**
18+
* @param Product $product
19+
* @return null|Transfer
20+
* @throws
21+
*/
22+
public function safePay(Product $product): ?Transfer;
23+
24+
/**
25+
* @param Product $product
26+
* @return bool
27+
* @throws
28+
*/
29+
public function refund(Product $product): bool;
30+
31+
/**
32+
* @param Product $product
33+
* @return bool
34+
*/
35+
public function safeRefund(Product $product): bool;
36+
37+
}

src/Interfaces/Product.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
interface Product extends Wallet
6+
{
7+
8+
/**
9+
* @return int
10+
*/
11+
public function getAmountProduct(): int;
12+
13+
/**
14+
* @return array
15+
*/
16+
public function getMetaProduct(): ?array;
17+
18+
}

src/Interfaces/Wallet.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
use Bavix\Wallet\Models\Transaction;
6+
use Bavix\Wallet\Models\Transfer;
7+
use Illuminate\Database\Eloquent\Relations\MorphMany;
8+
9+
interface Wallet
10+
{
11+
/**
12+
* @param int $amount
13+
* @param array|null $meta
14+
* @param bool $confirmed
15+
* @return Transaction
16+
*/
17+
public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction;
18+
19+
/**
20+
* @param int $amount
21+
* @param array|null $meta
22+
* @param bool $confirmed
23+
* @return Transaction
24+
*/
25+
public function withdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction;
26+
27+
/**
28+
* @param int $amount
29+
* @param array|null $meta
30+
* @param bool $confirmed
31+
* @return Transaction
32+
*/
33+
public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction;
34+
35+
/**
36+
* @param self $wallet
37+
* @param int $amount
38+
* @param array|null $meta
39+
* @return Transfer
40+
*/
41+
public function transfer(self $wallet, int $amount, ?array $meta = null): Transfer;
42+
43+
/**
44+
* @param self $wallet
45+
* @param int $amount
46+
* @param array|null $meta
47+
* @return null|Transfer
48+
*/
49+
public function safeTransfer(self $wallet, int $amount, ?array $meta = null): ?Transfer;
50+
51+
/**
52+
* @param Wallet $wallet
53+
* @param int $amount
54+
* @param array|null $meta
55+
* @return Transfer
56+
*/
57+
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer;
58+
59+
/**
60+
* @param int $amount
61+
* @return bool
62+
*/
63+
public function canWithdraw(int $amount): bool;
64+
65+
/**
66+
* @return int
67+
*/
68+
public function getBalanceAttribute(): int;
69+
70+
/**
71+
* @return MorphMany
72+
*/
73+
public function transactions(): MorphMany;
74+
}

0 commit comments

Comments
 (0)