Completed
Pull Request — master (#125)
by vijay
91:29 queued 37:43
created

ProcessController::postCurl()   B

Complexity

Conditions 3
Paths 13

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 20
c 2
b 1
f 0
nc 13
nop 1
dl 0
loc 24
rs 8.9713
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)
20
    {
21
        try {
22
            //dd($requests);
23
            $request = $requests['request'];
24
            $order = $requests['order'];
25
            $data = [];
26
            //dd($order);
27
28
            if ($request->input('payment_gateway') == 'paypal') {
29
                if (!\Schema::hasTable('paypal')) {
30
                    throw new \Exception('Paypal is not configured');
31
                }
32
                $paypal = $this->paypal->where('id', 1)->first();
33
                if (!$paypal) {
34
                    throw new \Exception('Paypal Fields not given');
35
                }
36
                $data = $this->getFields($order);
37
                $this->postForm($data);
38
            }
39
        } catch (\Exception $ex) {
40
            dd($ex);
41
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
42
        }
43
    }
44
45
    public function getFields($invoice)
46
    {
47
        try {
48
            //dd($invoice);
49
            $item = [];
50
            $data = [];
51
            $user = \Auth::user();
52
            if (!$user) {
53
                throw new \Exception('No autherized user');
54
            }
55
            $config = $this->paypal->where('id', 1)->first();
56
            if ($config) {
57
                $business = $config->business;
58
                $cmd = $config->cmd;
59
                $return = $config->success_url;
60
                $cancel_return = $config->cancel_url;
61
                $notify_url = $config->notify_url;
62
                $image_url = $config->image_url;
63
                $rm = 1;
64
                $currency_code = $invoice->currency;
65
                $invoice_id = $invoice->id;
66
                $first_name = $user->first_name;
67
                $last_name = $user->last_name;
68
                $address1 = $user->address;
69
                $city = $user->town;
70
                $zip = $user->zip;
71
                $email = $user->email;
72
73
                $data = [
74
                    'business'      => $business,
75
                    'cmd'           => $cmd,
76
                    'return'        => $return,
77
                    'cancel_return' => $cancel_return,
78
                    'notify_url'    => $notify_url,
79
                    'image_url'     => $image_url,
80
                    'rm'            => $rm,
81
                    'currency_code' => $currency_code,
82
                    'invoice'       => $invoice_id,
83
                    'first_name'    => $first_name,
84
                    'last_name'     => $last_name,
85
                    'address1'      => $address1,
86
                    'city'          => $city,
87
                    'zip'           => $zip,
88
                    'email'         => $email,
89
                ];
90
91
                $items = $invoice->invoiceItem()->get()->toArray();
92
                //dd($items);
93
                if (count($items) > 0) {
94
                    for ($i = 0; $i < count($items); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
95
                        $n = $i + 1;
96
                        $item = [
97
                            "item_name_$n" => $items[$i]['product_name'],
98
                            "quantity_$n"  => $items[$i]['quantity'],
99
                        ];
100
                    }
101
                    $data = array_merge($data, $item);
102
                    $total = ['amount' => $invoice->grand_total];
103
                    $data = array_merge($data, $total);
104
                }
105
            }
106
107
            return $data;
108
        } catch (\Exception $ex) {
109
            dd($ex);
110
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
111
        }
112
    }
113
114
    public function postCurl($data)
115
    {
116
        try {
117
            $config = $this->paypal->where('id', 1)->first();
118
            if (!$config) {
119
                throw new \Exception('Paypal Fields not given');
120
            }
121
            $url = $config->paypal_url;
122
            $post_data = http_build_query($data);
123
            echo $url;
124
            dd($post_data);
125
            $ch = curl_init();
126
            curl_setopt($ch, CURLOPT_URL, $url);
127
            curl_setopt($ch, CURLOPT_POST, 1);
128
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
129
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
130
            $output = curl_exec($ch);
131
            curl_close($ch);
132
            dd($output);
133
        } catch (\Exception $ex) {
134
            dd($ex);
135
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
136
        }
137
    }
138
139
    public function postForm($data)
140
    {
141
        try {
142
            $config = $this->paypal->where('id', 1)->first();
143
            if (!$config) {
144
                throw new \Exception('Paypal Fields not given');
145
            }
146
            $url = $config->paypal_url;
147
            $gif_path = asset('dist/gif/gifloader.gif');
148
            echo "<img src=$gif_path>";
149
            echo "<form action=$url id=form name=redirect method=post>";
150
            foreach ($data as $key => $value) {
151
                echo "<input type=hidden name=$key value=$value>";
152
            }
153
            echo '</form>';
154
            echo"<script language='javascript'>document.redirect.submit();</script>";
155
        } catch (\Exception $ex) {
156
            dd($ex);
157
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
158
        }
159
    }
160
}
161