Total Complexity | 45 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
Complex classes like CheckoutController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CheckoutController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class CheckoutController extends InfoController |
||
24 | { |
||
25 | use TaxCalculation; |
||
|
|||
26 | |||
27 | public $subscription; |
||
28 | public $plan; |
||
29 | public $templateController; |
||
30 | public $product; |
||
31 | public $price; |
||
32 | public $user; |
||
33 | public $setting; |
||
34 | public $template; |
||
35 | public $order; |
||
36 | public $addon; |
||
37 | public $invoice; |
||
38 | public $invoiceItem; |
||
39 | public $mailchimp; |
||
40 | |||
41 | public function __construct() |
||
75 | |||
76 | // $mailchimp = new MailChimpController(); |
||
77 | // $this->mailchimp = $mailchimp; |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * When Proceed to chekout button clicked first request comes here |
||
82 | */ |
||
83 | public function checkoutForm(Request $request) |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get all the Attributes Sent From the cart along with Tax Conditions. |
||
130 | * |
||
131 | * @param array $content Collection of the Cart Values |
||
132 | * |
||
133 | * @return array Items along with their details,Attributes(Currency,Agents) and Tax Conditions |
||
134 | */ |
||
135 | public function getAttributes($content) |
||
136 | { |
||
137 | try { |
||
138 | if (count($content) > 0) {//after ProductPurchase this is not true as cart is cleared |
||
139 | foreach ($content as $item) { |
||
140 | $cart_currency = $item->attributes->currency; //Get the currency of Product in the cart |
||
141 | $currency = \Auth::user()->currency != $cart_currency ? \Auth::user()->currency : $cart_currency; //If User Currency and cart currency are different the currency es set to user currency. |
||
142 | if ($cart_currency != $currency) { |
||
143 | $id = $item->id; |
||
144 | Cart::remove($id); |
||
145 | } |
||
146 | $require_domain = $item->associatedModel->require_domain; |
||
147 | $require = []; |
||
148 | if ($require_domain) { |
||
149 | $require[$key] = $item->id; |
||
150 | } |
||
151 | $taxConditions = $this->calculateTax($item->id, \Auth::user()->state, \Auth::user()->country); //Calculate Tax Condition by passing ProductId |
||
152 | Cart::condition($taxConditions); |
||
153 | Cart::remove($item->id); |
||
154 | |||
155 | //Return array of Product Details,attributes and their conditions |
||
156 | $items[] = ['id' => $item->id, 'name' => $item->name, 'price' => $item->price, |
||
157 | 'quantity' => $item->quantity, 'attributes' => ['currency'=> $cart_currency, 'symbol'=>$item->attributes->symbol, 'agents'=> $item->attributes->agents], 'associatedModel' => Product::find($item->id), 'conditions' => $taxConditions, ]; |
||
158 | } |
||
159 | Cart::add($items); |
||
160 | |||
161 | return $taxConditions; |
||
162 | } |
||
163 | } catch (\Exception $ex) { |
||
164 | app('log')->error($ex->getMessage()); |
||
165 | Bugsnag::notifyException($ex); |
||
166 | |||
167 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | public function payNow($invoiceid) |
||
172 | { |
||
173 | try { |
||
174 | $paid = 0; |
||
175 | $invoice = $this->invoice->find($invoiceid); |
||
176 | if ($invoice->user_id != \Auth::user()->id) { |
||
177 | throw new \Exception('Cannot initiate payment. Invalid modification of data'); |
||
178 | } |
||
179 | if (count($invoice->payment()->get())) {//If partial payment is made |
||
180 | $paid = array_sum($invoice->payment()->pluck('amount')->toArray()); |
||
181 | $invoice->grand_total = $invoice->grand_total - $paid; |
||
182 | } |
||
183 | $items = new \Illuminate\Support\Collection(); |
||
184 | if ($invoice) { |
||
185 | $items = $invoice->invoiceItem()->get(); |
||
186 | if (count($items) > 0) { |
||
187 | $product = $this->product($invoiceid); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return view('themes.default1.front.paynow', compact('invoice', 'items', 'product', 'paid')); |
||
192 | } catch (\Exception $ex) { |
||
193 | app('log')->error($ex->getMessage()); |
||
194 | Bugsnag::notifyException($ex); |
||
195 | |||
196 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public function postCheckout(Request $request) |
||
201 | { |
||
202 | $cost = $request->input('cost'); |
||
203 | if (Cart::getSubTotal() != 0 || $cost > 0) { |
||
204 | $this->validate($request, [ |
||
205 | 'payment_gateway'=> 'required', |
||
206 | ], [ |
||
207 | 'payment_gateway.required'=> 'Please Select a Payment Gateway', |
||
208 | ]); |
||
209 | } |
||
210 | try { |
||
211 | $invoice_controller = new \App\Http\Controllers\Order\InvoiceController(); |
||
212 | $info_cont = new \App\Http\Controllers\Front\InfoController(); |
||
213 | $payment_method = $request->input('payment_gateway'); |
||
214 | \Session::put('payment_method', $payment_method); |
||
215 | $paynow = $this->checkregularPaymentOrRenewal($request->input('invoice_id')); |
||
216 | $cost = $request->input('cost'); |
||
217 | $state = $this->getState(); |
||
218 | if ($paynow === false) {//When regular payment |
||
219 | $invoice = $invoice_controller->generateInvoice(); |
||
220 | $amount = intval(Cart::getSubTotal()); |
||
221 | if ($amount) {//If payment is for paid product |
||
222 | \Event::dispatch(new \App\Events\PaymentGateway(['request' => $request, 'invoice' => $invoice])); |
||
223 | } else { |
||
224 | $date = getDateHtml($invoice->date); |
||
225 | $product = $this->product($invoice->id); |
||
226 | $items = $invoice->invoiceItem()->get(); |
||
227 | $url = ''; |
||
228 | $this->checkoutAction($invoice); //For free product generate invoice without payment |
||
229 | $url = view('themes.default1.front.postCheckoutTemplate', compact('invoice', 'date', 'product', 'items', ))->render(); |
||
230 | // } |
||
231 | \Cart::clear(); |
||
232 | |||
233 | return redirect('checkout')->with('success', $url); |
||
234 | } |
||
235 | } else {//When renewal, pending payments |
||
236 | $invoiceid = $request->input('invoice_id'); |
||
237 | $invoice = $this->invoice->find($invoiceid); |
||
238 | $amount = intval($invoice->grand_total); |
||
239 | if ($amount) {//If payment is for paid product |
||
240 | \Event::dispatch(new \App\Events\PaymentGateway(['request' => $request, 'invoice' => $invoice])); |
||
241 | } else { |
||
242 | $control = new \App\Http\Controllers\Order\RenewController(); |
||
243 | $payment = new \App\Http\Controllers\Order\InvoiceController(); |
||
244 | $payment->postRazorpayPayment($invoice); |
||
245 | $date = getDateHtml($invoice->date); |
||
246 | $product = $this->product($invoice->id); |
||
247 | $items = $invoice->invoiceItem()->get(); |
||
248 | $url = ''; |
||
249 | $this->checkoutAction($invoice); //For free product generate invoice without payment |
||
250 | $url = view('themes.default1.front.postCheckoutTemplate', compact('invoice', 'date', 'product', 'items', ))->render(); |
||
251 | \Cart::clear(); |
||
252 | |||
253 | return redirect('checkout')->with('success', $url); |
||
254 | } |
||
255 | } |
||
256 | } catch (\Exception $ex) { |
||
257 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | private function getProcessingFee($paymentMethod, $currency) |
||
269 | } |
||
270 | } |
||
271 | |||
272 | public static function updateFinalPrice(Request $request) |
||
286 | } |
||
287 | |||
288 | public function checkregularPaymentOrRenewal($invoiceid) |
||
300 | } |
||
301 | |||
302 | public function checkoutAction($invoice) |
||
303 | { |
||
304 | try { |
||
305 | //get elements from invoice |
||
306 | $invoice_number = $invoice->number; |
||
307 | $invoice_id = $invoice->id; |
||
308 | |||
309 | foreach (\Cart::getConditionsByType('fee') as $value) { |
||
310 | $invoice->processing_fee = $value->getValue(); |
||
311 | } |
||
312 | // $invoice->processing_fee = |
||
313 | $invoice->status = 'success'; |
||
314 | $invoice->save(); |
||
315 | $user_id = \Auth::user()->id; |
||
316 | |||
317 | $url = ''; |
||
318 | |||
319 | $url = url("download/$user_id/$invoice->number"); |
||
320 | $payment = new \App\Http\Controllers\Order\InvoiceController(); |
||
321 | $payment->postRazorpayPayment($invoice); |
||
322 | //execute the order |
||
323 | $order = new \App\Http\Controllers\Order\OrderController(); |
||
324 | $order->executeOrder($invoice->id, $order_status = 'executed'); |
||
325 | |||
326 | return 'success'; |
||
327 | } catch (\Exception $ex) { |
||
328 | app('log')->error($ex->getMessage()); |
||
329 | Bugsnag::notifyException($ex); |
||
330 | |||
331 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | public function product($invoiceid) |
||
348 | } |
||
349 | } |
||
351 |