Completed
Push — development ( c6edbc...615590 )
by Ashutosh
09:59
created

RazorpayController::payment()   B

Complexity

Conditions 8
Paths 180

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 80
rs 7.2258
c 0
b 0
f 0
cc 8
nc 180
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\ApiKey;
6
use App\Model\Common\State;
7
use App\Model\Order\Invoice;
8
use App\Model\Order\InvoiceItem;
9
use App\Model\Order\Order;
10
use App\Model\Payment\TaxByState;
11
use App\Model\Product\Product;
12
use DateTime;
13
use DateTimeZone;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Input;
16
use Razorpay\Api\Api;
17
use Redirect;
18
19
class RazorpayController extends Controller
20
{
21
    public $invoice;
22
    public $invoiceItem;
23
24
    public function __construct()
25
    {
26
        $invoice = new Invoice();
27
        $this->invoice = $invoice;
28
29
        $invoiceItem = new InvoiceItem();
30
        $this->invoiceItem = $invoiceItem;
31
32
        // $mailchimp = new MailChimpController();
33
        // $this->mailchimp = $mailchimp;
34
    }
35
36
    public function payWithRazorpay()
37
    {
38
        $api = new Api(config('custom.razor_key'), config('custom.razor_secret'));
39
40
        return view('themes.default1.front.checkout', compact('api'));
41
    }
42
43
    public function payment($invoice, Request $request)
44
    {
45
46
        //Input items of form
47
        $input = Input::all();
48
49
        $success = true;
50
        $error = 'Payment Failed';
51
        $rzp_key = ApiKey::where('id', 1)->value('rzp_key');
52
        $rzp_secret = ApiKey::where('id', 1)->value('rzp_secret');
53
54
        $api = new Api($rzp_key, $rzp_secret);
55
        $payment = $api->payment->fetch($input['razorpay_payment_id']);
56
57
        if (count($input) && !empty($input['razorpay_payment_id'])) { //Verify Razorpay Payment Id and Signature
58
59
            //Fetch payment information by razorpay_payment_id
60
            try {
61
                $response = $api->payment->fetch($input['razorpay_payment_id']);
62
                // $api->utility->verifyPaymentSignature($attributes);
63
            } catch (SignatureVerificationError $e) {
64
                $success = false;
65
                $error = 'Razorpay Error : '.$e->getMessage();
66
            }
67
        }
68
        $country = \Auth::user()->country;
69
70
        if ($country != 'IN') {
71
            $state = State::where('state_subdivision_code', $stateCode)->pluck('state_subdivision_name')->first();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stateCode does not exist. Did you maybe mean $state?
Loading history...
72
        } else {
73
            $state = TaxByState::where('state_code', \Auth::user()->state)->pluck('state')->first();
74
        }
75
        $invoice = Invoice::where('id', $invoice)->first();
76
77
        if ($success === true) {
78
            try {
79
                //Change order Status as Success if payment is Successful
80
                $control = new \App\Http\Controllers\Order\RenewController();
81
82
                if ($control->checkRenew() === false) {
83
                    $checkout_controller = new \App\Http\Controllers\Front\CheckoutController();
84
85
                    $checkout_controller->checkoutAction($invoice);
86
87
                    $order = Order::where('invoice_id', $invoice->id)->first();
88
                    $invoiceItem = InvoiceItem::where('invoice_id', $invoice->id)->first();
89
                    $date1 = new DateTime($order->created_at);
90
                    $tz = \Auth::user()->timezone()->first()->name;
91
                    $date1->setTimezone(new DateTimeZone($tz));
92
                    $date = $date1->format('M j, Y, g:i a ');
93
                    $product = Product::where('id', $order->product)->select('id', 'name')->first();
94
95
                    \Cart::clear();
96
                    $status = 'success';
97
                    $message = view('themes.default1.front.postPaymentTemplate', compact('invoice','date','order',
98
                        'product','invoiceItem'))->render();
99
                    \Session::forget('items');
100
                } else {
101
                    $control->successRenew($invoice);
102
                    $payment = new \App\Http\Controllers\Order\InvoiceController();
103
                    $payment->postRazorpayPayment($invoice->id, $invoice->grand_total);
104
105
                    $invoiceItem = InvoiceItem::where('invoice_id', $invoice->id)->first();
106
                    $product = Product::where('name', $invoiceItem->product_name)->first();
107
                    $date1 = new DateTime($invoiceItem->created_at);
108
109
                    $tz = \Auth::user()->timezone()->first()->name;
110
111
                    $date1->setTimezone(new DateTimeZone($tz));
112
                    $date = $date1->format('M j, Y, g:i a ');
113
114
                    \Cart::clear();
115
                    $status = 'success';
116
117
                    $message =view('themes.default1.front.postRenewTemplate', compact('invoice','date','order',
118
                        'product','invoiceItem'))->render(); 
119
                }
120
                return redirect()->back()->with($status, $message);
121
            } catch (\Exception $ex) {
122
                throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
123
            }
124
        }
125
    }
126
}
127