Skip to content

Commit

Permalink
Tested for WHMCS v8 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
rkbi committed Nov 5, 2024
1 parent 5b5ce5e commit 532ee45
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 240 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ SSLCOMMERZ is the first payment gateway in Bangladesh opening doors for merchant

### Prerequisites

1. WHMCS V6-V7
1. WHMCS V8.0 or later
2. cURL php extension.
3. [Sandbox Account](https://developer.sslcommerz.com/registration/ "SSLCommerz Sandbox Registration")
3. [Sandbox](https://developer.sslcommerz.com/registration/ "SSLCommerz Sandbox Registration") or [Live](https://join.sslcommerz.com/ "SSLCommerz Live Registration") API credentials

### Change Log

1. New version 4 API.
2. New easyCheckout Hosted & Popup UI.
3. Dynamic IPN URL(Auto Configure)
4. Tested upto V7.5.1 R2
4. Tested upto V8.8

### How to install the WHMCS module?

Expand Down
241 changes: 91 additions & 150 deletions modules/gateways/callback/sslcommerz.php
Original file line number Diff line number Diff line change
@@ -1,159 +1,100 @@
<?php
# Required File Includes
include("../../../init.php");
include("../../../includes/functions.php");
include("../../../includes/gatewayfunctions.php");
include("../../../includes/invoicefunctions.php");

$gatewaymodule = "sslcommerz"; # Enter your gateway module name here replacing template


$GATEWAY = getGatewayVariables($gatewaymodule);

if (!$GATEWAY["type"]) die("Module Not Activated"); # Checks gateway module is active before accepting callback
if (!isset($_POST)) die("No Post Data To Validate!");

$invoiceid = $_POST["tran_id"];
$transid = $_POST["tran_id"];

$store_id = $GATEWAY["store_id"];
$store_passwd = $GATEWAY["store_password"];
$systemurl = $GATEWAY['systemurl'];
$url_last_slash = substr($systemurl, strrpos($systemurl, '/') + 0);

if($_POST['status']=='VALID' && (isset($_POST['val_id']) && $_POST['val_id'] != "") && (isset($_POST['tran_id']) && $_POST['tran_id'] != ""))
{
if ($GATEWAY["testmode"] == "on")
{
$requested_url = ("https://sandbox.sslcommerz.com/validator/api/validationserverAPI.php?val_id=".$val_id."&Store_Id=".$store_id."&Store_Passwd=".$store_passwd."&v=1&format=json");
}
else
{
$requested_url = ("https://securepay.sslcommerz.com/validator/api/validationserverAPI.php?val_id=".$val_id."&Store_Id=".$store_id."&Store_Passwd=".$store_passwd."&v=1&format=json");
}

$orderData = mysql_fetch_assoc(select_query('tblinvoices', 'total', array("id" => $invoiceid)));

$order_amount = $orderData['total'];

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $requested_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$results = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

if($code == 200 && !( curl_errno($handle)))
{
$result = json_decode($results);

$status = $result->status;
$tran_date = $result->tran_date;
$tran_id = $result->tran_id;
$val_id = $result->val_id;
$amount = intval($result->amount);
$store_amount = $result->store_amount;
$amount = intval($result->amount);
$bank_tran_id = $result->bank_tran_id;
$card_type = $result->card_type;
$base_amount = $result->currency_amount;
$risk_level = $result->risk_level;
$base_fair=$result->base_fair;
$value_total=$result->value_b;

if(($status=='VALID' || $status=='VALIDATED') && ($order_amount == $base_amount) && $risk_level == 0)
{
$status = 'success';
}
else
{
$status = 'failed';
}
}
$invoiceid = checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing

$orderStatus = mysql_fetch_assoc(select_query('tblinvoices', 'status', array("id" => $invoiceid)));

if($orderStatus['status'] == "Paid")
{
logTransaction($GATEWAY["name"], array("Gateway Response" => $_POST, "Validation Response" => json_decode($results, true), "Response" => "Already Succeed By IPN"), "Successful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."clientarea.php?action=services"); /* Redirect browser */
}
else{
header("Location: ".$systemurl."/clientarea.php?action=services"); /* Redirect browser */
}
exit();
}

checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does

if ($status=="success") {
$fee = 0;
addInvoicePayment($invoiceid, $transid, $base_amount, $fee, $gatewaymodule);
logTransaction($GATEWAY["name"], $_POST, "Successful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."clientarea.php?action=services"); /* Redirect browser */
}
else{
header("Location: ".$systemurl."/clientarea.php?action=services"); /* Redirect browser */
}
exit();

}
else {
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."clientarea.php?action=services"); /* Redirect browser */
}
else{
header("Location: ".$systemurl."/clientarea.php?action=services"); /* Redirect browser */
}
exit();
}
# Required File Includes
include("../../../init.php");
include("../../../includes/functions.php");
include("../../../includes/gatewayfunctions.php");
include("../../../includes/invoicefunctions.php");

$gatewaymodule = "sslcommerz"; # Enter your gateway module name here replacing template


$GATEWAY = getGatewayVariables($gatewaymodule);

if (!$GATEWAY["type"])
die("Module Not Activated"); # Checks gateway module is active before accepting callback
if (!isset($_POST))
die("No Post Data To Validate!");

$invoiceid = $_POST["value_c"];
$tran_id = $_POST["tran_id"];
$val_id = $_POST["val_id"];


$store_id = $GATEWAY["store_id"];
$store_passwd = $GATEWAY["store_password"];
$systemurl = rtrim($GATEWAY['systemurl'], '/');

if ($_POST['status'] == 'VALID' && !empty($_POST['val_id']) && !empty($_POST['tran_id'])) {
if ($GATEWAY["testmode"] == "on") {
$validation_url = "https://sandbox.sslcommerz.com/validator/api/validationserverAPI.php?val_id=" . $val_id . "&store_id=" . $store_id . "&store_passwd=" . $store_passwd . "&v=1&format=json";
} else {
$validation_url = "https://securepay.sslcommerz.com/validator/api/validationserverAPI.php?val_id=" . $val_id . "&store_id=" . $store_id . "&store_passwd=" . $store_passwd . "&v=1&format=json";
}
else if($_POST['status']=='FAILED' && (isset($_POST['tran_id']) && $_POST['tran_id'] != ""))
{
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."clientarea.php?action=services"); /* Redirect browser */
}
else{
header("Location: ".$systemurl."/clientarea.php?action=services"); /* Redirect browser */
}
exit();

$handle = curl_init();
curl_setopt_array($handle, [
CURLOPT_URL => $validation_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => true,
CURLOPT_SSL_VERIFYPEER => true,
]);
$results = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

if ($code == 200 && !(curl_errno($handle))) {
$result = json_decode($results);

$status = $result->status;
$tran_date = $result->tran_date;
$tran_id = $result->tran_id;
$val_id = $result->val_id;
$amount = $result->amount;
$store_amount = $result->store_amount;
$bank_tran_id = $result->bank_tran_id;
$card_type = $result->card_type;
$base_amount = $result->currency_amount;
$risk_level = $result->risk_level;
$base_fair = $result->base_fair;
$value_total = $result->value_b;

if (($status == 'VALID' || $status == 'VALIDATED') && $risk_level == 0) {
$status = 'success';
} else {
$status = 'failed';
}
} else {
$status = 'failed';
}
else if($_POST['status']=='CANCELLED')
{
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."viewinvoice.php?id=" . $invoiceid);
}
else{
header("Location: ".$systemurl."/viewinvoice.php?id=" . $invoiceid);
}

checkCbInvoiceID($invoiceid, $GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
checkCbTransID($tran_id);

if ($status == "success") {
logTransaction($GATEWAY["name"], array("Gateway Response" => $_POST, "Validation Response" => json_decode($results, true), "Response" => "Already Succeed By IPN"), "Successful"); # Save to Gateway Log: name, data array, status
header("Location: " . $systemurl . "/clientarea.php?action=services"); /* Redirect browser */
exit();
}
else
{

checkCbTransID($tran_id); # Checks transaction number isn't already in the database and ends processing if it does

if ($status == "success") {
$fee = 0;
addInvoicePayment($invoiceid, $tran_id, $base_amount, $fee, $gatewaymodule);
logTransaction($GATEWAY["name"], $_POST, "Successful"); # Save to Gateway Log: name, data array, status
header("Location: " . $systemurl . "/clientarea.php?action=services"); /* Redirect browser */
exit();

} else {
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
if($url_last_slash == "/")
{
header("Location: ".$systemurl."clientarea.php?action=services"); /* Redirect browser */
}
else{
header("Location: ".$systemurl."/clientarea.php?action=services"); /* Redirect browser */
}
exit();
header("Location: " . $systemurl . "/clientarea.php?action=services"); /* Redirect browser */
exit();
}


?>
} else if (in_array($_POST['status'], ['FAILED', 'CANCELLED', 'UNATTEMPTED', 'EXPIRED']) && !empty($_POST['tran_id']) && !empty($_POST['value_c'])) {
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
header("Location: " . $systemurl . "/clientarea.php?action=services"); /* Redirect browser */
exit();
} else {
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful"); # Save to Gateway Log: name, data array, status
header("Location: " . $systemurl . "/clientarea.php?action=services"); /* Redirect browser */
exit();
}
1 change: 1 addition & 0 deletions modules/gateways/callback/sslcommerz_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
$post_data['cus_country'] = $json_data['cus_country'];
$post_data['value_a'] = $json_data['value_a'];
$post_data['value_b'] = $json_data['value_b'];
$post_data['value_c'] = $json_data['value_c'];
$post_data['shipping_method'] = 'NO';
$post_data['num_of_item'] = '1';
$post_data['product_name'] = $json_data['product_name'];
Expand Down
53 changes: 19 additions & 34 deletions modules/gateways/callback/sslcommerz_ipn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,26 @@
if (!$GATEWAY["type"]) die("Module Not Activated"); # Checks gateway module is active before accepting callback
if (!isset($_POST)) die("No Post Data To Validate!");

$invoiceid = $_POST["tran_id"];
$transid = $_POST["tran_id"];
$invoiceid = $_POST["value_c"];
$tran_id = $_POST["tran_id"];
$val_id = $_POST["val_id"];

$store_id = $GATEWAY["store_id"];
$store_passwd = $GATEWAY["store_password"];
$systemurl = $GATEWAY['systemurl'];
$url_last_slash = substr($systemurl, strrpos($systemurl, '/') + 0);

if($_POST['status']=='VALID' && (isset($_POST['val_id']) && $_POST['val_id'] != "") && (isset($_POST['tran_id']) && $_POST['tran_id'] != ""))
if($_POST['status']=='VALID' && !empty($_POST['val_id']) && !empty($_POST['tran_id']))
{
if ($GATEWAY["testmode"] == "on")
{
$requested_url = ("https://sandbox.sslcommerz.com/validator/api/validationserverAPI.php?val_id=".$val_id."&Store_Id=".$store_id."&Store_Passwd=".$store_passwd."&v=1&format=json");
}
else
{
$requested_url = ("https://securepay.sslcommerz.com/validator/api/validationserverAPI.php?val_id=".$val_id."&Store_Id=".$store_id."&Store_Passwd=".$store_passwd."&v=1&format=json");
}

$orderData = mysql_fetch_assoc(select_query('tblinvoices', 'total', array("id" => $invoiceid)));

$order_amount = $orderData['total'];

$subdomain = $GATEWAY["testmode"] == "on" ? "sandbox" : "securepay";
$validation_url = "https://." .$subdomain. ".com/validator/api/validationserverAPI.php?val_id=".$val_id."&store_id=".$store_id."&store_passwd=".$store_passwd."&v=1&format=json";

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $requested_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($handle, [
CURLOPT_URL => $validation_url,
CURLOPT_RETURNTRANSFER=> true,
CURLOPT_SSL_VERIFYHOST=> true,
CURLOPT_SSL_VERIFYPEER=> true,
]);
$result = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

Expand All @@ -49,20 +41,13 @@
$result = json_decode($result);

$status = $result->status;
$tran_date = $result->tran_date;
$tran_id = $result->tran_id;
$val_id = $result->val_id;
$amount = intval($result->amount);
$store_amount = $result->store_amount;
$amount = intval($result->amount);
$amount = $result->amount;
$bank_tran_id = $result->bank_tran_id;
$card_type = $result->card_type;
$base_amount = $result->currency_amount;
$currency_amount = $result->currency_amount;
$risk_level = $result->risk_level;
$base_fair=$result->base_fair;
$value_total=$result->value_b;

if(($status=='VALID' || $status=='VALIDATED') && ($order_amount == $base_amount) && $risk_level == 0)
if(($status=='VALID' || $status=='VALIDATED') && $risk_level == 0)
{
$status = 'success';
}
Expand All @@ -72,12 +57,12 @@
}
}

$invoiceid = checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does
checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
checkCbTransID($tran_id); # Checks transaction number isn't already in the database and ends processing if it does

if ($status=="success") {
$fee = 0;
addInvoicePayment($invoiceid, $transid, $base_amount, $fee, $gatewaymodule);
addInvoicePayment($invoiceid, $tran_id, $currency_amount, $fee, $gatewaymodule);
logTransaction($GATEWAY["name"], array("Gateway Response" => $_POST, "IPN Response" => "Succeed By IPN"), "Successful"); # Save to Gateway Log: name, data array, status
exit();

Expand Down
Loading

0 comments on commit 532ee45

Please sign in to comment.