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