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