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 Bavix\Wallet\Objects\Cart; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Throwable; |
12
|
|
|
use function array_unique; |
13
|
|
|
use function count; |
14
|
|
|
|
15
|
|
|
trait CartPay |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use HasWallet; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Cart $cart |
22
|
|
|
* @return Transfer[] |
23
|
|
|
* @throws |
24
|
|
|
*/ |
25
|
2 |
|
public function payFreeCart(Cart $cart): array |
26
|
|
|
{ |
27
|
2 |
|
if (!$cart->canBuy($this)) { |
|
|
|
|
28
|
1 |
|
throw new ProductEnded(trans('wallet::errors.product_stock')); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return DB::transaction(function () use ($cart) { |
32
|
2 |
|
$results = []; |
33
|
2 |
|
foreach ($cart->getItems() as $product) { |
34
|
2 |
|
$results[] = $this->transfer( |
35
|
2 |
|
$product, |
36
|
2 |
|
0, |
37
|
2 |
|
$product->getMetaProduct(), |
38
|
2 |
|
Transfer::STATUS_PAID |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
return $results; |
43
|
2 |
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Cart $cart |
48
|
|
|
* @param bool $force |
49
|
|
|
* @return Transfer[] |
50
|
|
|
*/ |
51
|
1 |
|
public function safePayCart(Cart $cart, bool $force = null): array |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
1 |
|
return $this->payCart($cart, $force); |
55
|
1 |
|
} catch (Throwable $throwable) { |
56
|
1 |
|
return []; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Cart $cart |
62
|
|
|
* @param bool $force |
63
|
|
|
* @return Transfer[] |
64
|
|
|
* @throws |
65
|
|
|
*/ |
66
|
8 |
|
public function payCart(Cart $cart, bool $force = null): array |
67
|
|
|
{ |
68
|
8 |
|
if (!$cart->canBuy($this, $force)) { |
|
|
|
|
69
|
2 |
|
throw new ProductEnded(trans('wallet::errors.product_stock')); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return DB::transaction(function () use ($cart, $force) { |
73
|
|
|
|
74
|
8 |
|
$results = []; |
75
|
8 |
|
foreach ($cart->getItems() as $product) { |
76
|
8 |
|
if ($force) { |
77
|
1 |
|
$results[] = $this->forceTransfer( |
78
|
1 |
|
$product, |
79
|
1 |
|
$product->getAmountProduct(), |
80
|
1 |
|
$product->getMetaProduct(), |
81
|
1 |
|
Transfer::STATUS_PAID |
82
|
|
|
); |
83
|
|
|
|
84
|
1 |
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
$results[] = $this->transfer( |
88
|
7 |
|
$product, |
89
|
7 |
|
$product->getAmountProduct(), |
90
|
7 |
|
$product->getMetaProduct(), |
91
|
7 |
|
Transfer::STATUS_PAID |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
return $results; |
96
|
|
|
|
97
|
8 |
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Cart $cart |
102
|
|
|
* @return Transfer[] |
103
|
|
|
* @throws |
104
|
|
|
*/ |
105
|
1 |
|
public function forcePayCart(Cart $cart): array |
106
|
|
|
{ |
107
|
1 |
|
return $this->payCart($cart, true); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Cart $cart |
112
|
|
|
* @param bool $force |
113
|
|
|
* @param bool $gifts |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
3 |
|
public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = null): bool |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
3 |
|
return $this->refundCart($cart, $force, $gifts); |
120
|
3 |
|
} catch (Throwable $throwable) { |
121
|
3 |
|
return false; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Cart $cart |
127
|
|
|
* @param bool $force |
128
|
|
|
* @param bool $gifts |
129
|
|
|
* @return bool |
130
|
|
|
* @throws |
131
|
|
|
*/ |
132
|
7 |
|
public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool |
133
|
|
|
{ |
134
|
|
|
return DB::transaction(function () use ($cart, $force, $gifts) { |
135
|
|
|
|
136
|
7 |
|
$results = []; |
137
|
7 |
|
$transfers = $cart->hasPaid($this, $gifts); |
|
|
|
|
138
|
7 |
|
foreach ($cart->getItems() as $key => $product) { |
139
|
7 |
|
$transfer = $transfers[$key]; |
140
|
7 |
|
$transfer->load('withdraw.wallet'); |
141
|
|
|
|
142
|
7 |
|
if ($force) { |
143
|
2 |
|
$product->forceTransfer( |
144
|
2 |
|
$transfer->withdraw->wallet, |
145
|
2 |
|
$transfer->deposit->amount, |
146
|
2 |
|
$product->getMetaProduct() |
147
|
|
|
); |
148
|
|
|
} else { |
149
|
7 |
|
$product->transfer( |
150
|
7 |
|
$transfer->withdraw->wallet, |
151
|
7 |
|
$transfer->deposit->amount, |
152
|
7 |
|
$product->getMetaProduct() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
$results[] = $transfer->update([ |
157
|
7 |
|
'status' => Transfer::STATUS_REFUND, |
158
|
7 |
|
'status_last' => $transfer->status, |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
7 |
|
return count(array_unique($results)) === 1; |
163
|
|
|
|
164
|
7 |
|
}); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Cart $cart |
169
|
|
|
* @param bool $gifts |
170
|
|
|
* @return bool |
171
|
|
|
* @throws |
172
|
|
|
*/ |
173
|
1 |
|
public function forceRefundCart(Cart $cart, bool $gifts = null): bool |
174
|
|
|
{ |
175
|
1 |
|
return $this->refundCart($cart, true, $gifts); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param Cart $cart |
180
|
|
|
* @param bool $force |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
1 |
|
public function safeRefundGiftCart(Cart $cart, bool $force = null): bool |
184
|
|
|
{ |
185
|
|
|
try { |
186
|
1 |
|
return $this->refundGiftCart($cart, $force); |
187
|
1 |
|
} catch (Throwable $throwable) { |
188
|
1 |
|
return false; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param Cart $cart |
194
|
|
|
* @param bool $force |
195
|
|
|
* @return bool |
196
|
|
|
* @throws |
197
|
|
|
*/ |
198
|
1 |
|
public function refundGiftCart(Cart $cart, bool $force = null): bool |
199
|
|
|
{ |
200
|
1 |
|
return $this->refundCart($cart, $force, true); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param Cart $cart |
205
|
|
|
* @return bool |
206
|
|
|
* @throws |
207
|
|
|
*/ |
208
|
1 |
|
public function forceRefundGiftCart(Cart $cart): bool |
209
|
|
|
{ |
210
|
1 |
|
return $this->refundGiftCart($cart, true); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Checks acquired product your wallet. |
215
|
|
|
* |
216
|
|
|
* @param Product $product |
217
|
|
|
* @param bool $gifts |
218
|
|
|
* @return null|Transfer |
219
|
|
|
*/ |
220
|
11 |
|
public function paid(Product $product, bool $gifts = null): ?Transfer |
221
|
|
|
{ |
222
|
11 |
|
$status = [Transfer::STATUS_PAID]; |
223
|
11 |
|
if ($gifts) { |
224
|
2 |
|
$status[] = Transfer::STATUS_GIFT; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @var Model $product |
229
|
|
|
* @var Transfer $query |
230
|
|
|
*/ |
231
|
11 |
|
$query = $this->transfers(); |
232
|
|
|
return $query |
233
|
11 |
|
->where('to_type', $product->getMorphClass()) |
234
|
11 |
|
->where('to_id', $product->getKey()) |
235
|
11 |
|
->whereIn('status', $status) |
236
|
11 |
|
->orderBy('id', 'desc') |
237
|
11 |
|
->first(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|