1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moecasts\Laravel\Wallet\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
6
|
|
|
use Moecasts\Laravel\Wallet\Exceptions\ProductEnded; |
7
|
|
|
use Moecasts\Laravel\Wallet\Interfaces\Product; |
8
|
|
|
use Moecasts\Laravel\Wallet\Interfaces\Refundable; |
9
|
|
|
use Moecasts\Laravel\Wallet\Models\Transfer; |
10
|
|
|
|
11
|
|
|
trait CanPay { |
12
|
6 |
|
public function pay(Product $product, string $action = Transfer::ACTION_PAID, bool $force = false): Transfer |
13
|
|
|
{ |
14
|
6 |
|
if (! $product->canBePaid($this, $force)) { |
|
|
|
|
15
|
1 |
|
throw new ProductEnded(trans('wallet::errors.product_stock')); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
5 |
|
if ($force) { |
19
|
|
|
return $this->forceTransfer( |
|
|
|
|
20
|
|
|
$product, |
21
|
|
|
$product->getProductAmount($action), |
22
|
|
|
$product->getProductMeta($action), |
23
|
|
|
$action |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
5 |
|
return $this->transfer( |
|
|
|
|
28
|
5 |
|
$product, |
29
|
5 |
|
$product->getProductAmount($action), |
30
|
5 |
|
$product->getProductMeta($action), |
31
|
5 |
|
$action |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function safePay(Product $product, string $action = Transfer::ACTION_PAID, bool $force = false): ?Transfer |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
1 |
|
return $this->pay($product, $action, $force); |
39
|
1 |
|
} catch (\Throwable $throwable) { |
40
|
1 |
|
return null; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
public function paid(Product $product, string $action = Transfer::ACTION_PAID): ?Transfer |
45
|
|
|
{ |
46
|
4 |
|
$action = [$action]; |
47
|
|
|
|
48
|
4 |
|
$query = $this->holderTransfers(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $query |
51
|
4 |
|
->where('refund', false) |
52
|
4 |
|
->where('to_type', $product->getMorphClass()) |
|
|
|
|
53
|
4 |
|
->where('to_id', $product->getKey()) |
|
|
|
|
54
|
4 |
|
->whereIn('action', $action) |
55
|
4 |
|
->orderBy('id', 'desc') |
56
|
4 |
|
->first(); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function refund(Refundable $product, string $action = Transfer::ACTION_PAID) |
60
|
|
|
{ |
61
|
2 |
|
$transfer = $this->paid($product, $action); |
62
|
|
|
|
63
|
2 |
|
if (! $transfer) { |
64
|
2 |
|
throw (new ModelNotFoundException()) |
65
|
2 |
|
->setModel(config('wallet.transfer.model')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return \DB::transaction(function () use ($transfer) { |
69
|
1 |
|
$transfer->withdraw->update([ |
70
|
1 |
|
'confirmed' => false, |
71
|
|
|
]); |
72
|
|
|
|
73
|
1 |
|
$transfer->deposit->update([ |
74
|
1 |
|
'confirmed' => false, |
75
|
|
|
]); |
76
|
|
|
|
77
|
1 |
|
$transfer->update([ |
78
|
1 |
|
'refund' => true, |
79
|
|
|
]); |
80
|
|
|
|
81
|
1 |
|
return $transfer->fromWallet->refreshBalance() && |
82
|
1 |
|
$transfer->toWallet->refreshBalance(); |
83
|
1 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function safeRefund(Product $product, string $action = Transfer::ACTION_PAID) |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
1 |
|
return $this->refund($product, $action); |
90
|
1 |
|
} catch (\Throwable $throwable) { |
91
|
1 |
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.