-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcashfree.php
executable file
·243 lines (213 loc) · 7.83 KB
/
cashfree.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
<?php
define('CASHFREE_PLUGIN_VERSION', '2.3.0', true);
define('API_VERSION', '2022-09-01');
/**
* WHMCS Cashfree Payment Gateway Module
*/
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
/**
* Define module related meta data.
* @return array
*/
function cashfree_MetaData()
{
return array(
'DisplayName' => 'Cashfree',
'APIVersion' => CASHFREE_PLUGIN_VERSION,
'DisableLocalCredtCardInput'=> true,
'TokenisedStorage' => false,
);
}
/**
* Define Cashfree gateway configuration options.
* @return array
*/
function cashfree_config()
{
return array(
'FriendlyName' => array(
'Type' => 'System',
'Value' => 'Cashfree',
),
'appId' => array(
'FriendlyName' => 'App Id',
'Type' => 'text',
'Size' => '50',
'Description' => 'Cashfree "App Id". Available <a href="https://www.cashfree.com/" target="_blank" style="bottom-border:1px dotted;">HERE</a>',
),
'secretKey' => array(
'FriendlyName' => 'Secret Key',
'Type' => 'password',
'Size' => '50',
'Description' => 'Cashfree "Secret Key" shared during activation API Key',
),
'themeLogo' => array(
'FriendlyName' => 'Logo URL',
'Type' => 'text',
'Size' => '50',
'Description' => 'ONLY "http<strong>s</strong>://"; else leave blank.<br/><small>Size: 128px X 128px (or higher) | File Type: png/jpg/gif/ico</small>',
),
'themeColor' => array(
'FriendlyName' => 'Theme Color',
'Type' => 'text',
'Size' => '15',
'Default' => '#15A4D3',
'Description' => 'The colour of checkout form elements',
),
'testMode' => array(
'FriendlyName' => 'Test Mode',
'Type' => 'yesno',
'Description' => 'Tick to enable test mode',
),
);
}
/**
* Payment link.
* Required by third party payment gateway modules only.
* 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 string
*/
function cashfree_link($params)
{
// Invoice Parameters
$invoice_id = $params['invoiceid'];
// System Parameters
$system_url = $params['systemurl'];
$module_name = $params['paymentmethod'];
$invoice_details = mysql_fetch_assoc(select_query('tblinvoices', '*', array("id" => $invoice_id)));
#check whether order is already paid or not, if paid then redirect to complete page
if($invoice_details['status'] === 'Paid')
{
header("Location: ".$system_url."/viewinvoice.php?id=" . $invoice_id);
exit;
}
//Cashfree request parameters
$cf_request = array();
$cf_request['orderId'] = 'cf'.time().'_'.$invoice_id;
$cf_request['returnUrl'] = $system_url . 'modules/gateways/cashfree/' . $module_name . '.php?order_id={order_id}';
$cf_request['notifyUrl'] = $system_url . 'modules/gateways/cashfree/' . $module_name . '_notify.php';
$mode = $cf_request['mode'] = ($params['testMode'] == 'on') ? 'sandbox' : 'production';
$payment_session_id = generatePaymentSession($cf_request,$params);
$html_output = <<<EOT
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>
</head>
<body>
<button type="button" id="renderBtn">
Pay Now
</button>
</body>
<script>
const cashfree = Cashfree({
mode: "$mode"
});
document.getElementById("renderBtn").addEventListener("click", () => {
cashfree.checkout({
paymentSessionId: "$payment_session_id",
platformName: "wh"
});
});
</script>
</html>
EOT;
return $html_output;
}
function generatePaymentSession($cf_request, $params)
{
$api_endpoint = ($params['testMode'] == 'on') ? 'https://sandbox.cashfree.com/pg/orders' : 'https://api.cashfree.com/pg/orders';
$get_cashfree_order_url = $api_endpoint."/".$cf_request['orderId'];
$get_order = getCfOrder($params, $get_cashfree_order_url);
if ($get_order && $get_order->order_status == 'ACTIVE' &&
$get_order->order_amount == $params['amount'] && $get_order->order_currency == $params['currency']) {
return $get_order->payment_session_id;
}
$payment_session_id = createCashfreeOrder($cf_request, $params, $api_endpoint);
if ($payment_session_id) {
return $payment_session_id;
} else {
die("Unable to create your order. Please contact support.");
}
}
function getCfOrder($params, $curl_url) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"x-api-version: " . API_VERSION,
"x-client-id: ".$params['appId'],
"x-client-secret: ".$params['secretKey']
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
die("Unable to create your order. Please contact support.");
}
return json_decode($response);
}
function createCashfreeOrder($cf_request, $params, $api_endpoint) {
$customer_details = array(
"customer_id" => "WhmcsCustomer",
"customer_email" => $params['clientdetails']['email'],
"customer_name" => $params['clientdetails']['firstname'].' '.$params['clientdetails']['lastname'],
"customer_phone" => $params['clientdetails']['phonenumber']
);
$order_meta = array(
"return_url" => $cf_request['returnUrl'],
"notify_url" => $cf_request['notifyUrl']
);
$request = array(
"customer_details" => $customer_details,
"order_id" => $cf_request['orderId'],
"order_amount" => $params['amount'],
"order_currency" => $params['currency'],
"order_note" => "WHMCS Order",
"order_meta" => $order_meta
);
$curl_postfield = json_encode($request);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $api_endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $curl_postfield,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"x-api-version: " . API_VERSION,
"x-client-id: ".$params['appId'],
"x-client-secret: ".$params['secretKey']
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
die("Unable to create your order. Please contact support.");
}
$cf_order = json_decode($response);
if ($cf_order && !empty($cf_order->payment_session_id)) {
return $cf_order->payment_session_id;
} else {
return null;
}
}