Total Complexity | 47 |
Total Lines | 446 |
Duplicated Lines | 0 % |
Changes | 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 |
||
24 | class CheckoutController extends InfoController |
||
25 | { |
||
26 | public $subscription; |
||
27 | public $plan; |
||
28 | public $templateController; |
||
29 | public $product; |
||
30 | public $price; |
||
31 | public $user; |
||
32 | public $setting; |
||
33 | public $template; |
||
34 | public $order; |
||
35 | public $addon; |
||
36 | public $invoice; |
||
37 | public $invoiceItem; |
||
38 | public $mailchimp; |
||
39 | |||
40 | public function __construct() |
||
74 | |||
75 | // $mailchimp = new MailChimpController(); |
||
76 | // $this->mailchimp = $mailchimp; |
||
77 | } |
||
78 | |||
79 | /* |
||
80 | * When Proceed to chekout button clicked first request comes here |
||
81 | */ |
||
82 | public function checkoutForm(Request $request) |
||
83 | { |
||
84 | if (!\Auth::user()) {//If User is not Logged in then send him to login Page |
||
85 | $url = $request->segments(); //The requested url (chekout).Save it in Session |
||
86 | \Session::put('session-url', $url[0]); |
||
87 | $content = Cart::getContent(); |
||
88 | $domain = $request->input('domain'); |
||
89 | if ($domain) { |
||
90 | foreach ($domain as $key => $value) { |
||
91 | \Session::put('domain'.$key, $value); //Store all the domains Entered in Cart Page in Session |
||
92 | } |
||
93 | } |
||
94 | \Session::put('content', $content); |
||
95 | |||
96 | return redirect('auth/login')->with('fails', 'Please login'); |
||
97 | } |
||
98 | if (\Session::has('items')) { |
||
99 | $content = \Session::get('items'); |
||
100 | $attributes = $this->getAttributes($content); |
||
101 | } else { |
||
102 | $content = Cart::getContent(); |
||
103 | $attributes = $this->getAttributes($content); |
||
104 | |||
105 | $content = Cart::getContent(); |
||
106 | } |
||
107 | |||
108 | try { |
||
109 | $domain = $request->input('domain'); |
||
110 | if ($domain) {//Store the Domain in session when user Logged In |
||
111 | foreach ($domain as $key => $value) { |
||
112 | \Session::put('domain'.$key, $value); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return view('themes.default1.front.checkout', compact('content', 'attributes')); |
||
117 | } catch (\Exception $ex) { |
||
118 | app('log')->error($ex->getMessage()); |
||
119 | Bugsnag::notifyException($ex); |
||
120 | |||
121 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get all the Attributes Sent From the cart along with Tax Conditions. |
||
127 | * |
||
128 | * @param array $content Collection of the Cart Values |
||
129 | * |
||
130 | * @return array Items along with their details,Attributes(Currency,Agents) and Tax Conditions |
||
131 | */ |
||
132 | public function getAttributes($content) |
||
133 | { |
||
134 | try { |
||
135 | if (count($content) > 0) {//after ProductPurchase this is not true as cart is cleared |
||
136 | foreach ($content as $key => $item) { |
||
137 | $attributes[] = $item->attributes; |
||
138 | $cart_currency = $attributes[0]['currency']['currency']; //Get the currency of Product in the cart |
||
|
|||
139 | $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. |
||
140 | if ($cart_currency != $currency) { |
||
141 | $id = $item->id; |
||
142 | Cart::remove($id); |
||
143 | } |
||
144 | $require_domain = $this->product->where('id', $item->id)->first()->require_domain; |
||
145 | $require = []; |
||
146 | if ($require_domain == 1) { |
||
147 | $require[$key] = $item->id; |
||
148 | } |
||
149 | $cont = new CartController(); |
||
150 | $taxConditions = $cont->checkTax($item->id); //Calculate Tax Condition by passing ProductId |
||
151 | Cart::remove($item->id); |
||
152 | |||
153 | //Return array of Product Details,attributes and their conditions |
||
154 | $items[] = ['id' => $item->id, 'name' => $item->name, 'price' => $item->price, |
||
155 | 'quantity' => $item->quantity, 'attributes' => ['currency'=> $attributes[0]['currency'], |
||
156 | 'agents' => $attributes[0]['agents'], 'tax'=>$taxConditions['tax_attributes'], ], 'conditions'=>$taxConditions['conditions'], ]; |
||
157 | } |
||
158 | Cart::add($items); |
||
159 | } |
||
160 | } catch (\Exception $ex) { |
||
161 | app('log')->error($ex->getMessage()); |
||
162 | Bugsnag::notifyException($ex); |
||
163 | |||
164 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public function payNow($invoiceid) |
||
169 | { |
||
170 | try { |
||
171 | $invoice = $this->invoice->find($invoiceid); |
||
172 | // dd($invoice); |
||
173 | $items = new \Illuminate\Support\Collection(); |
||
174 | // dd($items); |
||
175 | if ($invoice) { |
||
176 | $items = $invoice->invoiceItem()->get(); |
||
177 | |||
178 | $product = $this->product($invoiceid); |
||
179 | } |
||
180 | |||
181 | return view('themes.default1.front.paynow', compact('invoice', 'items', 'product')); |
||
182 | } catch (\Exception $ex) { |
||
183 | app('log')->error($ex->getMessage()); |
||
184 | Bugsnag::notifyException($ex); |
||
185 | |||
186 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | public function postCheckout(Request $request) |
||
191 | { |
||
192 | $invoice_controller = new \App\Http\Controllers\Order\InvoiceController(); |
||
193 | $info_cont = new \App\Http\Controllers\Front\InfoController(); |
||
194 | $payment_method = $request->input('payment_gateway'); |
||
195 | \Session::put('payment_method', $payment_method); |
||
196 | $paynow = false; |
||
197 | if ($request->input('invoice_id')) { |
||
198 | $paynow = true; |
||
199 | } |
||
200 | $cost = $request->input('cost'); |
||
201 | $state = $this->getState(); |
||
202 | |||
203 | try { |
||
204 | if ($paynow === false) { |
||
205 | /* |
||
206 | * Do order, invoicing etc |
||
207 | */ |
||
208 | |||
209 | $invoice = $invoice_controller->generateInvoice(); |
||
210 | |||
211 | $pay = $this->payment($payment_method, $status = 'pending'); |
||
212 | $payment_method = $pay['payment']; |
||
213 | $status = $pay['status']; |
||
214 | $invoice_no = $invoice->number; |
||
215 | $date = $this->getDate($invoice); |
||
216 | $invoiceid = $invoice->id; |
||
217 | $amount = $invoice->grand_total; |
||
218 | $url = ''; |
||
219 | $cart = Cart::getContent(); |
||
220 | $invoices = $this->invoice->find($invoiceid); |
||
221 | $items = new \Illuminate\Support\Collection(); |
||
222 | if ($invoices) { |
||
223 | $items = $invoice->invoiceItem()->get(); |
||
224 | $product = $this->product($invoiceid); |
||
225 | $content = Cart::getContent(); |
||
226 | $attributes = $this->getAttributes($content); |
||
227 | } |
||
228 | } else { |
||
229 | $items = new \Illuminate\Support\Collection(); |
||
230 | $invoiceid = $request->input('invoice_id'); |
||
231 | $invoice = $this->invoice->find($invoiceid); |
||
232 | $date = $this->getDate($invoice); |
||
233 | $items = $invoice->invoiceItem()->get(); |
||
234 | $product = $this->product($invoiceid); |
||
235 | $amount = $invoice->grand_total; |
||
236 | $content = Cart::getContent(); |
||
237 | $attributes = $this->getAttributes($content); |
||
238 | } |
||
239 | if (Cart::getSubTotal() != 0 || $cost > 0) { |
||
240 | $v = $this->validate($request, [ |
||
241 | 'payment_gateway' => 'required', |
||
242 | ], [ |
||
243 | 'payment_gateway.required' => 'Please choose a payment gateway', |
||
244 | ]); |
||
245 | if ($payment_method == 'razorpay') { |
||
246 | $rzp_key = ApiKey::where('id', 1)->value('rzp_key'); |
||
247 | $rzp_secret = ApiKey::where('id', 1)->value('rzp_secret'); |
||
248 | $apilayer_key = ApiKey::where('id', 1)->value('apilayer_key'); |
||
249 | |||
250 | return view( |
||
251 | 'themes.default1.front.postCheckout', |
||
252 | compact( |
||
253 | 'amount', |
||
254 | 'invoice_no', |
||
255 | ' invoiceid', |
||
256 | ' payment_method', |
||
257 | 'phone', |
||
258 | 'invoice', |
||
259 | 'items', |
||
260 | 'product', |
||
261 | 'paynow', |
||
262 | 'content', |
||
263 | 'attributes', |
||
264 | 'rzp_key', |
||
265 | 'rzp_secret', |
||
266 | 'apilayer_key' |
||
267 | ) |
||
268 | ); |
||
269 | } else { |
||
270 | \Event::fire(new \App\Events\PaymentGateway(['request' => $request, 'cart' => Cart::getContent(), 'order' => $invoice])); |
||
271 | } |
||
272 | } else { |
||
273 | $action = $this->checkoutAction($invoice); |
||
274 | $check_product_category = $this->product($invoiceid); |
||
275 | //Update Subscriber To Mailchimp |
||
276 | $mailchimpStatus = StatusSetting::first()->value('mailchimp_status'); |
||
277 | if ($mailchimpStatus == 1) { |
||
278 | $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
279 | $r = $mailchimp->updateSubscriberForFreeProduct(\Auth::user()->email, $check_product_category->id); |
||
280 | } |
||
281 | |||
282 | $url = ''; |
||
283 | if ($check_product_category->category) { |
||
284 | $url = view('themes.default1.front.postCheckoutTemplate', compact( |
||
285 | 'invoice', |
||
286 | 'date', |
||
287 | 'product', |
||
288 | 'items', |
||
289 | 'attributes', |
||
290 | 'state' |
||
291 | ))->render(); |
||
292 | } |
||
293 | \Cart::clear(); |
||
294 | |||
295 | return redirect()->back()->with('success', $url); |
||
296 | } |
||
297 | } catch (\Exception $ex) { |
||
298 | app('log')->error($ex->getMessage()); |
||
299 | Bugsnag::notifyException($ex); |
||
300 | |||
301 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | public function checkoutAction($invoice) |
||
306 | { |
||
307 | try { |
||
308 | //get elements from invoice |
||
309 | $invoice_number = $invoice->number; |
||
310 | $invoice_id = $invoice->id; |
||
311 | $invoice->status = 'success'; |
||
312 | $invoice->save(); |
||
313 | $user_id = \Auth::user()->id; |
||
314 | |||
315 | $url = ''; |
||
316 | |||
317 | $url = url("download/$user_id/$invoice->number"); |
||
318 | //execute the order |
||
319 | $order = new \App\Http\Controllers\Order\OrderController(); |
||
320 | $order->executeOrder($invoice->id, $order_status = 'executed'); |
||
321 | $payment = new \App\Http\Controllers\Order\InvoiceController(); |
||
322 | $payment->postRazorpayPayment($invoice_id, $invoice->grand_total); |
||
323 | |||
324 | return 'success'; |
||
325 | } catch (\Exception $ex) { |
||
326 | app('log')->error($ex->getMessage()); |
||
327 | Bugsnag::notifyException($ex); |
||
328 | |||
329 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | public function product($invoiceid) |
||
334 | { |
||
335 | try { |
||
336 | $invoice = $this->invoiceItem->where('invoice_id', $invoiceid)->first(); |
||
337 | $name = $invoice->product_name; |
||
338 | $product = $this->product->where('name', $name)->first(); |
||
339 | |||
340 | return $product; |
||
341 | } catch (\Exception $ex) { |
||
342 | app('log')->error($ex->getMessage()); |
||
343 | Bugsnag::notifyException($ex); |
||
344 | |||
345 | throw new \Exception($ex->getMessage()); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | public function GenerateOrder() |
||
350 | { |
||
351 | try { |
||
352 | $products = []; |
||
353 | $items = \Cart::getContent(); |
||
354 | foreach ($items as $item) { |
||
355 | //this is product |
||
356 | $id = $item->id; |
||
357 | $this->AddProductToOrder($id); |
||
358 | } |
||
359 | } catch (\Exception $ex) { |
||
360 | app('log')->error($ex->getMessage()); |
||
361 | Bugsnag::notifyException($ex); |
||
362 | |||
363 | throw new \Exception('Can not Generate Order'); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | public function AddProductToOrder($id) |
||
368 | { |
||
369 | try { |
||
370 | $cart = \Cart::get($id); |
||
371 | $client = \Auth::user()->id; |
||
372 | $payment_method = \Input::get('payment_gateway'); |
||
373 | $promotion_code = ''; |
||
374 | $order_status = 'pending'; |
||
375 | $serial_key = ''; |
||
376 | $product = $id; |
||
377 | $addon = ''; |
||
378 | $domain = ''; |
||
379 | $price_override = $cart->getPriceSumWithConditions(); |
||
380 | $qty = $cart->quantity; |
||
381 | |||
382 | $planid = $this->price->where('product_id', $id)->first()->subscription; |
||
383 | |||
384 | $or = $this->order->create(['client' => $client, |
||
385 | 'payment_method' => $payment_method, 'promotion_code' => $promotion_code, |
||
386 | 'order_status' => $order_status, 'serial_key' => $serial_key, |
||
387 | 'product' => $product, 'addon' => $addon, 'domain' => $domain, |
||
388 | 'price_override' => $price_override, 'qty' => $qty, ]); |
||
389 | |||
390 | $this->AddSubscription($or->id, $planid); |
||
391 | } catch (\Exception $ex) { |
||
392 | app('log')->error($ex->getMessage()); |
||
393 | Bugsnag::notifyException($ex); |
||
394 | |||
395 | throw new \Exception('Can not Generate Order for Product'); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | public function AddSubscription($orderid, $planid) |
||
419 | } |
||
420 | } |
||
421 | |||
422 | public function GenerateInvoice() |
||
423 | { |
||
424 | try { |
||
425 | $user_id = \Auth::user()->id; |
||
426 | $number = rand(11111111, 99999999); |
||
427 | $date = \Carbon\Carbon::now(); |
||
428 | $grand_total = \Cart::getSubTotal(); |
||
429 | |||
430 | $invoice = $this->invoice->create(['user_id' => $user_id, |
||
431 | 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, ]); |
||
432 | foreach (\Cart::getContent() as $cart) { |
||
433 | $this->CreateInvoiceItems($invoice->id, $cart); |
||
434 | } |
||
435 | } catch (\Exception $ex) { |
||
436 | app('log')->error($ex->getMessage()); |
||
437 | Bugsnag::notifyException($ex); |
||
438 | |||
439 | throw new \Exception('Can not Generate Invoice'); |
||
440 | } |
||
441 | } |
||
442 | |||
443 | public function CreateInvoiceItems($invoiceid, $cart) |
||
470 | } |
||
471 | } |
||
472 | } |
||
473 |