Completed
Push — development ( c63668...24f425 )
by Ashutosh
09:30
created

RazorpayController::getCurrency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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