|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\BalanceIsEmpty; |
|
6
|
|
|
use Bavix\Wallet\Exceptions\InsufficientFunds; |
|
7
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
|
8
|
|
|
use Bavix\Wallet\Models\Transaction; |
|
9
|
|
|
use Bavix\Wallet\Models\Transfer; |
|
10
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
|
11
|
|
|
use Bavix\Wallet\Objects\Bring; |
|
12
|
|
|
use Bavix\Wallet\Objects\Operation; |
|
13
|
|
|
use Bavix\Wallet\Traits\HasWallet; |
|
14
|
|
|
use Illuminate\Support\Facades\DB; |
|
15
|
|
|
use function app; |
|
16
|
|
|
use function compact; |
|
17
|
|
|
|
|
18
|
|
|
class CommonService |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Wallet $from |
|
23
|
|
|
* @param Wallet $to |
|
24
|
|
|
* @param int $amount |
|
25
|
|
|
* @param array|null $meta |
|
26
|
|
|
* @param string $status |
|
27
|
|
|
* @return Transfer |
|
28
|
|
|
*/ |
|
29
|
12 |
|
public function transfer(Wallet $from, Wallet $to, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): Transfer |
|
30
|
|
|
{ |
|
31
|
12 |
|
$this->verifyWithdraw($from, $amount); |
|
32
|
12 |
|
return $this->forceTransfer($from, $to, $amount, $meta, $status); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Wallet $from |
|
37
|
|
|
* @param Wallet $to |
|
38
|
|
|
* @param int $amount |
|
39
|
|
|
* @param array|null $meta |
|
40
|
|
|
* @param string $status |
|
41
|
|
|
* @return Transfer |
|
42
|
|
|
*/ |
|
43
|
19 |
|
public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): Transfer |
|
44
|
|
|
{ |
|
45
|
19 |
|
$fee = app(WalletService::class)->fee($to, $amount); |
|
46
|
19 |
|
$withdraw = $this->forceWithdraw($from, $amount + $fee, $meta); |
|
47
|
19 |
|
$deposit = $this->deposit($to, $amount, $meta); |
|
48
|
|
|
|
|
49
|
19 |
|
$from = app(WalletService::class) |
|
50
|
19 |
|
->getWallet($from); |
|
51
|
|
|
|
|
52
|
19 |
|
$transfers = $this->multiBrings([ |
|
53
|
19 |
|
(new Bring()) |
|
54
|
19 |
|
->setStatus($status) |
|
55
|
19 |
|
->setDeposit($deposit) |
|
56
|
19 |
|
->setWithdraw($withdraw) |
|
57
|
19 |
|
->setFrom($from) |
|
58
|
19 |
|
->setTo($to) |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
19 |
|
return current($transfers); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Wallet $wallet |
|
66
|
|
|
* @param int $amount |
|
67
|
|
|
* @param array|null $meta |
|
68
|
|
|
* @param bool|null $confirmed |
|
69
|
|
|
* @return Transaction |
|
70
|
|
|
*/ |
|
71
|
30 |
|
public function forceWithdraw(Wallet $wallet, int $amount, ?array $meta, bool $confirmed = true): Transaction |
|
72
|
|
|
{ |
|
73
|
30 |
|
$walletService = app(WalletService::class); |
|
74
|
30 |
|
$walletService->checkAmount($amount); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var WalletModel $wallet |
|
78
|
|
|
*/ |
|
79
|
30 |
|
$wallet = $walletService->getWallet($wallet); |
|
80
|
|
|
|
|
81
|
30 |
|
$transactions = $this->multiOperation($wallet, [ |
|
82
|
30 |
|
(new Operation()) |
|
83
|
30 |
|
->setType(Transaction::TYPE_WITHDRAW) |
|
84
|
30 |
|
->setConfirmed($confirmed) |
|
85
|
30 |
|
->setAmount(-$amount) |
|
86
|
30 |
|
->setMeta($meta) |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
30 |
|
return current($transactions); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Wallet $wallet |
|
94
|
|
|
* @param int $amount |
|
95
|
|
|
* @param array|null $meta |
|
96
|
|
|
* @param bool $confirmed |
|
97
|
|
|
* @return Transaction |
|
98
|
|
|
*/ |
|
99
|
35 |
|
public function deposit(Wallet $wallet, int $amount, ?array $meta, bool $confirmed = true): Transaction |
|
100
|
|
|
{ |
|
101
|
35 |
|
$walletService = app(WalletService::class); |
|
102
|
35 |
|
$walletService->checkAmount($amount); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var WalletModel $wallet |
|
106
|
|
|
*/ |
|
107
|
32 |
|
$wallet = $walletService->getWallet($wallet); |
|
108
|
|
|
|
|
109
|
32 |
|
$transactions = $this->multiOperation($wallet, [ |
|
110
|
32 |
|
(new Operation()) |
|
111
|
32 |
|
->setType(Transaction::TYPE_DEPOSIT) |
|
112
|
32 |
|
->setConfirmed($confirmed) |
|
113
|
32 |
|
->setAmount($amount) |
|
114
|
32 |
|
->setMeta($meta) |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
32 |
|
return current($transactions); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param Wallet $wallet |
|
122
|
|
|
* @param int $amount |
|
123
|
|
|
* @return void |
|
124
|
|
|
* @throws BalanceIsEmpty |
|
125
|
|
|
* @throws InsufficientFunds |
|
126
|
|
|
*/ |
|
127
|
36 |
|
public function verifyWithdraw(Wallet $wallet, int $amount): void |
|
128
|
|
|
{ |
|
129
|
|
|
/** |
|
130
|
|
|
* @var HasWallet $wallet |
|
131
|
|
|
*/ |
|
132
|
36 |
|
if ($amount && !$wallet->balance) { |
|
133
|
14 |
|
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty')); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
30 |
|
if (!$wallet->canWithdraw($amount)) { |
|
137
|
1 |
|
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds')); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
29 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Create Operation without DB::transaction |
|
143
|
|
|
* |
|
144
|
|
|
* @param Wallet $self |
|
145
|
|
|
* @param array $operations |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
32 |
|
public function multiOperation(Wallet $self, array $operations): array |
|
149
|
|
|
{ |
|
150
|
32 |
|
$amount = 0; |
|
151
|
32 |
|
$objects = []; |
|
152
|
32 |
|
foreach ($operations as $operation) { |
|
153
|
32 |
|
if ($operation->isConfirmed()) { |
|
154
|
32 |
|
$amount += $operation->getAmount(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
32 |
|
$objects[] = $operation |
|
158
|
32 |
|
->setWallet($self) |
|
159
|
32 |
|
->create(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
32 |
|
$this->addBalance($self, $amount); |
|
163
|
32 |
|
return $objects; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Create Bring with DB::transaction |
|
168
|
|
|
* |
|
169
|
|
|
* @param Bring[] $brings |
|
170
|
|
|
* @return array |
|
171
|
|
|
* @throws |
|
172
|
|
|
*/ |
|
173
|
2 |
|
public function assemble(array $brings): array |
|
174
|
|
|
{ |
|
175
|
|
|
return DB::transaction(function () use ($brings) { |
|
176
|
2 |
|
return $this->multiBrings($brings); |
|
177
|
2 |
|
}); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Create Bring without DB::transaction |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $brings |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
20 |
|
public function multiBrings(array $brings): array |
|
187
|
|
|
{ |
|
188
|
20 |
|
$objects = []; |
|
189
|
20 |
|
foreach ($brings as $bring) { |
|
190
|
20 |
|
$objects[] = $bring->create(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
20 |
|
return $objects; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param Wallet $wallet |
|
198
|
|
|
* @param int $amount |
|
199
|
|
|
* @return bool |
|
200
|
|
|
* @throws |
|
201
|
|
|
*/ |
|
202
|
32 |
|
public function addBalance(Wallet $wallet, int $amount): bool |
|
203
|
|
|
{ |
|
204
|
|
|
/** |
|
205
|
|
|
* @var ProxyService $proxy |
|
206
|
|
|
* @var WalletModel $wallet |
|
207
|
|
|
*/ |
|
208
|
32 |
|
$proxy = app(ProxyService::class); |
|
209
|
32 |
|
$balance = $wallet->balance + $amount; |
|
210
|
32 |
|
if ($proxy->has($wallet->getKey())) { |
|
|
|
|
|
|
211
|
32 |
|
$balance = $proxy->get($wallet->getKey()) + $amount; |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
32 |
|
$result = $wallet->update(compact('balance')); |
|
215
|
32 |
|
$proxy->set($wallet->getKey(), $balance); |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
32 |
|
return $result; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|