Test Setup Failed
Push — development ( ac5058...9fa3be )
by Ashutosh
11:12
created

CheckoutController::getAttributes()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 27
rs 9.0111
c 0
b 0
f 0
cc 6
nc 9
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Common\MailChimpController;
7
use App\Http\Controllers\Common\TemplateController;
8
use App\Model\Common\Setting;
9
use App\Model\Common\State;
10
use App\Model\Common\Template;
11
use App\Model\Order\Invoice;
12
use App\Model\Order\InvoiceItem;
13
use App\Model\Order\Order;
14
use App\Model\Payment\Plan;
15
use App\Model\Product\Price;
16
use App\Model\Product\Product;
17
use App\Model\Product\Subscription;
18
use App\User;
19
use Bugsnag;
20
use Cart;
21
use Illuminate\Http\Request;
22
use Log;
23
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()
41
    {
42
        $subscription = new Subscription();
43
        $this->subscription = $subscription;
44
45
        $plan = new Plan();
46
        $this->plan = $plan;
47
48
        $templateController = new TemplateController();
49
        $this->templateController = $templateController;
50
51
        $product = new Product();
52
        $this->product = $product;
53
54
        $price = new Price();
55
        $this->price = $price;
56
57
        $user = new User();
58
        $this->user = $user;
59
60
        $setting = new Setting();
61
        $this->setting = $setting;
62
63
        $template = new Template();
64
        $this->template = $template;
65
66
        $order = new Order();
67
        $this->order = $order;
68
69
        $invoice = new Invoice();
70
        $this->invoice = $invoice;
71
72
        $invoiceItem = new InvoiceItem();
73
        $this->invoiceItem = $invoiceItem;
74
75
        // $mailchimp = new MailChimpController();
76
        // $this->mailchimp = $mailchimp;
77
    }
78
79
    public function checkoutForm(Request $request)
80
    {
81
        $currency = 'INR';
82
        $cart_currency = 'INR';
83
        if (!\Auth::user()) {
84
            $url = $request->segments();
85
            $content = Cart::getContent();
86
87
            \Session::put('session-url', $url[0]);
88
            $domain = $request->input('domain');
89
            if (count($domain) > 0) {
90
                foreach ($domain as $key => $value) {
91
                    \Session::put('domain'.$key, $value);
92
                }
93
            }
94
            \Session::put('content', $content);
95
96
            return redirect('auth/login')->with('fails', 'Please login');
97
        }
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
107
        $require = [];
108
109
        //        if ($content->count() == 0) {
110
        //            return redirect('home');
111
        //        }
112
        if ($cart_currency != $currency) {
113
            return redirect('checkout');
114
        }
115
        if (count($require) > 0) {
116
            $this->validate($request, [
117
                'domain.*' => 'required',
118
                    ], [
119
                'domain.*.required' => 'Please provide Domain name',
120
                //'domain.*.url'      => 'Domain name is not valid',
121
                       ]);
122
        }
123
124
        try {
125
            $domain = $request->input('domain');
126
            if (count($domain) > 0) {
127
                foreach ($domain as $key => $value) {
128
                    \Session::put('domain'.$key, $value);
129
                }
130
            }
131
            //$content = Cart::getContent();
132
            return view('themes.default1.front.checkout', compact('content', 'attributes'));
133
        } catch (\Exception $ex) {
134
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
135
            app('log')->info($ex->getMessage());
136
            Bugsnag::notifyException($ex);
137
138
            return redirect()->back()->with('fails', $ex->getMessage());
139
        }
140
    }
141
142
    public function getAttributes($content)
143
    {
144
        $attributes = [];
145
        foreach ($content as $key => $item) {
146
            $attributes[] = $item->attributes;
147
            $cart_currency = $attributes[0]['currency'][0]['code'];
148
            $user_currency = \Auth::user()->currency;
149
            $currency = 'INR';
150
            if ($user_currency == 1 || $user_currency == 'USD') {
151
                $currency = 'USD';
152
            }
153
            if ($cart_currency != $currency) {
154
                $id = $item->id;
155
                Cart::remove($id);
156
                $controller = new CartController();
157
                $items = $controller->addProduct($id);
158
                Cart::add($items);
159
                //
160
            }
161
162
            $require_domain = $this->product->where('id', $item->id)->first()->require_domain;
163
            $require = [];
164
            if ($require_domain == 1) {
165
                $require[$key] = $item->id;
166
            }
167
168
            return $attributes;
169
            //$attributes[] = $item->attributes;
170
        }
171
    }
172
173
    public function payNow($invoiceid)
174
    {
175
        try {
176
            $invoice = $this->invoice->find($invoiceid);
177
            // dd($invoice);
178
            $items = new \Illuminate\Support\Collection();
179
            // dd($items);
180
            if ($invoice) {
181
                $items = $invoice->invoiceItem()->get();
182
183
                $product = $this->product($invoiceid);
184
            }
185
186
            return view('themes.default1.front.paynow', compact('invoice', 'items', 'product'));
187
        } catch (\Exception $ex) {
188
            Bugsnag::notifyException($ex);
189
190
            return redirect()->back()->with('fails', $ex->getMessage());
191
        }
192
    }
193
194
    public function postCheckout(Request $request)
195
    {
196
        $invoice_controller = new \App\Http\Controllers\Order\InvoiceController();
197
        $info_cont = new \App\Http\Controllers\Front\InfoController();
198
        $payment_method = $request->input('payment_gateway');
199
        $paynow = false;
200
        if ($request->input('invoice_id')) {
201
            $paynow = true;
202
        }
203
        $cost = $request->input('cost');
204
        $state = $this->getState();
205
206
        try {
207
            if ($paynow === false) {
208
                /*
209
                 * Do order, invoicing etc
210
                 */
211
                $invoice = $invoice_controller->generateInvoice();
212
213
                $pay = $this->payment($payment_method, $status = 'pending');
214
                $payment_method = $pay['payment'];
215
                $status = $pay['status'];
216
                $invoice_no = $invoice->number;
217
                $date = $this->getDate($invoice);
218
                $invoiceid = $invoice->id;
219
                $amount = $invoice->grand_total;
220
                $url = '';
221
                $cart = Cart::getContent();
222
                $invoices = $this->invoice->find($invoiceid);
223
                $items = new \Illuminate\Support\Collection();
224
                if ($invoices) {
225
                    $items = $invoice->invoiceItem()->get();
226
                    $product = $this->product($invoiceid);
227
                    $content = Cart::getContent();
228
                    $attributes = $this->getAttributes($content);
229
                }
230
            } else {
231
                $items = new \Illuminate\Support\Collection();
232
                $invoice_id = $request->input('invoice_id');
233
                $invoice = $this->invoice->find($invoice_id);
234
                $items = $invoice->invoiceItem()->get();
235
                $product = $this->product($invoice_id);
236
                $amount = $invoice->grand_total;
237
                $content = Cart::getContent();
238
                $attributes = $this->getAttributes($content);
239
            }
240
            if (Cart::getSubTotal() != 0 || $cost > 0) {
241
                $rzp_key = ApiKey::where('id', 1)->value('rzp_key');
242
                $rzp_secret = ApiKey::where('id', 1)->value('rzp_secret');
243
                $apilayer_key = ApiKey::where('id', 1)->value('apilayer_key');
244
245
                return view('themes.default1.front.postCheckout', compact('amount', 'invoice_no', ' invoiceid', ' payment_method','phone', 'invoice', 'items', 'product', 'paynow', 'attributes','rzp_key','rzp_secret',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 216 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
246
                    'apilayer_key'));
247
            } else {
248
                $action = $this->checkoutAction($invoice);
249
                $check_product_category = $this->product($invoiceid);
250
                $url = '';
251
                if ($check_product_category->category) {
252
                    $url = view('themes.default1.front.postCheckoutTemplate', compact('invoice','date',
253
                        'product', 'items', 'attributes', 'state'))->render();
254
                }
255
                \Cart::clear();
256
257
                return redirect()->back()->with('success', $url);
258
            }
259
        } catch (\Exception $ex) {
260
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
261
            app('log')->info($ex->getMessage());
262
            Bugsnag::notifyException($ex);
263
264
            return redirect()->back()->with('fails', $ex->getMessage());
265
        }
266
    }
267
268
    public function checkoutAction($invoice)
269
    {
270
        try {
271
            //get elements from invoice
272
            $invoice_number = $invoice->number;
273
            $invoice_id = $invoice->id;
274
            $invoice->status = 'success';
275
            $invoice->save();
276
            //dd($invoice->id);
277
278
            $invoice_items = $this->invoiceItem->where('invoice_id', $invoice->id)->first();
279
            $product = $invoice_items->product_name;
280
281
            $user_id = \Auth::user()->id;
282
283
            $url = '';
284
            $check_product_category = $this->product($invoice_id);
285
            if ($check_product_category->category) {
286
                $url = url("download/$user_id/$invoice_number");
287
                //execute the order
288
                $order = new \App\Http\Controllers\Order\OrderController();
289
                $order->executeOrder($invoice->id, $order_status = 'executed');
290
                $payment = new \App\Http\Controllers\Order\InvoiceController();
291
                $payment->postRazorpayPayment($invoice_id, $invoice->grand_total);
292
            }
293
294
            return 'success';
295
        } catch (\Exception $ex) {
296
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
297
            app('log')->info($ex->getMessage());
298
            Bugsnag::notifyException($ex);
299
300
            return redirect()->back()->with('fails', $ex->getMessage());
301
        }
302
    }
303
304
    public function product($invoiceid)
305
    {
306
        try {
307
            $invoice = $this->invoiceItem->where('invoice_id', $invoiceid)->first();
308
            // dd($invoice);
309
            $name = $invoice->product_name;
310
            $product = $this->product->where('name', $name)->first();
311
312
            return $product;
313
        } catch (\Exception $ex) {
314
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
315
            app('log')->info($ex->getMessage());
316
            Bugsnag::notifyException($ex);
317
318
            throw new \Exception($ex->getMessage());
319
        }
320
    }
321
322
    public function GenerateOrder()
1 ignored issue
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
323
    {
324
        try {
325
            $products = [];
326
            $items = \Cart::getContent();
327
            foreach ($items as $item) {
328
329
               //this is product
330
                $id = $item->id;
331
                $this->AddProductToOrder($id);
332
            }
333
        } catch (\Exception $ex) {
334
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
335
            app('log')->info($ex->getMessage());
336
            Bugsnag::notifyException($ex);
337
338
            throw new \Exception('Can not Generate Order');
339
        }
340
    }
341
342
    public function AddProductToOrder($id)
1 ignored issue
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
343
    {
344
        try {
345
            $cart = \Cart::get($id);
346
            $client = \Auth::user()->id;
347
            $payment_method = \Input::get('payment_gateway');
348
            $promotion_code = '';
349
            $order_status = 'pending';
350
            $serial_key = '';
351
            $product = $id;
352
            $addon = '';
353
            $domain = '';
354
            $price_override = $cart->getPriceSumWithConditions();
355
            $qty = $cart->quantity;
356
357
            $planid = $this->price->where('product_id', $id)->first()->subscription;
358
359
            $or = $this->order->create(['client' => $client, 'payment_method' => $payment_method, 'promotion_code' => $promotion_code, 'order_status' => $order_status, 'serial_key' => $serial_key, 'product' => $product, 'addon' => $addon, 'domain' => $domain, 'price_override' => $price_override, 'qty' => $qty]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 313 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
360
361
            $this->AddSubscription($or->id, $planid);
362
        } catch (\Exception $ex) {
363
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
364
            app('log')->info($ex->getMessage());
365
            Bugsnag::notifyException($ex);
366
367
            throw new \Exception('Can not Generate Order for Product');
368
        }
369
    }
370
371
    public function AddSubscription($orderid, $planid)
1 ignored issue
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
372
    {
373
        try {
374
            $days = $this->plan->where('id', $planid)->first()->days;
375
            //dd($days);
376
            if ($days > 0) {
377
                $dt = \Carbon\Carbon::now();
378
                //dd($dt);
379
                $user_id = \Auth::user()->id;
380
                $ends_at = $dt->addDays($days);
381
            } else {
382
                $ends_at = '';
383
            }
384
            $this->subscription->create(['user_id' => \Auth::user()->id, 'plan_id' => $planid, 'order_id' => $orderid, 'ends_at' => $ends_at]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
385
        } catch (\Exception $ex) {
386
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
387
            app('log')->info($ex->getMessage());
388
            Bugsnag::notifyException($ex);
389
390
            throw new \Exception('Can not Generate Subscription');
391
        }
392
    }
393
394
    public function GenerateInvoice()
1 ignored issue
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
395
    {
396
        try {
397
            $user_id = \Auth::user()->id;
398
            $number = rand(11111111, 99999999);
399
            $date = \Carbon\Carbon::now();
400
            $grand_total = \Cart::getSubTotal();
401
402
            $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
403
            foreach (\Cart::getContent() as $cart) {
404
                $this->CreateInvoiceItems($invoice->id, $cart);
405
            }
406
        } catch (\Exception $ex) {
407
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
408
            app('log')->info($ex->getMessage());
409
            Bugsnag::notifyException($ex);
410
411
            throw new \Exception('Can not Generate Invoice');
412
        }
413
    }
414
415
    public function CreateInvoiceItems($invoiceid, $cart)
1 ignored issue
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
416
    {
417
        try {
418
            $product_name = $cart->name;
419
            $regular_price = $cart->price;
420
            $quantity = $cart->quantity;
421
            $subtotal = $cart->getPriceSumWithConditions();
422
423
            $tax_name = '';
424
            $tax_percentage = '';
425
426
            foreach ($cart->attributes['tax'] as $tax) {
427
                //dd($tax['name']);
428
                $tax_name .= $tax['name'].',';
429
                $tax_percentage .= $tax['rate'].',';
430
            }
431
432
            //dd($tax_name);
433
434
            $invoiceItem = $this->invoiceItem->create(['invoice_id' => $invoiceid, 'product_name' => $product_name, 'regular_price' => $regular_price, 'quantity' => $quantity, 'tax_name' => $tax_name, 'tax_percentage' => $tax_percentage, 'subtotal' => $subtotal]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 264 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
435
        } catch (\Exception $ex) {
436
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
437
            app('log')->info($ex->getMessage());
438
            Bugsnag::notifyException($ex);
439
440
            throw new \Exception('Can not create Invoice Items');
441
        }
442
    }
443
}
444