Skip to content

Commit f2f8db7

Browse files
Merge pull request #24 from cashfree/version_bump_2.4.0
Support cashfree popup checkout
2 parents 44cbef1 + 04b4a1e commit f2f8db7

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

modules/gateways/cashfree.php

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
define('CASHFREE_PLUGIN_VERSION', '2.3.0', true);
2+
define('CASHFREE_PLUGIN_VERSION', '2.4.0', true);
33
define('API_VERSION', '2022-09-01');
44

55
/**
@@ -62,6 +62,11 @@ function cashfree_config()
6262
'Type' => 'yesno',
6363
'Description' => 'Tick to enable test mode',
6464
),
65+
'checkoutPopUp' => array(
66+
'FriendlyName' => 'Enable Popup Checkout',
67+
'Type' => 'yesno',
68+
'Description' => 'Tick to enable popup checkout',
69+
),
6570
);
6671
}
6772
/**
@@ -73,55 +78,60 @@ function cashfree_config()
7378
* @return string
7479
*/
7580
function cashfree_link($params)
76-
{
81+
{
7782
// Invoice Parameters
78-
$invoice_id = $params['invoiceid'];
83+
$invoice_id = $params['invoiceid'];
7984

8085
// System Parameters
81-
$system_url = $params['systemurl'];
82-
$module_name = $params['paymentmethod'];
86+
$system_url = $params['systemurl'];
87+
$module_name = $params['paymentmethod'];
8388
$invoice_details = mysql_fetch_assoc(select_query('tblinvoices', '*', array("id" => $invoice_id)));
8489

85-
#check whether order is already paid or not, if paid then redirect to complete page
86-
if($invoice_details['status'] === 'Paid')
87-
{
88-
header("Location: ".$system_url."/viewinvoice.php?id=" . $invoice_id);
89-
90+
// Check if the order is already paid
91+
if ($invoice_details['status'] === 'Paid') {
92+
header("Location: " . $system_url . "/viewinvoice.php?id=" . $invoice_id);
9093
exit;
91-
}
92-
93-
//Cashfree request parameters
94-
$cf_request = array();
95-
$cf_request['orderId'] = 'cf'.time().'_'.$invoice_id;
96-
$cf_request['returnUrl'] = $system_url . 'modules/gateways/cashfree/' . $module_name . '.php?order_id={order_id}';
97-
$cf_request['notifyUrl'] = $system_url . 'modules/gateways/cashfree/' . $module_name . '_notify.php';
98-
$mode = $cf_request['mode'] = ($params['testMode'] == 'on') ? 'sandbox' : 'production';
99-
$payment_session_id = generatePaymentSession($cf_request,$params);
94+
}
10095

96+
// Cashfree request parameters
97+
$cf_request = array(
98+
'orderId' => 'cf' . time() . '_' . $invoice_id,
99+
'returnUrl' => $system_url . 'modules/gateways/cashfree/' . $module_name . '.php?order_id={order_id}',
100+
'notifyUrl' => $system_url . 'modules/gateways/cashfree/' . $module_name . '_notify.php',
101+
'mode' => ($params['testMode'] == 'on') ? 'sandbox' : 'production'
102+
);
103+
$callback_url = $system_url . 'modules/gateways/cashfree/' . $module_name . '.php?order_id=' . $cf_request['orderId'];
104+
$payment_session_id = generatePaymentSession($cf_request, $params);
105+
// HTML Output
106+
$checkout_function = $params['checkoutPopUp'] == 'on' ? "openCheckout()" : "cashfree.checkout({paymentSessionId: '$payment_session_id', platformName: 'wh'})";
101107
$html_output = <<<EOT
102108
<!DOCTYPE html>
103109
<html lang="en">
104110
<head>
105111
<script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>
106112
</head>
107113
<body>
108-
<button type="button" id="renderBtn">
109-
Pay Now
110-
</button>
114+
<form action="javascript:void(0);"></form>
115+
<button type="button" id="renderBtn">Pay Now</button>
111116
</body>
112117
<script>
113-
const cashfree = Cashfree({
114-
mode: "$mode"
115-
});
116-
document.getElementById("renderBtn").addEventListener("click", () => {
118+
const cashfree = Cashfree({mode: "{$cf_request['mode']}"});
119+
function openCheckout(){
117120
cashfree.checkout({
118-
paymentSessionId: "$payment_session_id",
119-
platformName: "wh"
121+
paymentSessionId: "$payment_session_id",
122+
redirectTarget: "_modal",
123+
platformName: "wh"
124+
}).then((result) => {
125+
window.location.href = "$callback_url";
120126
});
127+
}
128+
document.getElementById("renderBtn").addEventListener("click", () => {
129+
$checkout_function;
121130
});
122131
</script>
123132
</html>
124-
EOT;
133+
EOT;
134+
125135
return $html_output;
126136
}
127137

@@ -223,15 +233,15 @@ function createCashfreeOrder($cf_request, $params, $api_endpoint) {
223233
]);
224234

225235
$response = curl_exec($curl);
226-
227-
$err = curl_error($curl);
228-
236+
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
229237
curl_close($curl);
230-
231-
if ($err) {
238+
if ($httpcode != 200) {
239+
$errArray = json_decode($response, true);
240+
if (isset($errArray['message'])) {
241+
die($errArray['message']);
242+
}
232243
die("Unable to create your order. Please contact support.");
233244
}
234-
235245
$cf_order = json_decode($response);
236246

237247
if ($cf_order && !empty($cf_order->payment_session_id)) {

modules/gateways/cashfree/cashfree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function checkTransIdExist($transaction_id) {
123123
// var_dump(checkTransIdExist($transaction_id));
124124
// die;
125125
if (!checkTransIdExist($transaction_id)){
126-
addInvoicePayment($invoice_id, $transaction_id, $invoice_amount, 0, $gateway_params["name"]);
126+
addInvoicePayment($invoice_id, $transaction_id, $invoice_amount, 0, $gateway_module_name);
127127

128128
# Successful
129129
# Save to Gateway Log: name, data array, status

0 commit comments

Comments
 (0)