|
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 CanPay |
|
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
|
|
|
* @param bool $gifts |
|
76
|
|
|
* @return null|Transfer |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function paid(Product $product, bool $gifts = false): ?Transfer |
|
79
|
|
|
{ |
|
80
|
9 |
|
$status = [Transfer::STATUS_PAID]; |
|
81
|
9 |
|
if ($gifts) { |
|
82
|
2 |
|
$status[] = Transfer::STATUS_GIFT; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var Model $product |
|
87
|
|
|
* @var Transfer $query |
|
88
|
|
|
*/ |
|
89
|
9 |
|
$query = $this->transfers(); |
|
90
|
|
|
return $query |
|
91
|
9 |
|
->where('to_type', $product->getMorphClass()) |
|
92
|
9 |
|
->where('to_id', $product->getKey()) |
|
93
|
9 |
|
->whereIn('status', $status) |
|
94
|
9 |
|
->orderBy('id', 'desc') |
|
95
|
9 |
|
->first(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Product $product |
|
100
|
|
|
* @param bool $force |
|
101
|
|
|
* @param bool $gifts |
|
102
|
|
|
* @return bool |
|
103
|
|
|
* @throws |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function refund(Product $product, bool $force = false, bool $gifts = false): bool |
|
106
|
|
|
{ |
|
107
|
5 |
|
$transfer = $this->paid($product, $gifts); |
|
108
|
|
|
|
|
109
|
5 |
|
if (!$transfer) { |
|
|
|
|
|
|
110
|
2 |
|
throw (new ModelNotFoundException()) |
|
111
|
2 |
|
->setModel($this->transfers()->getMorphClass()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return DB::transaction(function () use ($product, $transfer, $force) { |
|
115
|
5 |
|
$transfer->load('withdraw.payable'); |
|
116
|
|
|
|
|
117
|
5 |
|
if ($force) { |
|
118
|
2 |
|
$product->forceTransfer( |
|
119
|
2 |
|
$transfer->withdraw->payable, |
|
120
|
2 |
|
$transfer->deposit->amount, |
|
121
|
2 |
|
$product->getMetaProduct() |
|
122
|
|
|
); |
|
123
|
|
|
} else { |
|
124
|
5 |
|
$product->transfer( |
|
125
|
5 |
|
$transfer->withdraw->payable, |
|
126
|
5 |
|
$transfer->deposit->amount, |
|
127
|
5 |
|
$product->getMetaProduct() |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
5 |
|
return $transfer->update([ |
|
132
|
5 |
|
'status' => Transfer::STATUS_REFUND, |
|
133
|
5 |
|
'status_last' => $transfer->status, |
|
134
|
|
|
]); |
|
135
|
5 |
|
}); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param Product $product |
|
140
|
|
|
* @param bool $force |
|
141
|
|
|
* @param bool $gifts |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool |
|
145
|
|
|
{ |
|
146
|
|
|
try { |
|
147
|
3 |
|
return $this->refund($product, $force, $gifts); |
|
148
|
3 |
|
} catch (\Throwable $throwable) { |
|
149
|
3 |
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param Product $product |
|
155
|
|
|
* @param bool $gifts |
|
156
|
|
|
* @return bool |
|
157
|
|
|
* @throws |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function forceRefund(Product $product, bool $gifts = false): bool |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->refund($product, true, $gifts); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param Product $product |
|
166
|
|
|
* @param bool $force |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function refundGift(Product $product, bool $force = false): bool |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->refund($product, $force, true); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param Product $product |
|
176
|
|
|
* @param bool $force |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function safeRefundGift(Product $product, bool $force = false): bool |
|
180
|
|
|
{ |
|
181
|
|
|
try { |
|
182
|
1 |
|
return $this->refundGift($product, $force); |
|
183
|
1 |
|
} catch (\Throwable $throwable) { |
|
184
|
1 |
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param Product $product |
|
190
|
|
|
* @return bool |
|
191
|
|
|
* @throws |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function forceRefundGift(Product $product): bool |
|
194
|
|
|
{ |
|
195
|
1 |
|
return $this->refundGift($product, true); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|