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