|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\ProductEnded; |
|
6
|
|
|
use Bavix\Wallet\Interfaces\Product; |
|
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
|
|
12
|
|
|
trait CanBePaid |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
use HasWallet; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Product $product |
|
19
|
|
|
* @return Transfer |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public function payFree(Product $product): Transfer |
|
22
|
|
|
{ |
|
23
|
2 |
|
if (!$product->canBuy($this)) { |
|
|
|
|
|
|
24
|
1 |
|
throw new ProductEnded(trans('wallet::errors.product_stock')); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
2 |
|
return $this->transfer($product, 0, $product->getMetaProduct()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param Product $product |
|
32
|
|
|
* @param bool $force |
|
33
|
|
|
* @return Transfer |
|
34
|
|
|
* @throws |
|
35
|
|
|
*/ |
|
36
|
6 |
|
public function pay(Product $product, bool $force = false): Transfer |
|
37
|
|
|
{ |
|
38
|
6 |
|
if (!$product->canBuy($this, $force)) { |
|
|
|
|
|
|
39
|
2 |
|
throw new ProductEnded(trans('wallet::errors.product_stock')); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
if ($force) { |
|
43
|
1 |
|
return $this->forceTransfer($product, $product->getAmountProduct(), $product->getMetaProduct()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
return $this->transfer($product, $product->getAmountProduct(), $product->getMetaProduct()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Product $product |
|
51
|
|
|
* @param bool $force |
|
52
|
|
|
* @return Transfer|null |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function safePay(Product $product, bool $force = false): ?Transfer |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
1 |
|
return $this->pay($product, $force); |
|
58
|
1 |
|
} catch (\Throwable $throwable) { |
|
59
|
1 |
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Product $product |
|
65
|
|
|
* @return Transfer |
|
66
|
|
|
* @throws |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function forcePay(Product $product): Transfer |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->pay($product, true); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Product $product |
|
75
|
|
|
* @return null|Transfer |
|
76
|
|
|
*/ |
|
77
|
7 |
|
public function paid(Product $product): ?Transfer |
|
78
|
|
|
{ |
|
79
|
|
|
/** |
|
80
|
|
|
* @var Model $product |
|
81
|
|
|
*/ |
|
82
|
7 |
|
return $this->transfers() |
|
83
|
7 |
|
->where('to_type', $product->getMorphClass()) |
|
84
|
7 |
|
->where('to_id', $product->getKey()) |
|
85
|
7 |
|
->where('refund', 0) |
|
86
|
7 |
|
->orderBy('id', 'desc') |
|
87
|
7 |
|
->first(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Product $product |
|
92
|
|
|
* @param bool $force |
|
93
|
|
|
* @return bool |
|
94
|
|
|
* @throws |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function refund(Product $product, bool $force = false): bool |
|
97
|
|
|
{ |
|
98
|
4 |
|
$transfer = $this->paid($product); |
|
99
|
|
|
|
|
100
|
4 |
|
if (!$transfer) { |
|
101
|
1 |
|
throw (new ModelNotFoundException()) |
|
102
|
1 |
|
->setModel($this->transfers()->getMorphClass()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return DB::transaction(function () use ($product, $transfer, $force) { |
|
106
|
4 |
|
if ($force) { |
|
107
|
1 |
|
$product->forceTransfer($this, $transfer->deposit->amount, $product->getMetaProduct()); |
|
|
|
|
|
|
108
|
|
|
} else { |
|
109
|
4 |
|
$product->transfer($this, $transfer->deposit->amount, $product->getMetaProduct()); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
4 |
|
return $transfer->update(['refund' => 1]); |
|
113
|
4 |
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Product $product |
|
118
|
|
|
* @param bool $force |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function safeRefund(Product $product, bool $force = false): bool |
|
122
|
|
|
{ |
|
123
|
|
|
try { |
|
124
|
2 |
|
return $this->refund($product, $force); |
|
125
|
2 |
|
} catch (\Throwable $throwable) { |
|
126
|
2 |
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Product $product |
|
132
|
|
|
* @return bool |
|
133
|
|
|
* @throws |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function forceRefund(Product $product): bool |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->refund($product, true); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|