Issues (40)

Controllers/PublishSslCommerzPaymentController.php (11 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\DB;
7
use AfzalSabbir\SSLaraCommerz\Library\SslCommerz\SslCommerzNotification;
8
9
class SslCommerzPaymentController extends Controller
0 ignored issues
show
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
{
11
    public function exampleEasyCheckout()
12
    {
13
        return view('sslaracommerz::exampleEasycheckout');
14
    }
15
16
    public function exampleHostedCheckout()
17
    {
18
        return view('sslaracommerz::exampleHosted');
19
    }
20
21
    public function index(Request $request)
22
    {
23
        # Here you have to receive all the order data to initate the payment.
24
        # Let's say, your oder transaction informations are saving in a table called "orders"
25
        # 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.
26
27
        $post_data                 = array();
28
        $post_data['total_amount'] = '10'; # You cant not pay less than 10
29
        $post_data['currency']     = "BDT";
30
        $post_data['tran_id']      = uniqid(); // tran_id must be unique
31
32
        # CUSTOMER INFORMATION
33
        $post_data['cus_name']     = 'Customer Name';
34
        $post_data['cus_email']    = '[email protected]';
35
        $post_data['cus_add1']     = 'Customer Address';
36
        $post_data['cus_add2']     = "";
37
        $post_data['cus_city']     = "";
38
        $post_data['cus_state']    = "";
39
        $post_data['cus_postcode'] = "";
40
        $post_data['cus_country']  = "Bangladesh";
41
        $post_data['cus_phone']    = '8801XXXXXXXXX';
42
        $post_data['cus_fax']      = "";
43
44
        # SHIPMENT INFORMATION
45
        $post_data['ship_name']     = "Store Test";
46
        $post_data['ship_add1']     = "Dhaka";
47
        $post_data['ship_add2']     = "Dhaka";
48
        $post_data['ship_city']     = "Dhaka";
49
        $post_data['ship_state']    = "Dhaka";
50
        $post_data['ship_postcode'] = "1000";
51
        $post_data['ship_phone']    = "";
52
        $post_data['ship_country']  = "Bangladesh";
53
54
        $post_data['shipping_method']  = "NO";
55
        $post_data['product_name']     = "Computer";
56
        $post_data['product_category'] = "Goods";
57
        $post_data['product_profile']  = "physical-goods";
58
59
        # OPTIONAL PARAMETERS
60
        $post_data['value_a'] = "ref001";
61
        $post_data['value_b'] = "ref002";
62
        $post_data['value_c'] = "ref003";
63
        $post_data['value_d'] = "ref004";
64
65
        #Before  going to initiate the payment order status need to insert or update as Pending.
66
        $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
67
            ->where('transaction_id', $post_data['tran_id'])
68
            ->updateOrInsert([
69
                'name'           => $post_data['cus_name'],
70
                'email'          => $post_data['cus_email'],
71
                'phone'          => $post_data['cus_phone'],
72
                'amount'         => $post_data['total_amount'],
73
                'status'         => 'Pending',
74
                'address'        => $post_data['cus_add1'],
75
                'transaction_id' => $post_data['tran_id'],
76
                'currency'       => $post_data['currency']
77
            ]);
78
79
        $sslc = new SslCommerzNotification();
80
        # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
81
        $payment_options = $sslc->makePayment($post_data, 'hosted');
82
83
        if (!is_array($payment_options)) {
84
            print_r($payment_options);
85
            $payment_options = array();
0 ignored issues
show
The assignment to $payment_options is dead and can be removed.
Loading history...
86
        }
87
88
    }
89
90
    public function payViaAjax(Request $request)
91
    {
92
        # Here you have to receive all the order data to initate the payment.
93
        # Lets your oder trnsaction informations are saving in a table called "orders"
94
        # 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.
95
96
        $post_data                 = array();
97
        $post_data['total_amount'] = '10'; # You cant not pay less than 10
98
        $post_data['currency']     = "BDT";
99
        $post_data['tran_id']      = uniqid(); // tran_id must be unique
100
101
        # CUSTOMER INFORMATION
102
        $post_data['cus_name']     = 'Customer Name';
103
        $post_data['cus_email']    = '[email protected]';
104
        $post_data['cus_add1']     = 'Customer Address';
105
        $post_data['cus_add2']     = "";
106
        $post_data['cus_city']     = "";
107
        $post_data['cus_state']    = "";
108
        $post_data['cus_postcode'] = "";
109
        $post_data['cus_country']  = "Bangladesh";
110
        $post_data['cus_phone']    = '8801XXXXXXXXX';
111
        $post_data['cus_fax']      = "";
112
113
        # SHIPMENT INFORMATION
114
        $post_data['ship_name']     = "Store Test";
115
        $post_data['ship_add1']     = "Dhaka";
116
        $post_data['ship_add2']     = "Dhaka";
117
        $post_data['ship_city']     = "Dhaka";
118
        $post_data['ship_state']    = "Dhaka";
119
        $post_data['ship_postcode'] = "1000";
120
        $post_data['ship_phone']    = "";
121
        $post_data['ship_country']  = "Bangladesh";
122
123
        $post_data['shipping_method']  = "NO";
124
        $post_data['product_name']     = "Computer";
125
        $post_data['product_category'] = "Goods";
126
        $post_data['product_profile']  = "physical-goods";
127
128
        # OPTIONAL PARAMETERS
129
        $post_data['value_a'] = "ref001";
130
        $post_data['value_b'] = "ref002";
131
        $post_data['value_c'] = "ref003";
132
        $post_data['value_d'] = "ref004";
133
134
135
        #Before  going to initiate the payment order status need to update as Pending.
136
        $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
137
            ->where('transaction_id', $post_data['tran_id'])
138
            ->updateOrInsert([
139
                'name'           => $post_data['cus_name'],
140
                'email'          => $post_data['cus_email'],
141
                'phone'          => $post_data['cus_phone'],
142
                'amount'         => $post_data['total_amount'],
143
                'status'         => 'Pending',
144
                'address'        => $post_data['cus_add1'],
145
                'transaction_id' => $post_data['tran_id'],
146
                'currency'       => $post_data['currency']
147
            ]);
148
149
        $sslc = new SslCommerzNotification();
150
        # initiate(Transaction Data , false: Redirect to SSLCOMMERZ gateway/ true: Show all the Payement gateway here )
151
        $payment_options = $sslc->makePayment($post_data, 'checkout', 'json');
152
153
        if (!is_array($payment_options)) {
0 ignored issues
show
The condition is_array($payment_options) is always false.
Loading history...
154
            print_r($payment_options);
155
            $payment_options = array();
0 ignored issues
show
The assignment to $payment_options is dead and can be removed.
Loading history...
156
        }
157
158
    }
159
160
    public function success(Request $request)
161
    {
162
        echo "Transaction is Successful";
163
164
        $tran_id  = $request->input('tran_id');
165
        $amount   = $request->input('amount');
166
        $currency = $request->input('currency');
167
168
        $sslc = new SslCommerzNotification();
169
170
        #Check order status in order tabel against the transaction id or order id.
171
        $order_detials = DB::table('orders')
172
            ->where('transaction_id', $tran_id)
173
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
174
175
        if ($order_detials->status == 'Pending') {
176
            $validation = $sslc->orderValidate($request->all(), $tran_id, $amount, $currency);
177
178
            if ($validation) {
179
                /*
180
                That means IPN did not work or IPN URL was not set in your merchant panel. Here you need to update order status
181
                in order table as Processing or Complete.
182
                Here you can also sent sms or email for successfull transaction to customer
183
                */
184
                $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
185
                    ->where('transaction_id', $tran_id)
186
                    ->update(['status' => 'Processing']);
187
188
                echo "<br >Transaction is successfully Completed";
189
            }
190
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
191
            /*
192
             That means through IPN Order status already updated. Now you can just show the customer that transaction is completed. No need to udate database.
193
             */
194
            echo "Transaction is successfully Completed";
195
        } else {
196
            #That means something wrong happened. You can redirect customer to your product page.
197
            echo "Invalid Transaction";
198
        }
199
200
201
    }
202
203
    public function fail(Request $request)
204
    {
205
        $tran_id = $request->input('tran_id');
206
207
        $order_detials = DB::table('orders')
208
            ->where('transaction_id', $tran_id)
209
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
210
211
        if ($order_detials->status == 'Pending') {
212
            $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
213
                ->where('transaction_id', $tran_id)
214
                ->update(['status' => 'Failed']);
215
            echo "Transaction is Falied";
216
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
217
            echo "Transaction is already Successful";
218
        } else {
219
            echo "Transaction is Invalid";
220
        }
221
222
    }
223
224
    public function cancel(Request $request)
225
    {
226
        $tran_id = $request->input('tran_id');
227
228
        $order_detials = DB::table('orders')
229
            ->where('transaction_id', $tran_id)
230
            ->select('transaction_id', 'status', 'currency', 'amount')->first();
231
232
        if ($order_detials->status == 'Pending') {
233
            $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
234
                ->where('transaction_id', $tran_id)
235
                ->update(['status' => 'Canceled']);
236
            echo "Transaction is Cancel";
237
        } else if ($order_detials->status == 'Processing' || $order_detials->status == 'Complete') {
238
            echo "Transaction is already Successful";
239
        } else {
240
            echo "Transaction is Invalid";
241
        }
242
243
244
    }
245
246
    public function ipn(Request $request)
247
    {
248
        #Received all the payement information from the gateway
249
        if ($request->input('tran_id')) #Check transation id is posted or not.
250
        {
251
252
            $tran_id = $request->input('tran_id');
253
254
            #Check order status in order tabel against the transaction id or order id.
255
            $order_details = DB::table('orders')
256
                ->where('transaction_id', $tran_id)
257
                ->select('transaction_id', 'status', 'currency', 'amount')->first();
258
259
            if ($order_details->status == 'Pending') {
260
                $sslc       = new SslCommerzNotification();
261
                $validation =
262
                    $sslc->orderValidate($request->all(), $tran_id, $order_details->amount, $order_details->currency);
263
                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...
264
                    /*
265
                    That means IPN worked. Here you need to update order status
266
                    in order table as Processing or Complete.
267
                    Here you can also sent sms or email for successful transaction to customer
268
                    */
269
                    $update_product = DB::table('orders')
0 ignored issues
show
The assignment to $update_product is dead and can be removed.
Loading history...
270
                        ->where('transaction_id', $tran_id)
271
                        ->update(['status' => 'Processing']);
272
273
                    echo "Transaction is successfully Completed";
274
                }
275
            } else if ($order_details->status == 'Processing' || $order_details->status == 'Complete') {
276
277
                #That means Order status already updated. No need to udate database.
278
279
                echo "Transaction is already successfully Completed";
280
            } else {
281
                #That means something wrong happened. You can redirect customer to your product page.
282
283
                echo "Invalid Transaction";
284
            }
285
        } else {
286
            echo "Invalid Data";
287
        }
288
    }
289
290
}