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
|
3 |
|
public function safeConfirm(Transaction $transaction): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
3 |
|
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
|
9 |
|
public function resetConfirm(Transaction $transaction): bool |
64
|
|
|
{ |
65
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
66
|
9 |
|
$self = $this; |
67
|
|
|
return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
68
|
9 |
|
$wallet = app(WalletService::class)->getWallet($self); |
|
|
|
|
69
|
9 |
|
if (!$wallet->refreshBalance()) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
if (!$transaction->confirmed) { |
74
|
4 |
|
throw new ConfirmedInvalid(trans('wallet::errors.unconfirmed_invalid')); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return $transaction->update(['confirmed' => false]) && |
78
|
|
|
|
79
|
|
|
// update balance |
80
|
4 |
|
app(CommonService::class) |
81
|
4 |
|
->addBalance($wallet, -$transaction->amount); |
82
|
9 |
|
}); |
83
|
9 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Transaction $transaction |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
5 |
|
public function safeResetConfirm(Transaction $transaction): bool |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
5 |
|
return $this->resetConfirm($transaction); |
94
|
2 |
|
} catch (\Throwable $throwable) { |
95
|
2 |
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Transaction $transaction |
101
|
|
|
* @return bool |
102
|
|
|
* @throws ConfirmedInvalid |
103
|
|
|
* @throws WalletOwnerInvalid |
104
|
|
|
*/ |
105
|
10 |
|
public function forceConfirm(Transaction $transaction): bool |
106
|
|
|
{ |
107
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
108
|
10 |
|
$self = $this; |
109
|
|
|
return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
110
|
|
|
|
111
|
10 |
|
$wallet = app(WalletService::class) |
112
|
10 |
|
->getWallet($self); |
|
|
|
|
113
|
|
|
|
114
|
10 |
|
if ($transaction->confirmed) { |
115
|
2 |
|
throw new ConfirmedInvalid(trans('wallet::errors.confirmed_invalid')); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
8 |
|
if ($wallet->getKey() !== $transaction->wallet_id) { |
|
|
|
|
119
|
2 |
|
throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid')); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
6 |
|
return $transaction->update(['confirmed' => true]) && |
123
|
|
|
|
124
|
|
|
// update balance |
125
|
6 |
|
app(CommonService::class) |
126
|
6 |
|
->addBalance($wallet, $transaction->amount); |
127
|
|
|
|
128
|
10 |
|
}); |
129
|
10 |
|
}); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|