1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Services; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\AmountInvalid; |
6
|
|
|
use Bavix\Wallet\Interfaces\MinimalTaxable; |
7
|
|
|
use Bavix\Wallet\Interfaces\Storable; |
8
|
|
|
use Bavix\Wallet\Interfaces\Taxable; |
9
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
10
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
11
|
|
|
use Bavix\Wallet\Traits\HasWallet; |
12
|
|
|
use function app; |
13
|
|
|
|
14
|
|
|
class WalletService |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Wallet $object |
19
|
|
|
* @return int |
20
|
|
|
*/ |
21
|
11 |
|
public function decimalPlaces(Wallet $object): int |
22
|
|
|
{ |
23
|
11 |
|
$decimalPlaces = $this->getWallet($object)->decimal_places ?: 2; |
24
|
11 |
|
return 10 ** $decimalPlaces; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Consider the fee that the system will receive. |
29
|
|
|
* |
30
|
|
|
* @param Wallet $wallet |
31
|
|
|
* @param int $amount |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
28 |
|
public function fee(Wallet $wallet, int $amount): int |
35
|
|
|
{ |
36
|
28 |
|
$fee = 0; |
37
|
28 |
|
if ($wallet instanceof Taxable) { |
38
|
2 |
|
$fee = (int) ($amount * $wallet->getFeePercent() / 100); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Added minimum commission condition |
43
|
|
|
* |
44
|
|
|
* @see https://github.com/bavix/laravel-wallet/issues/64#issuecomment-514483143 |
45
|
|
|
*/ |
46
|
28 |
|
if ($wallet instanceof MinimalTaxable) { |
47
|
1 |
|
$minimal = $wallet->getMinimalFee(); |
48
|
1 |
|
if ($fee < $minimal) { |
49
|
1 |
|
$fee = $wallet->getMinimalFee(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
28 |
|
return $fee; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The amount of checks for errors |
58
|
|
|
* |
59
|
|
|
* @param int $amount |
60
|
|
|
* @throws |
61
|
|
|
*/ |
62
|
69 |
|
public function checkAmount(int $amount): void |
63
|
|
|
{ |
64
|
69 |
|
if ($amount < 0) { |
65
|
3 |
|
throw new AmountInvalid(trans('wallet::errors.price_positive')); |
|
|
|
|
66
|
|
|
} |
67
|
66 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Wallet $object |
71
|
|
|
* @param bool $autoSave |
72
|
|
|
* @return WalletModel |
73
|
|
|
*/ |
74
|
83 |
|
public function getWallet(Wallet $object, bool $autoSave = true): WalletModel |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* @var WalletModel $wallet |
78
|
|
|
*/ |
79
|
83 |
|
$wallet = $object; |
80
|
|
|
|
81
|
83 |
|
if (!($object instanceof WalletModel)) { |
82
|
|
|
/** |
83
|
|
|
* @var HasWallet $object |
84
|
|
|
*/ |
85
|
51 |
|
$wallet = $object->wallet; |
86
|
|
|
} |
87
|
|
|
|
88
|
83 |
|
if ($autoSave) { |
89
|
83 |
|
$wallet->exists or $wallet->save(); |
90
|
|
|
} |
91
|
|
|
|
92
|
83 |
|
return $wallet; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Wallet $object |
97
|
|
|
* @return int |
98
|
|
|
* @deprecated use Storable::getBalance |
99
|
|
|
*/ |
100
|
|
|
public function getBalance(Wallet $object): int |
101
|
|
|
{ |
102
|
|
|
return app(Storable::class) |
103
|
|
|
->getBalance($object); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param WalletModel $wallet |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
17 |
|
public function refresh(WalletModel $wallet): bool |
111
|
|
|
{ |
112
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($wallet) { |
113
|
17 |
|
app(Storable::class)->getBalance($wallet); |
114
|
17 |
|
$balance = $wallet->getAvailableBalance(); |
115
|
17 |
|
$wallet->balance = $balance; |
116
|
|
|
|
117
|
17 |
|
return app(Storable::class)->setBalance($wallet, $balance) && |
118
|
17 |
|
$wallet->save(); |
119
|
17 |
|
}); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|