1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
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\Services\CommonService; |
14
|
|
|
use Bavix\Wallet\Services\ProxyService; |
15
|
|
|
use Bavix\Wallet\Services\WalletService; |
16
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
18
|
|
|
use Illuminate\Support\Collection; |
19
|
|
|
use Illuminate\Support\Facades\DB; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait HasWallet |
23
|
|
|
* |
24
|
|
|
* @package Bavix\Wallet\Traits |
25
|
|
|
* |
26
|
|
|
* @property-read WalletModel $wallet |
27
|
|
|
* @property-read Collection|WalletModel[] $wallets |
28
|
|
|
* @property-read int $balance |
29
|
|
|
*/ |
30
|
|
|
trait HasWallet |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The input means in the system |
35
|
|
|
* |
36
|
|
|
* @param int $amount |
37
|
|
|
* @param array|null $meta |
38
|
|
|
* @param bool $confirmed |
39
|
|
|
* |
40
|
|
|
* @return Transaction |
41
|
|
|
*/ |
42
|
32 |
|
public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction |
43
|
|
|
{ |
44
|
32 |
|
$walletService = \app(WalletService::class); |
45
|
32 |
|
$walletService->checkAmount($amount); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var WalletModel $wallet |
49
|
|
|
*/ |
50
|
29 |
|
$wallet = $walletService->getWallet($this); |
|
|
|
|
51
|
|
|
|
52
|
29 |
|
$transactions = \app(CommonService::class)->enforce($wallet, [ |
53
|
29 |
|
(new Operation()) |
54
|
29 |
|
->setType(Transaction::TYPE_DEPOSIT) |
55
|
29 |
|
->setConfirmed($confirmed) |
56
|
29 |
|
->setAmount($amount) |
57
|
29 |
|
->setMeta($meta) |
58
|
|
|
]); |
59
|
|
|
|
60
|
29 |
|
return \current($transactions); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Magic laravel framework method, makes it |
65
|
|
|
* possible to call property balance |
66
|
|
|
* |
67
|
|
|
* Example: |
68
|
|
|
* $user1 = User::first()->load('wallet'); |
69
|
|
|
* $user2 = User::first()->load('wallet'); |
70
|
|
|
* |
71
|
|
|
* Without static: |
72
|
|
|
* var_dump($user1->balance, $user2->balance); // 100 100 |
73
|
|
|
* $user1->deposit(100); |
74
|
|
|
* $user2->deposit(100); |
75
|
|
|
* var_dump($user1->balance, $user2->balance); // 200 200 |
76
|
|
|
* |
77
|
|
|
* With static: |
78
|
|
|
* var_dump($user1->balance, $user2->balance); // 100 100 |
79
|
|
|
* $user1->deposit(100); |
80
|
|
|
* var_dump($user1->balance); // 200 |
81
|
|
|
* $user2->deposit(100); |
82
|
|
|
* var_dump($user2->balance); // 300 |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
* @throws |
86
|
|
|
*/ |
87
|
39 |
|
public function getBalanceAttribute(): int |
88
|
|
|
{ |
89
|
39 |
|
if ($this instanceof WalletModel) { |
90
|
39 |
|
$this->exists or $this->save(); |
91
|
39 |
|
$proxy = \app(ProxyService::class); |
92
|
39 |
|
if (!$proxy->has($this->getKey())) { |
|
|
|
|
93
|
39 |
|
$proxy->set($this->getKey(), (int)($this->attributes['balance'] ?? 0)); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
39 |
|
return $proxy[$this->getKey()]; |
97
|
|
|
} |
98
|
|
|
|
99
|
39 |
|
return $this->wallet->balance; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* all user actions on wallets will be in this method |
104
|
|
|
* |
105
|
|
|
* @return MorphMany |
106
|
|
|
*/ |
107
|
29 |
|
public function transactions(): MorphMany |
108
|
|
|
{ |
109
|
29 |
|
return ($this instanceof WalletModel ? $this->holder : $this) |
110
|
29 |
|
->morphMany(\config('wallet.transaction.model'), 'payable'); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* This method ignores errors that occur when transferring funds |
115
|
|
|
* |
116
|
|
|
* @param Wallet $wallet |
117
|
|
|
* @param int $amount |
118
|
|
|
* @param array|null $meta |
119
|
|
|
* @param string $status |
120
|
|
|
* @return null|Transfer |
121
|
|
|
*/ |
122
|
3 |
|
public function safeTransfer(Wallet $wallet, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): ?Transfer |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
3 |
|
return $this->transfer($wallet, $amount, $meta, $status); |
126
|
3 |
|
} catch (\Throwable $throwable) { |
127
|
3 |
|
return null; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* A method that transfers funds from host to host |
133
|
|
|
* |
134
|
|
|
* @param Wallet $wallet |
135
|
|
|
* @param int $amount |
136
|
|
|
* @param array|null $meta |
137
|
|
|
* @param string $status |
138
|
|
|
* @return Transfer |
139
|
|
|
* @throws |
140
|
|
|
*/ |
141
|
16 |
|
public function transfer(Wallet $wallet, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): Transfer |
142
|
|
|
{ |
143
|
|
|
return DB::transaction(function () use ($amount, $wallet, $meta, $status) { |
144
|
16 |
|
$fee = \app(WalletService::class) |
145
|
16 |
|
->fee($wallet, $amount); |
146
|
|
|
|
147
|
16 |
|
$withdraw = $this->withdraw($amount + $fee, $meta); |
148
|
16 |
|
$deposit = $wallet->deposit($amount, $meta); |
149
|
16 |
|
return $this->assemble($wallet, $withdraw, $deposit, $status); |
150
|
16 |
|
}); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Withdrawals from the system |
155
|
|
|
* |
156
|
|
|
* @param int $amount |
157
|
|
|
* @param array|null $meta |
158
|
|
|
* @param bool $confirmed |
159
|
|
|
* |
160
|
|
|
* @return Transaction |
161
|
|
|
*/ |
162
|
34 |
|
public function withdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction |
163
|
|
|
{ |
164
|
34 |
|
if ($amount && !$this->balance) { |
165
|
14 |
|
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty')); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
28 |
|
if (!$this->canWithdraw($amount)) { |
169
|
1 |
|
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds')); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
27 |
|
return $this->forceWithdraw($amount, $meta, $confirmed); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Checks if you can withdraw funds |
177
|
|
|
* |
178
|
|
|
* @param int $amount |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
28 |
|
public function canWithdraw(int $amount): bool |
182
|
|
|
{ |
183
|
28 |
|
return $this->balance >= $amount; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Forced to withdraw funds from system |
188
|
|
|
* |
189
|
|
|
* @param int $amount |
190
|
|
|
* @param array|null $meta |
191
|
|
|
* @param bool $confirmed |
192
|
|
|
* |
193
|
|
|
* @return Transaction |
194
|
|
|
*/ |
195
|
28 |
|
public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction |
196
|
|
|
{ |
197
|
28 |
|
$walletService = \app(WalletService::class); |
198
|
28 |
|
$walletService->checkAmount($amount); |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @var WalletModel $wallet |
202
|
|
|
*/ |
203
|
28 |
|
$wallet = $walletService->getWallet($this); |
|
|
|
|
204
|
|
|
|
205
|
28 |
|
$transactions = \app(CommonService::class)->enforce($wallet, [ |
206
|
28 |
|
(new Operation()) |
207
|
28 |
|
->setType(Transaction::TYPE_WITHDRAW) |
208
|
28 |
|
->setConfirmed($confirmed) |
209
|
28 |
|
->setAmount(-$amount) |
210
|
28 |
|
->setMeta($meta) |
211
|
|
|
]); |
212
|
|
|
|
213
|
28 |
|
return \current($transactions); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* this method adds a new transfer to the transfer table |
218
|
|
|
* |
219
|
|
|
* @param Wallet $wallet |
220
|
|
|
* @param Transaction $withdraw |
221
|
|
|
* @param Transaction $deposit |
222
|
|
|
* @param string $status |
223
|
|
|
* @return Transfer |
224
|
|
|
* @throws |
225
|
|
|
*/ |
226
|
18 |
|
protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $deposit, string $status = Transfer::STATUS_PAID): Transfer |
227
|
|
|
{ |
228
|
18 |
|
$from = ($this instanceof WalletModel ? $this : $this->wallet); |
229
|
|
|
|
230
|
18 |
|
$transfers = \app(CommonService::class)->assemble([ |
231
|
18 |
|
(new Bring()) |
232
|
18 |
|
->setStatus($status) |
233
|
18 |
|
->setDeposit($deposit) |
234
|
18 |
|
->setWithdraw($withdraw) |
235
|
18 |
|
->setFrom($from) |
236
|
18 |
|
->setTo($wallet) |
237
|
|
|
]); |
238
|
|
|
|
239
|
18 |
|
return \current($transfers); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* the forced transfer is needed when the user does not have the money and we drive it. |
244
|
|
|
* Sometimes you do. Depends on business logic. |
245
|
|
|
* |
246
|
|
|
* @param Wallet $wallet |
247
|
|
|
* @param int $amount |
248
|
|
|
* @param array|null $meta |
249
|
|
|
* @param string $status |
250
|
|
|
* @return Transfer |
251
|
|
|
*/ |
252
|
6 |
|
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null, string $status = Transfer::STATUS_TRANSFER): Transfer |
253
|
|
|
{ |
254
|
|
|
return DB::transaction(function () use ($amount, $wallet, $meta, $status) { |
255
|
6 |
|
$fee = \app(WalletService::class) |
256
|
6 |
|
->fee($wallet, $amount); |
257
|
|
|
|
258
|
6 |
|
$withdraw = $this->forceWithdraw($amount + $fee, $meta); |
259
|
6 |
|
$deposit = $wallet->deposit($amount, $meta); |
260
|
6 |
|
return $this->assemble($wallet, $withdraw, $deposit, $status); |
261
|
6 |
|
}); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* the transfer table is used to confirm the payment |
266
|
|
|
* this method receives all transfers |
267
|
|
|
* |
268
|
|
|
* @return MorphMany |
269
|
|
|
*/ |
270
|
11 |
|
public function transfers(): MorphMany |
271
|
|
|
{ |
272
|
11 |
|
return \app(WalletService::class) |
273
|
11 |
|
->getWallet($this) |
|
|
|
|
274
|
11 |
|
->morphMany(\config('wallet.transfer.model'), 'from'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get default Wallet |
279
|
|
|
* this method is used for Eager Loading |
280
|
|
|
* |
281
|
|
|
* @return MorphOne|WalletModel |
282
|
|
|
*/ |
283
|
39 |
|
public function wallet(): MorphOne |
284
|
|
|
{ |
285
|
39 |
|
return ($this instanceof WalletModel ? $this->holder : $this) |
286
|
39 |
|
->morphOne(\config('wallet.wallet.model'), 'holder') |
|
|
|
|
287
|
39 |
|
->where('slug', \config('wallet.wallet.default.slug')) |
288
|
39 |
|
->withDefault([ |
289
|
39 |
|
'name' => \config('wallet.wallet.default.name'), |
290
|
39 |
|
'slug' => \config('wallet.wallet.default.slug'), |
291
|
39 |
|
'balance' => 0, |
292
|
|
|
]); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
} |
296
|
|
|
|