1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\ConfirmedInvalid; |
6
|
|
|
use Bavix\Wallet\Exceptions\WalletOwnerInvalid; |
7
|
|
|
use Bavix\Wallet\Interfaces\Mathable; |
8
|
|
|
use Bavix\Wallet\Models\Transaction; |
9
|
|
|
use Bavix\Wallet\Services\CommonService; |
10
|
|
|
use Bavix\Wallet\Services\DbService; |
11
|
|
|
use Bavix\Wallet\Services\LockService; |
12
|
|
|
use Bavix\Wallet\Services\WalletService; |
13
|
|
|
|
14
|
|
|
trait CanConfirm |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Transaction $transaction |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
11 |
|
public function confirm(Transaction $transaction): bool |
23
|
|
|
{ |
24
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
25
|
11 |
|
$self = $this; |
26
|
|
|
return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
27
|
11 |
|
$wallet = app(WalletService::class)->getWallet($self); |
|
|
|
|
28
|
11 |
|
if (!$wallet->refreshBalance()) { |
29
|
1 |
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
10 |
|
if ($transaction->type === Transaction::TYPE_WITHDRAW) { |
33
|
2 |
|
app(CommonService::class)->verifyWithdraw( |
34
|
2 |
|
$wallet, |
35
|
2 |
|
app(Mathable::class)->abs($transaction->amount) |
|
|
|
|
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
8 |
|
return $self->forceConfirm($transaction); |
40
|
11 |
|
}); |
41
|
11 |
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Transaction $transaction |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
2 |
|
public function safeConfirm(Transaction $transaction): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
2 |
|
return $this->confirm($transaction); |
52
|
2 |
|
} catch (\Throwable $throwable) { |
53
|
2 |
|
return false; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Removal of confirmation (forced), use at your own peril and risk. |
59
|
|
|
* |
60
|
|
|
* @param Transaction $transaction |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
5 |
|
public function resetConfirm(Transaction $transaction): bool |
64
|
|
|
{ |
65
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
66
|
5 |
|
$self = $this; |
67
|
|
|
return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
68
|
5 |
|
$wallet = app(WalletService::class)->getWallet($self); |
|
|
|
|
69
|
5 |
|
if (!$wallet->refreshBalance()) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
if (!$transaction->confirmed) { |
74
|
|
|
throw new ConfirmedInvalid(trans('wallet::errors.unconfirmed_invalid')); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
if ($wallet->getKey() !== $transaction->wallet_id) { |
|
|
|
|
78
|
|
|
throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
return $transaction->update(['confirmed' => false]) && |
82
|
|
|
|
83
|
|
|
// update balance |
84
|
4 |
|
app(CommonService::class) |
85
|
4 |
|
->addBalance($wallet, -$transaction->amount); |
86
|
5 |
|
}); |
87
|
5 |
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Transaction $transaction |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
2 |
|
public function safeResetConfirm(Transaction $transaction): bool |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
2 |
|
return $this->resetConfirm($transaction); |
98
|
|
|
} catch (\Throwable $throwable) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Transaction $transaction |
105
|
|
|
* @return bool |
106
|
|
|
* @throws ConfirmedInvalid |
107
|
|
|
* @throws WalletOwnerInvalid |
108
|
|
|
*/ |
109
|
10 |
|
public function forceConfirm(Transaction $transaction): bool |
110
|
|
|
{ |
111
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
112
|
10 |
|
$self = $this; |
113
|
|
|
return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
114
|
|
|
|
115
|
10 |
|
$wallet = app(WalletService::class) |
116
|
10 |
|
->getWallet($self); |
|
|
|
|
117
|
|
|
|
118
|
10 |
|
if ($transaction->confirmed) { |
119
|
2 |
|
throw new ConfirmedInvalid(trans('wallet::errors.confirmed_invalid')); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
8 |
|
if ($wallet->getKey() !== $transaction->wallet_id) { |
|
|
|
|
123
|
2 |
|
throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid')); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
6 |
|
return $transaction->update(['confirmed' => true]) && |
127
|
|
|
|
128
|
|
|
// update balance |
129
|
6 |
|
app(CommonService::class) |
130
|
6 |
|
->addBalance($wallet, $transaction->amount); |
131
|
|
|
|
132
|
10 |
|
}); |
133
|
10 |
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|