Issues (40)

Http/Controllers/SslCommerzPaymentController.php (12 issues)

1
<?php
2
3
namespace AfzalSabbir\SSLaraCommerz\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\DB;
8
use AfzalSabbir\SSLaraCommerz\Library\SslCommerz\SslCommerzNotification;
9
10
class SslCommerzPaymentController extends Controller
11
{
12
    public function exampleEasyCheckout()
13
    {
14
        return view('sslaracommerz::exampleEasycheckout');
15
    }
16
17
    public function exampleHostedCheckout()
18
    {
19
        return view('sslaracommerz::exampleHosted');
20
    }
21
22
    public function index(Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public function index(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        # Here you have to receive all the order data to initate the payment.
25
        # Let's say, your oder transaction informations are saving in a table called "orders"
26
        # In "orders" table, order unique identity is "transaction_id". "status" field contain status of the transaction, "amount" is the order amount to be paid and "currency" is for storing Site Currency which will be checked with paid currency.
27
28
        $post_data                 = array();
29
        $post_data['total_amount'] = '10'; # You cant not pay less than 10
30
        $post_data['currency']     = "BDT";
31
        $post_data['tran_id']      = uniqid(); // tran_id must be unique
32
33
        # CUSTOMER INFORMATION
34
        $post_data['cus_name']     = 'Customer Name';
35
        $post_data['cus_email']    = '[email protected]';
36
        $post_data['cus_add1']     = 'Customer Address';
37
        $post_data['cus_add2']     = "";
38
        $post_data['cus_city']     = "";
39
        $post_data['cus_state']    = "";
40
        $post_data['cus_postcode'] = "";
41
        $post_data['cus_country']  = "Bangladesh";
42
        $post_data['cus_phone']    = '8801XXXXXXXXX';
43
        $post_data['cus_fax']      = "";
44
45
        # SHIPMENT INFORMATION
46
        $post_data['ship_name']     = "Store Test";
47
        $post_data['ship_add1']     = "Dhaka";
48
        $post_data['ship_add2']     = "Dhaka";
49
        $post_data['ship_city']     = "Dhaka";
50
        $post_data['ship_state']    = "Dhaka";
51
        $post_data['ship_postcode'] = "1000";
52
        $post_data['ship_phone']    = "";
53
        $post_data['ship_country']  = "Bangladesh";
54
55
        $post_data['shipping_method']  = "NO";
56
        $post_data['product_name']     = "Computer";
57
        $post_data['product_category'] = "Goods";
58
        $post_data['product_profile']  = "physical-goods";
59
60
        # OPTIONAL PARAMETERS
61
        $post_data['value_a'] = "ref001";
62
        $post_data['value_b'] = "ref002";
63
        $post_data['value_c'] = "ref003";
64
        $post_data['value_d'] = "ref004";
65
66
        #Before  going to initiate the payment order status need to insert or update as Pending.
67
        $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
68
            ->where('transaction_id', $post_data['tran_id'])
69
            ->updateOrInsert([
70
                'name'           => $post_data['cus_name'],
71
                'email'          => $post_data['cus_email'],
72
                'phone'          => $post_data['cus_phone'],
73
                'amount'         => $post_data['total_amount'],
74
                'status'         => 'Pending',
75
                'address'        => $post_data['cus_add1'],
76
                'transaction_id' => $post_data['tran_id'],
77
                'currency'       => $post_data['currency']
78
            ]);
79
80
        $sslc = new SslCommerzNotification();
81
        # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
82
        $payment_options = $sslc->makePayment($post_data, 'hosted');
83
84
        if (!is_array($payment_options)) {
85
            print_r($payment_options);
86
            $payment_options = array();
0 ignored issues
show
The assignment to $payment_options is dead and can be removed.
Loading history...
87
        }
88
89
    }
90
91
    public function payViaAjax(Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
    public function payViaAjax(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        # Here you have to receive all the order data to initate the payment.
94
        # Lets your oder trnsaction informations are saving in a table called "orders"
95
        # In orders table order uniq identity is "transaction_id","status" field contain status of the transaction, "amount" is the order amount to be paid and "currency" is for storing Site Currency which will be checked with paid currency.
96
97
        $post_data                 = array();
98
        $post_data['total_amount'] = '10'; # You cant not pay less than 10
99
        $post_data['currency']     = "BDT";
100
        $post_data['tran_id']      = uniqid(); // tran_id must be unique
101
102
        # CUSTOMER INFORMATION
103
        $post_data['cus_name']     = 'Customer Name';
104
        $post_data['cus_email']    = '[email protected]';
105
        $post_data['cus_add1']     = 'Customer Address';
106
        $post_data['cus_add2']     = "";
107
        $post_data['cus_city']     = "";
108
        $post_data['cus_state']    = "";
109
        $post_data['cus_postcode'] = "";
110
        $post_data['cus_country']  = "Bangladesh";
111
        $post_data['cus_phone']    = '8801XXXXXXXXX';
112
        $post_data['cus_fax']      = "";
113
114
        # SHIPMENT INFORMATION
115
        $post_data['ship_name']     = "Store Test";
116
        $post_data['ship_add1']     = "Dhaka";
117
        $post_data['ship_add2']     = "Dhaka";
118
        $post_data['ship_city']     = "Dhaka";
119
        $post_data['ship_state']    = "Dhaka";
120
        $post_data['ship_postcode'] = "1000";
121
        $post_data['ship_phone']    = "";
122
        $post_data['ship_country']  = "Bangladesh";
123
124
        $post_data['shipping_method']  = "NO";
125
        $post_data['product_name']     = "Computer";
126
        $post_data['product_category'] = "Goods";
127
        $post_data['product_profile']  = "physical-goods";
128
129
        # OPTIONAL PARAMETERS
130
        $post_data['value_a'] = "ref001";
131
        $post_data['value_b'] = "ref002";
132
        $post_data['value_c'] = "ref003";
133
        $post_data['value_d'] = "ref004";
134
135
136
        #Before  going to initiate the payment order status need to update as Pending.
137
        $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
138
            ->where('transaction_id', $post_data['tran_id'])
139
            ->updateOrInsert([
140
                'name'           => $post_data['cus_name'],
141
                'email'          => $post_data['cus_email'],
142
                'phone'          => $post_data['cus_phone'],
143
                'amount'         => $post_data['total_amount'],
144
                'status'         => 'Pending',
145
                'address'        => $post_data['cus_add1'],
146
                'transaction_id' => $post_data['tran_id'],
147
                'currency'       => $post_data['currency']
148
            ]);
149
150
        $sslc = new SslCommerzNotification();
151
        # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
152
        $payment_options = $sslc->makePayment($post_data, 'checkout', 'json');
153
154
        if (!is_array($payment_options)) {
0 ignored issues
show
The condition is_array($payment_options) is always false.
Loading history...
155
            print_r($payment_options);
156
            $payment_options = array();
0 ignored issues
show
The assignment to $payment_options is dead and can be removed.
Loading history...
157
        }
158
159
    }
160
161
    public function success(Request $request)
162
    {
163
        echo "Transaction is Successful";
164
165
        $tran_id  = $request->input('tran_id');
166
        $amount   = $request->input('amount');
167
        $currency = $request->input('currency');
168
169
        $sslc = new SslCommerzNotification();
170
171
        #Check order status in order tabel against the transaction id or order id.
172
        $order_detials = DB::table('orders')
173
            ->where('transaction_id', $tran_id)
174
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
175
176
        if ($order_detials->status == 'Pending') {
177
            $validation = $sslc->orderValidate($request->all(), $tran_id, $amount, $currency);
178
179
            if ($validation) {
180
                /*
181
                That means IPN did not work or IPN URL was not set in your merchant panel. Here you need to update order status
182
                in order table as Processing or Complete.
183
                Here you can also sent sms or email for successfull transaction to customer
184
                */
185
                $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
186
                    ->where('transaction_id', $tran_id)
187
                    ->update(['status' => 'Processing']);
188
189
                echo "<br >Transaction is successfully Completed";
190
            }
191
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
192
            /*
193
             That means through IPN Order status already updated. Now you can just show the customer that transaction is completed. No need to udate database.
194
             */
195
            echo "Transaction is successfully Completed";
196
        } else {
197
            #That means something wrong happened. You can redirect customer to your product page.
198
            echo "Invalid Transaction";
199
        }
200
201
202
    }
203
204
    public function fail(Request $request)
205
    {
206
        $tran_id = $request->input('tran_id');
207
208
        $order_detials = DB::table('orders')
209
            ->where('transaction_id', $tran_id)
210
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
211
212
        if ($order_detials->status == 'Pending') {
213
            $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
214
                ->where('transaction_id', $tran_id)
215
                ->update(['status' => 'Failed']);
216
            echo "Transaction is Falied";
217
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
218
            echo "Transaction is already Successful";
219
        } else {
220
            echo "Transaction is Invalid";
221
        }
222
223
    }
224
225
    public function cancel(Request $request)
226
    {
227
        $tran_id = $request->input('tran_id');
228
229
        $order_detials = DB::table('orders')
230
            ->where('transaction_id', $tran_id)
231
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
232
233
        if ($order_detials->status == 'Pending') {
234
            $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
235
                ->where('transaction_id', $tran_id)
236
                ->update(['status' => 'Canceled']);
237
            echo "Transaction is Cancel";
238
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
239
            echo "Transaction is already Successful";
240
        } else {
241
            echo "Transaction is Invalid";
242
        }
243
244
245
    }
246
247
    public function ipn(Request $request)
248
    {
249
        #Received all the payement information from the gateway
250
        if ($request->input('tran_id')) #Check transation id is posted or not.
251
        {
252
253
            $tran_id = $request->input('tran_id');
254
255
            #Check order status in order tabel against the transaction id or order id.
256
            $order_details = DB::table('orders')
257
                ->where('transaction_id', $tran_id)
258
                ->select('transaction_id', 'status', 'currency', 'amount')->first();
259
260
            if ($order_details->status == 'Pending') {
261
                $sslc       = new SslCommerzNotification();
262
                $validation =
263
                    $sslc->orderValidate($request->all(), $tran_id, $order_details->amount, $order_details->currency);
264
                if ($validation == TRUE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
265
                    /*
266
                    That means IPN worked. Here you need to update order status
267
                    in order table as Processing or Complete.
268
                    Here you can also sent sms or email for successful transaction to customer
269
                    */
270
                    $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
271
                        ->where('transaction_id', $tran_id)
272
                        ->update(['status' => 'Processing']);
273
274
                    echo "Transaction is successfully Completed";
275
                }
276
            } else if ($order_details->status == 'Processing' || $order_details->status == 'Complete') {
277
278
                #That means Order status already updated. No need to udate database.
279
280
                echo "Transaction is already successfully Completed";
281
            } else {
282
                #That means something wrong happened. You can redirect customer to your product page.
283
284
                echo "Invalid Transaction";
285
            }
286
        } else {
287
            echo "Invalid Data";
288
        }
289
    }
290
291
}