Completed
Push — development ( c72a58...bbb3cb )
by Ashutosh
10:50 queued 12s
created

ProcessController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Plugins\Paypal\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Plugins\Paypal\Model\Paypal;
7
use Illuminate\Http\Request;
8
9
class ProcessController extends Controller
10
{
11
    protected $paypal;
12
13
    public function __construct()
14
    {
15
        $paypal = new Paypal();
16
        $this->paypal = $paypal;
17
    }
18
19
    public function PassToPayment($requests)
0 ignored issues
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...
20
    {
21
        try {
22
            $request = $requests['request'];
23
            $order = $requests['order'];
24
            $cart = $requests['cart'];
25
            //dd($request);
26
            if ($cart->count() > 0) {
27
                $total = \Cart::getSubTotal();
28
            } else {
29
                $total = $request->input('cost');
30
                \Cart::clear();
31
                \Session::put('invoiceid', $order->id);
32
            }
33
34
            if ($request->input('payment_gateway') == 'paypal') {
35
                if (!\Schema::hasTable('paypal')) {
36
                    throw new \Exception('Paypal is not configured');
37
                }
38
                $paypal = $this->paypal->where('id', 1)->first();
39
                if (!$paypal) {
40
                    throw new \Exception('Paypal Fields not given');
41
                }
42
                $data = $this->getFields($order);
43
                $this->middlePage($data);
44
            }
45
        } catch (\Exception $ex) {
46
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
47
        }
48
    }
49
50
    public function getFields($invoice)
51
    {
52
        try {
53
            //dd($invoice);
54
            $item = [];
55
            $data = [];
56
            $user = \Auth::user();
57
            if (!$user) {
58
                throw new \Exception('No autherized user');
59
            }
60
            $config = $this->paypal->where('id', 1)->first();
61
            if ($config) {
62
                $business = $config->business;
63
                $cmd = $config->cmd;
64
                $return = $config->success_url;
65
                $cancel_return = $config->cancel_url;
66
                $notify_url = $config->notify_url;
67
                $image_url = $config->image_url;
68
                $rm = 1;
69
                $currency_code = $invoice->currency;
70
                $invoice_id = $invoice->id;
71
                $first_name = $user->first_name;
72
                $last_name = $user->last_name;
73
                $address1 = $user->address;
74
                $city = $user->town;
75
                $zip = $user->zip;
76
                $email = $user->email;
77
                $product_name = '';
78
                if ($invoice->invoiceItem()->first()) {
79
                    $product_name = str_replace(' ', '-', $invoice->invoiceItem()->first()->product_name);
80
                }
81
82
                $data = [
83
                    'business'      => $business,
84
                    'cmd'           => $cmd,
85
                    'return'        => $return,
86
                    'cancel_return' => $cancel_return,
87
                    'notify_url'    => $notify_url,
88
                    'image_url'     => $image_url,
89
                    'rm'            => $rm,
90
                    'currency_code' => 'USD', //$currency_code,
91
                    'invoice'       => $invoice_id,
92
                    'first_name'    => $first_name,
93
                    'last_name'     => $last_name,
94
                    'address1'      => $address1,
95
                    'city'          => $city,
96
                    'zip'           => $zip,
97
                    'email'         => $email,
98
                    'item_name'     => $product_name,
99
                ];
100
101
                $items = $invoice->invoiceItem()->get()->toArray();
102
                //dd($items);
103
                $c = count($items);
104
                if (count($items) > 0) {
105
                    for ($i = 0; $i < $c; $i++) {
106
                        $n = $i + 1;
107
                        $item = [
108
                            "item_name_$n" => $items[$i]['product_name'],
109
                            "quantity_$n"  => $items[$i]['quantity'],
110
                        ];
111
                    }
112
                    $data = array_merge($data, $item);
113
                    $total = ['amount' => $invoice->grand_total];
114
                    $data = array_merge($data, $total);
115
                }
116
            }
117
118
            return $data;
119
        } catch (\Exception $ex) {
120
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
121
        }
122
    }
123
124
    public function postCurl($data)
125
    {
126
        try {
127
            $config = $this->paypal->where('id', 1)->first();
128
            if (!$config) {
129
                throw new \Exception('Paypal Fields not given');
130
            }
131
            $url = $config->paypal_url;
132
            $post_data = http_build_query($data);
133
            echo $url;
134
            $ch = curl_init();
135
            curl_setopt($ch, CURLOPT_URL, $url);
136
            curl_setopt($ch, CURLOPT_POST, 1);
137
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
138
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
139
            $output = curl_exec($ch);
140
            curl_close($ch);
141
        } catch (\Exception $ex) {
142
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
143
        }
144
    }
145
146
    public function postForm($data)
147
    {
148
        try {
149
            $config = $this->paypal->where('id', 1)->first();
150
            if (!$config) {
151
                throw new \Exception('Paypal Fields not given');
152
            }
153
            \Session::put('invoiceid', $data['invoice']);
154
            $url = $config->paypal_url;
155
            echo "<form action=$url id=form name=redirect method=post>";
156
            foreach ($data as $key => $value) {
157
                echo "<input type=hidden name=$key value=$value>";
158
            }
159
            echo '</form>';
160
            echo"<script language='javascript'>document.redirect.submit();</script>";
161
        } catch (\Exception $ex) {
162
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
163
        }
164
    }
165
166
    public function middlePage($data)
167
    {
168
        try {
169
            $path = app_path().'/Plugins/Paypal/views';
170
            \View::addNamespace('plugins', $path);
171
            echo view('plugins::middle-page', compact('data'));
172
        } catch (\Exception $ex) {
173
            dd($ex);
174
        }
175
    }
176
177
    public function response(Request $request)
178
    {
179
        $id = '';
180
        $url = 'checkout';
181
        if (\Session::has('invoiceid')) {
182
            $invoiceid = \Session::get('invoiceid');
183
            $url = 'paynow/'.$id;
184
        }
185
        // if (\Cart::getContent()->count() > 0) {
186
        //     \Cart::clear();
187
        // }
188
        if ($invoiceid) {
189
            $control = new \App\Http\Controllers\Order\RenewController();
190
            if ($control->checkRenew() === false) {
191
                $invoice = new \App\Model\Order\Invoice();
192
                $invoice = $invoice->findOrFail($invoiceid);
193
                $checkout_controller = new \App\Http\Controllers\Front\CheckoutController();
194
                $state = \Auth::user()->state;
195
                $currency = \Auth::user()->currency_symbol;
196
                $checkout_controller->checkoutAction($invoice);
197
                $cont = new \App\Http\Controllers\RazorpayController();
198
                $view = $cont->getViewMessageAfterPayment($invoice, $state, $currency);
199
                $status = $view['status'];
200
                $message = $view['message'];
201
                  \Session::forget('items');
202
                    \Session::forget('code');
203
                    \Session::forget('codevalue');
204
            } else {
205
                $invoice = new \App\Model\Order\Invoice();
206
                $invoice = $invoice->findOrFail($invoiceid);
207
                $control->/* @scrutinizer ignore-call */
208
                successRenew($invoice);
209
                $payment = new \App\Http\Controllers\Order\InvoiceController();
210
                $payment->postRazorpayPayment($invoice->id, $invoice->grand_total);
211
                $state = \Auth::user()->state;
212
                $currency = \Auth::user()->currency_symbol;
213
                $cont = new \App\Http\Controllers\RazorpayController();
214
                $view = $cont->getViewMessageAfterRenew($invoice, $state, $currency);
215
                $status = $view['status'];
216
                $message = $view['message'];
217
            }
218
            return redirect()->back()->with($status, $message);
219
            \Cart::clear();
0 ignored issues
show
Unused Code introduced by
Cart::clear() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
220
        }
221
        
222
    }
223
224
    public function cancel(Request $request)
225
    {
226
        $url = 'checkout';
227
        if (\Session::has('invoiceid')) {
228
            $id = \Session::get('invoiceid');
229
            $url = 'paynow/'.$id;
230
        }
231
        \Session::forget('invoiceid');
232
233
        return redirect($url)->with('fails', 'Thank you for your order. However,the transaction has been declined. Try again.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 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...
234
    }
235
236
    public function notify(Request $request)
237
    {
238
        dd($request);
239
    }
240
241
  
242
}
243