Completed
Push — development ( 50bb76...10afda )
by Ashutosh
08:23
created

RazorpayController::getState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
                } catch (SignatureVerificationError $e) {
63
                $success = false;
64
                $error = 'Razorpay Error : '.$e->getMessage();
65
            }
66
        }
67
        $state = $this->getState(\Auth::user()->state);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $state is correct as $this->getState(Auth::user()->state) targeting App\Http\Controllers\Raz...yController::getState() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
        $invoice = Invoice::where('id', $invoice)->first();
69
70
        if ($success === true) {
71
            try {
72
                //Change order Status as Success if payment is Successful
73
                $control = new \App\Http\Controllers\Order\RenewController();
74
75
                if ($control->checkRenew() === false) {
76
                    $checkout_controller = new \App\Http\Controllers\Front\CheckoutController();
77
                    $checkout_controller->checkoutAction($invoice);
78
79
                    $view = $this->getViewMessageAfterPayment($invoice);
80
                    $status = $view['status'];
81
                    $message = $view['message'];
82
83
                    \Session::forget('items');
84
                } else {
85
                    $control->successRenew($invoice);
86
                    $payment = new \App\Http\Controllers\Order\InvoiceController();
87
                    $payment->postRazorpayPayment($invoice->id, $invoice->grand_total);
88
89
                    $view = $this->getViewMessageAfterRenew($invoice);
90
                    $status = $view['status'];
91
                    $message = $view['message'];
92
                   
93
94
                }
95
96
                return redirect()->back()->with($status, $message);
97
            } catch (\Exception $ex) {
98
                dd($ex);
99
                throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
100
            }
101
        }
102
    }
103
104
    public function getState($stateCode)
105
    {
106
       if (\Auth::user()->country != 'IN') {
107
            $state = State::where('state_subdivision_code', $stateCode)->pluck('state_subdivision_name')->first();
108
        } else {
109
            $state = TaxByState::where('state_code', \Auth::user()->state)->pluck('state')->first();
110
        } 
111
    }
112
113
    public function getViewMessageAfterPayment($invoice)
114
    {
115
        $order = Order::where('invoice_id', $invoice->id)->first();
116
        $invoiceItem = InvoiceItem::where('invoice_id', $invoice->id)->first();
117
        $date1 = new DateTime($order->created_at);
118
        $tz = \Auth::user()->timezone()->first()->name;
119
        $date1->setTimezone(new DateTimeZone($tz));
120
        $date = $date1->format('M j, Y, g:i a ');
121
        $product = Product::where('id', $order->product)->select('id', 'name')->first();
122
123
        \Cart::clear();
124
        $status = 'success';
125
        $message = view('themes.default1.front.postPaymentTemplate', compact('invoice','date','order',
126
            'product','invoiceItem'))->render();
127
        return ['status'=>$status, 'message'=>$message];
128
    }
129
130
    public function getViewMessageAfterRenew($invoice)
131
    {
132
        $invoiceItem = InvoiceItem::where('invoice_id', $invoice->id)->first();
133
        $product = Product::where('name', $invoiceItem->product_name)->first();
134
        $date1 = new DateTime($invoiceItem->created_at);
135
136
        $tz = \Auth::user()->timezone()->first()->name;
137
138
        $date1->setTimezone(new DateTimeZone($tz));
139
        $date = $date1->format('M j, Y, g:i a ');
140
141
        \Cart::clear();
142
        $status = 'success';
143
144
        $message =view('themes.default1.front.postRenewTemplate', compact('invoice','date','order',
145
            'product','invoiceItem'))->render(); 
146
        return ['status'=>$status, 'message'=>$message];
147
    }
148
}
149