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\Models\Transaction; |
8
|
|
|
use Bavix\Wallet\Services\CommonService; |
9
|
|
|
use Bavix\Wallet\Services\LockService; |
10
|
|
|
use Bavix\Wallet\Services\WalletService; |
11
|
|
|
use Illuminate\Support\Facades\DB; |
12
|
|
|
|
13
|
|
|
trait CanConfirm |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Transaction $transaction |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public function confirm(Transaction $transaction): bool |
22
|
|
|
{ |
23
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
24
|
|
|
$self = $this; |
25
|
|
|
return DB::transaction(static function() use ($self, $transaction) { |
26
|
|
|
$wallet = app(WalletService::class) |
27
|
|
|
->getWallet($self); |
|
|
|
|
28
|
|
|
|
29
|
|
|
if (!$wallet->refreshBalance()) { |
30
|
|
|
// @codeCoverageIgnoreStart |
31
|
|
|
return false; |
32
|
|
|
// @codeCoverageIgnoreEnd |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($transaction->type === Transaction::TYPE_WITHDRAW) { |
36
|
|
|
app(CommonService::class)->verifyWithdraw( |
37
|
|
|
$wallet, |
38
|
|
|
\abs($transaction->amount) |
|
|
|
|
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $self->forceConfirm($transaction); |
43
|
|
|
}); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Transaction $transaction |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function safeConfirm(Transaction $transaction): bool |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
return $this->confirm($transaction); |
55
|
|
|
} catch (\Throwable $throwable) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Transaction $transaction |
62
|
|
|
* @return bool |
63
|
|
|
* @throws ConfirmedInvalid |
64
|
|
|
* @throws WalletOwnerInvalid |
65
|
|
|
*/ |
66
|
|
|
public function forceConfirm(Transaction $transaction): bool |
67
|
|
|
{ |
68
|
|
|
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
69
|
|
|
$self = $this; |
70
|
|
|
return DB::transaction(static function () use ($self, $transaction) { |
71
|
|
|
|
72
|
|
|
$wallet = app(WalletService::class) |
73
|
|
|
->getWallet($self); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ($transaction->confirmed) { |
76
|
|
|
throw new ConfirmedInvalid(trans('wallet::errors.confirmed_invalid')); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($wallet->id !== $transaction->wallet_id) { |
|
|
|
|
80
|
|
|
throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid')); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $transaction->update(['confirmed' => true]) && |
84
|
|
|
|
85
|
|
|
// update balance |
86
|
|
|
app(CommonService::class) |
87
|
|
|
->addBalance($wallet, $transaction->amount); |
88
|
|
|
|
89
|
|
|
}); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|