|
2 | 2 |
|
3 | 3 | namespace Bavix\Wallet\Traits; |
4 | 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 | | - |
| 5 | +/** |
| 6 | + * Trait CanBePaid |
| 7 | + * @package Bavix\Wallet\Traits |
| 8 | + * @deprecated use trait CanPay |
| 9 | + * @see https://github.com/bavix/laravel-wallet/issues/19 |
| 10 | + */ |
12 | 11 | trait CanBePaid |
13 | 12 | { |
14 | | - |
15 | | - use HasWallet; |
16 | | - |
17 | | - /** |
18 | | - * @param Product $product |
19 | | - * @return Transfer |
20 | | - */ |
21 | | - public function payFree(Product $product): Transfer |
22 | | - { |
23 | | - if (!$product->canBuy($this)) { |
24 | | - throw new ProductEnded(trans('wallet::errors.product_stock')); |
25 | | - } |
26 | | - |
27 | | - 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 | | - public function pay(Product $product, bool $force = false): Transfer |
37 | | - { |
38 | | - if (!$product->canBuy($this, $force)) { |
39 | | - throw new ProductEnded(trans('wallet::errors.product_stock')); |
40 | | - } |
41 | | - |
42 | | - if ($force) { |
43 | | - return $this->forceTransfer($product, $product->getAmountProduct(), $product->getMetaProduct()); |
44 | | - } |
45 | | - |
46 | | - 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 | | - public function safePay(Product $product, bool $force = false): ?Transfer |
55 | | - { |
56 | | - try { |
57 | | - return $this->pay($product, $force); |
58 | | - } catch (\Throwable $throwable) { |
59 | | - return null; |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * @param Product $product |
65 | | - * @return Transfer |
66 | | - * @throws |
67 | | - */ |
68 | | - public function forcePay(Product $product): Transfer |
69 | | - { |
70 | | - return $this->pay($product, true); |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * @param Product $product |
75 | | - * @param bool $gifts |
76 | | - * @return null|Transfer |
77 | | - */ |
78 | | - public function paid(Product $product, bool $gifts = false): ?Transfer |
79 | | - { |
80 | | - $status = [Transfer::STATUS_PAID]; |
81 | | - if ($gifts) { |
82 | | - $status[] = Transfer::STATUS_GIFT; |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * @var Model $product |
87 | | - * @var Transfer $query |
88 | | - */ |
89 | | - $query = $this->transfers(); |
90 | | - return $query |
91 | | - ->where('to_type', $product->getMorphClass()) |
92 | | - ->where('to_id', $product->getKey()) |
93 | | - ->whereIn('status', $status) |
94 | | - ->orderBy('id', 'desc') |
95 | | - ->first(); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * @param Product $product |
100 | | - * @param bool $force |
101 | | - * @param bool $gifts |
102 | | - * @return bool |
103 | | - * @throws |
104 | | - */ |
105 | | - public function refund(Product $product, bool $force = false, bool $gifts = false): bool |
106 | | - { |
107 | | - $transfer = $this->paid($product, $gifts); |
108 | | - |
109 | | - if (!$transfer) { |
110 | | - throw (new ModelNotFoundException()) |
111 | | - ->setModel($this->transfers()->getMorphClass()); |
112 | | - } |
113 | | - |
114 | | - return DB::transaction(function () use ($product, $transfer, $force) { |
115 | | - $transfer->load('withdraw.payable'); |
116 | | - |
117 | | - if ($force) { |
118 | | - $product->forceTransfer( |
119 | | - $transfer->withdraw->payable, |
120 | | - $transfer->deposit->amount, |
121 | | - $product->getMetaProduct() |
122 | | - ); |
123 | | - } else { |
124 | | - $product->transfer( |
125 | | - $transfer->withdraw->payable, |
126 | | - $transfer->deposit->amount, |
127 | | - $product->getMetaProduct() |
128 | | - ); |
129 | | - } |
130 | | - |
131 | | - return $transfer->update([ |
132 | | - 'status' => Transfer::STATUS_REFUND, |
133 | | - 'status_last' => $transfer->status, |
134 | | - ]); |
135 | | - }); |
136 | | - } |
137 | | - |
138 | | - /** |
139 | | - * @param Product $product |
140 | | - * @param bool $force |
141 | | - * @param bool $gifts |
142 | | - * @return bool |
143 | | - */ |
144 | | - public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool |
145 | | - { |
146 | | - try { |
147 | | - return $this->refund($product, $force, $gifts); |
148 | | - } catch (\Throwable $throwable) { |
149 | | - return false; |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - /** |
154 | | - * @param Product $product |
155 | | - * @param bool $gifts |
156 | | - * @return bool |
157 | | - * @throws |
158 | | - */ |
159 | | - public function forceRefund(Product $product, bool $gifts = false): bool |
160 | | - { |
161 | | - return $this->refund($product, true, $gifts); |
162 | | - } |
163 | | - |
164 | | - /** |
165 | | - * @param Product $product |
166 | | - * @param bool $force |
167 | | - * @return bool |
168 | | - */ |
169 | | - public function refundGift(Product $product, bool $force = false): bool |
170 | | - { |
171 | | - return $this->refund($product, $force, true); |
172 | | - } |
173 | | - |
174 | | - /** |
175 | | - * @param Product $product |
176 | | - * @param bool $force |
177 | | - * @return bool |
178 | | - */ |
179 | | - public function safeRefundGift(Product $product, bool $force = false): bool |
180 | | - { |
181 | | - try { |
182 | | - return $this->refundGift($product, $force); |
183 | | - } catch (\Throwable $throwable) { |
184 | | - return false; |
185 | | - } |
186 | | - } |
187 | | - |
188 | | - /** |
189 | | - * @param Product $product |
190 | | - * @return bool |
191 | | - * @throws |
192 | | - */ |
193 | | - public function forceRefundGift(Product $product): bool |
194 | | - { |
195 | | - return $this->refundGift($product, true); |
196 | | - } |
197 | | - |
| 13 | + use CanPay; |
198 | 14 | } |
0 commit comments