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