RazorpayController::payment()   B
last analyzed

Complexity

Conditions 9
Paths 69

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 44
dl 0
loc 61
rs 7.6604
c 1
b 1
f 0
cc 9
nc 69
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 App\Plugins\Stripe\Controllers\SettingsController;
13
use DateTime;
14
use DateTimeZone;
15
use Illuminate\Http\Request;
16
use Illuminate\Support\Facades\Input;
17
use Razorpay\Api\Api;
18
use Redirect;
19
20
class RazorpayController extends Controller
21
{
22
    public $invoice;
23
    public $invoiceItem;
24
25
    public function __construct()
26
    {
27
        $invoice = new Invoice();
28
        $this->invoice = $invoice;
29
30
        $invoiceItem = new InvoiceItem();
31
        $this->invoiceItem = $invoiceItem;
32
33
        // $mailchimp = new MailChimpController();
34
        // $this->mailchimp = $mailchimp;
35
    }
36
37
    /*
38
    * Create Order And Payment for invoice paid with Razorpay
39
     */
40
    public function payment($invoice, Request $request)
41
    {
42
        $userId = Invoice::find($invoice)->user_id;
43
        if (\Auth::user()->role != 'admin' && $userId != \Auth::user()->id) {
44
            return errorResponse('Payment cannot be initiated. Invalid modification of data');
45
        }
46
        //Input items of form
47
        $input = $request->all();
48
        $error = 'Payment Failed';
49
        $rzp_key = ApiKey::where('id', 1)->value('rzp_key');
50
        $rzp_secret = ApiKey::where('id', 1)->value('rzp_secret');
51
        $invoice = Invoice::where('id', $invoice)->first();
52
        if (count($input) && ! empty($input['razorpay_payment_id'])) { //Verify Razorpay Payment Id and Signature
53
54
            //Fetch payment information by razorpay_payment_id
55
            try {
56
                $api = new Api($rzp_key, $rzp_secret);
57
                $payment = $api->payment->fetch($input['razorpay_payment_id']);
58
                $response = $api->payment->fetch($input['razorpay_payment_id']);
59
60
                $stateCode = \Auth::user()->state;
61
                $state = $this->getState($stateCode);
62
                $currency = $this->getCurrency();
63
64
                //Change order Status as Success if payment is Successful
65
                $control = new \App\Http\Controllers\Order\RenewController();
66
                //After Regular Payment
67
                if ($control->checkRenew() === false) {
68
                    $checkout_controller = new \App\Http\Controllers\Front\CheckoutController();
69
                    $checkout_controller->checkoutAction($invoice);
70
                    $view = $this->getViewMessageAfterPayment($invoice, $state, $currency);
71
                    $status = $view['status'];
72
                    $message = $view['message'];
73
                    \Session::forget('items');
74
                    \Session::forget('code');
75
                    \Session::forget('codevalue');
76
                } else {
77
                    //Afer Renew
78
                    $control->successRenew($invoice);
79
                    $payment = new \App\Http\Controllers\Order\InvoiceController();
80
                    $payment->postRazorpayPayment($invoice);
81
                    if ($invoice->grand_total) {
82
                        SettingsController::sendPaymentSuccessMailtoAdmin($invoice->currency, $invoice->grand_total, \Auth::user(), $invoice->invoiceItem()->first()->product_name);
83
                    }
84
85
                    $view = $this->getViewMessageAfterRenew($invoice, $state, $currency);
86
                    $status = $view['status'];
87
                    $message = $view['message'];
88
                }
89
                \Cart::removeCartCondition('Processing fee');
90
91
                return redirect('checkout')->with($status, $message);
92
            } catch (\Razorpay\Api\Errors\SignatureVerificationError | \Razorpay\Api\Errors\BadRequestError | \Razorpay\Api\Errors\GatewayError | \Razorpay\Api\Errors\ServerError $e) {
93
                dd($e);
94
                SettingsController::sendFailedPaymenttoAdmin($invoice->grand_total, $e->getMessage());
95
96
                return redirect('checkout')->with('fails', 'Your Payment was declined. '.$e->getMessage().'. Please try again or try the other gateway');
97
            } catch (\Exception $e) {
98
                dd($e);
99
100
                return redirect('checkout')->with('fails', 'Your Payment was declined. '.$e->getMessage().'. Please try again or try the other gateway');
101
            }
102
        }
103
    }
104
105
    public function getCurrency()
106
    {
107
        $symbol = \Auth::user()->currency_symbol;
108
109
        return $symbol;
110
    }
111
112
    public function getState($stateCode)
113
    {
114
        if (\Auth::user()->country != 'IN') {
115
            $state = State::where('state_subdivision_code', $stateCode)->pluck('state_subdivision_name')->first();
116
        } else {
117
            $state = TaxByState::where('state_code', \Auth::user()->state)->pluck('state')->first();
118
        }
119
120
        return $state;
121
    }
122
123
    public function getViewMessageAfterPayment($invoice, $state, $currency)
124
    {
125
        $orders = Order::where('invoice_id', $invoice->id)->get();
126
        $invoiceItems = InvoiceItem::where('invoice_id', $invoice->id)->get();
127
128
        \Cart::clear();
129
        $status = 'success';
130
        $message = view('themes.default1.front.postPaymentTemplate', compact('invoice','orders',
131
             'invoiceItems', 'state', 'currency'))->render();
132
133
        return ['status'=>$status, 'message'=>$message];
134
    }
135
136
    public function getViewMessageAfterRenew($invoice, $state, $currency)
137
    {
138
        $invoiceItem = InvoiceItem::where('invoice_id', $invoice->id)->first();
139
        $product = Product::where('name', $invoiceItem->product_name)->first();
140
        $date1 = new DateTime($invoiceItem->created_at);
141
142
        $tz = \Auth::user()->timezone()->first()->name;
143
144
        $date1->setTimezone(new DateTimeZone($tz));
145
        $date = $date1->format('M j, Y, g:i a ');
146
147
        \Cart::clear();
148
        $status = 'success';
149
150
        $message = view('themes.default1.front.postRenewTemplate', compact('invoice','date',
151
            'product', 'invoiceItem', 'state', 'currency'))->render();
152
153
        return ['status'=>$status, 'message'=>$message];
154
    }
155
}
156