-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshurjopay.php
382 lines (343 loc) · 12.3 KB
/
shurjopay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
// *************************************************************************
// * *
// * WHMCS - The Complete Client Management, Billing & Support Solution *
// * Copyright (c) WHMCS Ltd. All Rights Reserved, *
// * Version:8.3.2 (8.3.2-release.1) *
// * BuildId:dac69f7.159 *
// * Build Date:25 Nov 2021 *
// * *
// *************************************************************************
// * *
// * Email: [email protected] *
// * Website: http://www.whmcs.com *
// * *
// *************************************************************************
// * *
// * This software is furnished under a license and may be used and copied *
// * only in accordance with the terms of such license and with the *
// * inclusion of the above copyright notice. This software or any other *
// * copies thereof may not be provided or otherwise made available to any *
// * other person. No title to and ownership of the software is hereby *
// * transferred. *
// * *
// * You may not reverse engineer, decompile, defeat license encryption *
// * mechanisms, or disassemble this software product or software product *
// * license. WHMCompleteSolution may terminate this license if you don't *
// * comply with any of the terms and conditions set forth in our end user *
// * license agreement (EULA). In such event, licensee agrees to return *
// * licensor or destroy all copies of software upon termination of the *
// * license. *
// * *
// * Please see the EULA file for the full End User License Agreement. *
// * *
// *************************************************************************
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
/**
* Define module related meta data.
*
* Values returned here are used to determine module related capabilities and
* settings.
*
* @return array
*/
function shurjopay_MetaData()
{
return array(
'DisplayName' => 'ShurjoPay',
'APIVersion' => '2.1', // Use API Version 1.1
'DisableLocalCredtCardInput' => true,
'TokenisedStorage' => false,
);
}
/**
* Define gateway configuration options.
*
* The fields you define here determine the configuration options that are
* presented to administrator users when activating and configuring your
* payment gateway module for use.
*
* @return array
*/
function shurjopay_config()
{
return array(
// the friendly display name for a payment gateway should be
// defined here for backwards compatibility
'FriendlyName' => array(
'Type' => 'System',
'Value' => 'ShurjoPay',
),
// a text field type allows for single line text input
'merchantName' => array(
'FriendlyName' => 'Merchant Name/ID',
'Type' => 'text',
'Size' => '25',
'Default' => '',
'Description' => 'Enter your merchant ID here',
),
// a password field type allows for masked text input
'merchantPassword' => array(
'FriendlyName' => 'Merchant Password',
'Type' => 'password',
'Size' => '25',
'Default' => '',
'Description' => 'Enter Merchant Password here',
),
'uniqeID' => array(
'FriendlyName' => 'Merchant prefix Unique ID',
'Type' => 'text',
'Size' => '25',
'Default' => '',
'Description' => 'Enter your unique id prefix here',
),
'merchantIP' => array(
'FriendlyName' => 'Merchant IP',
'Type' => 'text',
'Size' => '25',
'Default' => '',
'Description' => 'Enter your merchant ip here',
),
// the yesno field type displays a single checkbox option
'testMode' => array(
'FriendlyName' => 'Test Mode',
'Type' => 'yesno',
'Options' => array(
'1' => 'Sandbox',
'2' => 'Live',
),
'Description' => 'Tick to enable test mode',
),
);
}
/**
* Payment link.
*
* Defines the HTML output displayed on an invoice. Typically consists of an
* HTML form that will take the user to the payment gateway endpoint.
*
* @param array $params Payment Gateway Module Parameters
*
* @return json
*/
function shurjopay_link($params)
{
// Gateway Configuration Parameters
$merchantName = $params['merchantName'];
$merchantPassword = $params['merchantPassword'];
$isSandbox = $params['testMode'];
$uniqeID = $params['uniqeID'];
$returnUrl = $params['returnUrl'];
$merchantIP = get_client_ip();
// System Parameters
$companyName = $params['companyname'];
$systemUrl = $params['systemurl'];
$returnUrl = $params['returnurl'];
$langPayNow = $params['langpaynow'];
$moduleDisplayName = $params['name'];
$moduleName = $params['paymentmethod'];
$whmcsVersion = $params['whmcsVersion'];
// Invoice Parameters
$invoiceId = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount'];
$currencyCode = $params['currency'];
$returnUrl = $systemUrl . 'viewinvoice.php?id=' . $invoiceId;
$callback_url = $systemUrl . 'modules/gateways/callback/' . $moduleName . '.php';
$postfields['return_url'] = $returnUrl;
$uniq_transaction_key=$uniqeID.time().'_'.$invoiceId;
// Get token
$token_raw_data = get_sp_token($isSandbox,$merchantName,$merchantPassword);
$token_data = json_decode($token_raw_data);
// Send payload
$requestbodyJson= json_encode (
array(
'token' => $token_data->token,
'store_id' => $token_data->store_id,
'prefix' => $uniqeID,
'currency' => $params['currency'],
'return_url' => $callback_url,
'cancel_url' => $callback_url,
'amount' => $amount,
// Order information
'order_id' => (string) $uniq_transaction_key,
'discsount_amount' => 0,
// Customer information
'client_ip' => $merchantIP,
'customer_name' => $params['clientdetails']['firstname']." ".$params['clientdetails']['lastname'],
'customer_phone' => $params['clientdetails']['phonenumber'],
'customer_email' => $params['clientdetails']['email'],
'customer_address' => $params['clientdetails']['address1']." ".$params['clientdetails']['address2'],
'customer_city' => $params['clientdetails']['city'],
'customer_state' => $params['clientdetails']['state'],
'customer_postcode' => $params['clientdetails']['state'],
'customer_country' => $params['clientdetails']['country'],
'custome1' => $params['description']
)
);
// Redirect to gateway
$response = send_payment($isSandbox,$token_data->token,$requestbodyJson);
$response_decoted = json_decode($response);
if(isset($response_decoted->checkout_url) && !empty($response_decoted->checkout_url)) {
//header("Location: {$response_decoted->checkout_url}");
//exit;
// Generate payment URL
$val = parse_url($response_decoted->checkout_url);
$host = $val['schema'].'//'.$val['host'].$val['path'];
$code .='<form method="GET" action="'.$host.'">';
$code .= '<input type="hidden" name="order_id" value="'.$response_decoted->sp_order_id.'" >';
$code .= '<input type="hidden" name="token" value="'.$token_data->token.'" >';
$code .='<input type="submit" value="Pay Now" />';
$code .='</form>';
return $code;
} else {
$errror = "Checkout url not found!";
logTransaction($GATEWAY["name"], $errror, "Unsuccessful");
}
}
/*
* Get ip of client
* @return string IP address
*/
function get_client_ip()
{
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if (isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = '127.0.0.1';
return $ipaddress;
}
/*
* Get token
* @ Request params bolean,credentials string
* @ Return token json string
*/
function get_sp_token($isSandbox,$username,$password)
{
$curl = curl_init();
if( $isSandbox == "on") {
$server_url = 'https://sandbox.shurjopayment.com';
} else {
$server_url = 'https://engine.shurjopayment.com';
}
$request_credential = array(
'username' => $username,
'password' => $password
);
$requestbodyJson = json_encode($request_credential);
curl_setopt_array($curl, [
CURLOPT_URL => $server_url.'/api/get_token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $requestbodyJson,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
return $response;
}
}
/*
* Redirect to gateway
* @ Request params bolean, credentials string
* @ Response json
*/
function send_payment($isSandbox,$token,$requestbodyJson)
{
if(empty($requestbodyJson)) {
return false;
}
if($isSandbox == 'on') {
$server_url = 'https://sandbox.shurjopayment.com';
} else {
$server_url = 'https://engine.shurjopayment.com';
}
$curl = curl_init();
$array = json_decode($token, true);
curl_setopt_array($curl, [
CURLOPT_URL => $server_url."/api/secret-pay",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $requestbodyJson,
CURLOPT_HTTPHEADER => [
"Content-Type: Application/json",
"Authorization: Bearer ". $token
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "Error #:" . $err;
} else {
return $response;
}
}
/*
* Payment verification call
* @ Request params bolean, order id string
* @ Response json array
*/
function verify_payment($isSandbox,$orderid)
{
if(empty($orderid)) {
return false;
}
if($isSandbox == 'on') {
$server_url = 'https://sandbox.shurjopayment.com';
} else {
$server_url = 'https://engine.shurjopayment.com';
}
$curl = curl_init();
$token = get_token($username,$password);
$array = json_decode($token, true);
curl_setopt_array($curl, [
CURLOPT_URL => $server_url."/api/verification",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\"order_id\":\"".$orderid."\"\n}",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer ". $array['token'],
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}