Skip to content

Commit 26a2a0a

Browse files
committed
Merge branch 'whmcs-6'
2 parents b4ad4cb + 127110c commit 26a2a0a

File tree

3 files changed

+184
-117
lines changed

3 files changed

+184
-117
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
## Razorpay Payment Extension for WHMCS
2+
23
Allows you to use Razorpay payment gateway with the WHMCS Store.
34

45
## Description
56

6-
​This is the Razorpay payment gateway plugin for WHMCS. Allows Indian merchants to accept credit cards, debit cards, netbanking with the WHMCS store. It uses a seamless integration, allowing the customer to pay on your website without being redirected away from your WHMCS website.
7+
​This is the Razorpay payment gateway plugin for WHMCS. Allows Indian merchants to accept credit cards, debit cards, netbanking and wallet payments with the WHMCS store. It uses a seamless integration, allowing the customer to pay on your website without being redirected away from your WHMCS website.
8+
9+
## Branches
10+
11+
- Use the `master` branch if you are on WHMCS 6
12+
- Use the `whmcs-5` branch if you are on WHMCS 5
713

814
## Installation
15+
916
1. Ensure you have latest version of WHMCS installed.
1017
2. Download the zip of this repo.
1118
3. Upload the contents of the repo to your WHMCS Installation directory (content of module folder goes in module folder).
@@ -21,4 +28,10 @@ Allows you to use Razorpay payment gateway with the WHMCS Store.
2128

2229
### Support
2330

24-
Visit [https://razorpay.com](https://razorpay.com) for support requests or email contact@razorpay.com.
31+
Visit [https://razorpay.com](https://razorpay.com) for support requests or email <integrations@razorpay.com>.
32+
33+
### License
34+
35+
This is licensed under the [MIT License][mit]
36+
37+
[mit]: https://opensource.org/licenses/MIT
Lines changed: 55 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,99 @@
11
<?php
2-
3-
# Required File Includes
4-
include("../../../dbconnect.php");
5-
include("../../../includes/functions.php");
6-
include("../../../includes/gatewayfunctions.php");
7-
include("../../../includes/invoicefunctions.php");
8-
9-
$gatewaymodule = "razorpay";
10-
11-
$GATEWAY = getGatewayVariables($gatewaymodule);
12-
13-
# Checks gateway module is active before accepting callback
14-
if (!$GATEWAY["type"])
2+
/**
3+
* WHMCS Razorpay Payment Callback File
4+
*
5+
* Verifying that the payment gateway module is active,
6+
* Validating an Invoice ID, Checking for the existence of a Transaction ID,
7+
* Logging the Transaction for debugging and Adding Payment to an Invoice.
8+
*/
9+
10+
// Require libraries needed for gateway module functions.
11+
require_once __DIR__ . '/../../../init.php';
12+
require_once __DIR__ . '/../../../includes/gatewayfunctions.php';
13+
require_once __DIR__ . '/../../../includes/invoicefunctions.php';
14+
// Detect module name from filename.
15+
$gatewayModuleName = 'razorpay';
16+
// Fetch gateway configuration parameters.
17+
$gatewayParams = getGatewayVariables($gatewayModuleName);
18+
// Die if module is not active.
19+
if (!$gatewayParams['type']) {
1520
die("Module Not Activated");
16-
17-
$key_id = $GATEWAY["KeyId"];
18-
$key_secret = $GATEWAY["KeySecret"];
19-
20-
21-
# Get Returned Variables
21+
}
22+
$keyId = $gatewayParams["keyId"];
23+
$keySecret = $gatewayParams["keySecret"];
24+
// Retrieve data returned in payment gateway callback
2225
$merchant_order_id = $_POST["merchant_order_id"];
2326
$razorpay_payment_id = $_POST["razorpay_payment_id"];
24-
25-
# Checks invoice ID is a valid invoice number or ends processing
26-
$merchant_order_id = checkCbInvoiceID($merchant_order_id, $GATEWAY["name"]);
27-
28-
# Checks transaction number isn't already in the database and ends processing if it does
29-
checkCbTransID($razorpay_payment_id);
30-
27+
// Validate Callback Invoice ID.
28+
$merchant_order_id = checkCbInvoiceID($merchant_order_id, $gatewayParams['name']);
29+
// Check Callback Transaction ID.
30+
checkCbTransID($razorpay_payment_id);
31+
/**
32+
* Fetch amount to verify transaction
33+
*/
3134
# Fetch invoice to get the amount
32-
$result = mysql_fetch_assoc(select_query('tblinvoices','total',array("id"=>$merchant_order_id)));
35+
$result = mysql_fetch_assoc(select_query('tblinvoices', 'total', array("id"=>$merchant_order_id)));
3336
$amount = $result['total'];
34-
3537
# Check if amount is INR, convert if not.
3638
$currency = getCurrency();
37-
if($currency['code'] !== 'INR') {
38-
$result = mysql_fetch_array(select_query( "tblcurrencies", "id", array( "code" => 'INR' )));
39-
$inr_id= $result['id'];
40-
$converted_amount = convertCurrency($amount,$currency['id'], $inr_id);
41-
}
42-
else {
39+
if ($currency['code'] !== 'INR') {
40+
$result = mysql_fetch_array(select_query("tblcurrencies", "id", array("code"=>'INR')));
41+
$inr_id = $result['id'];
42+
$converted_amount = convertCurrency($amount, $currency['id'], $inr_id);
43+
} else {
4344
$converted_amount = $amount;
4445
}
45-
4646
# Amount in Paisa
4747
$converted_amount = 100*$converted_amount;
48-
4948
$success = true;
5049
$error = "";
51-
5250
try {
5351
$url = 'https://api.razorpay.com/v1/payments/'.$razorpay_payment_id.'/capture';
5452
$fields_string="amount=$converted_amount";
55-
5653
//cURL Request
5754
$ch = curl_init();
58-
5955
//set the url, number of POST vars, POST data
60-
curl_setopt($ch,CURLOPT_URL, $url);
61-
curl_setopt($ch,CURLOPT_USERPWD, $key_id . ":" . $key_secret);
62-
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
63-
curl_setopt($ch,CURLOPT_POST, 1);
64-
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
65-
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
66-
56+
curl_setopt($ch, CURLOPT_URL, $url);
57+
curl_setopt($ch, CURLOPT_USERPWD, $keyId . ":" . $keySecret);
58+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
59+
curl_setopt($ch, CURLOPT_POST, 1);
60+
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
61+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6762
//execute post
6863
$result = curl_exec($ch);
6964
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
70-
71-
72-
if($result === false) {
65+
if ($result === false) {
7366
$success = false;
7467
$error = 'Curl error: ' . curl_error($ch);
75-
}
76-
else {
68+
} else {
7769
$response_array = json_decode($result, true);
7870
//Check success response
79-
if($http_status === 200 and isset($response_array['error']) === false){
80-
$success = true;
81-
}
82-
else {
71+
if ($http_status === 200 and isset($response_array['error']) === false) {
72+
$success = true;
73+
} else {
8374
$success = false;
84-
85-
if(!empty($response_array['error']['code'])) {
75+
if (!empty($response_array['error']['code'])) {
8676
$error = $response_array['error']['code'].":".$response_array['error']['description'];
87-
}
88-
else {
77+
} else {
8978
$error = "RAZORPAY_ERROR:Invalid Response <br/>".$result;
9079
}
9180
}
9281
}
9382

9483
//close connection
9584
curl_close($ch);
96-
}
97-
catch (Exception $e) {
85+
} catch (Exception $e) {
9886
$success = false;
9987
$error ="WHMCS_ERROR:Request to Razorpay Failed";
10088
}
101-
10289
if ($success === true) {
103-
# Successful
90+
# Successful
10491
# Apply Payment to Invoice: invoiceid, transactionid, amount paid, fees, modulename
105-
addInvoicePayment($merchant_order_id, $razorpay_payment_id, $amount, 0, $GATEWAY["name"]);
106-
logTransaction($GATEWAY["name"], $_POST, "Successful"); # Save to Gateway Log: name, data array, status
107-
}
108-
else {
92+
addInvoicePayment($merchant_order_id, $razorpay_payment_id, $amount, 0, $gatewayParams["name"]);
93+
logTransaction($gatewayParams["name"], $_POST, "Successful"); # Save to Gateway Log: name, data array, status
94+
} else {
10995
# Unsuccessful
11096
# Save to Gateway Log: name, data array, status
111-
logTransaction($GATEWAY["name"], $_POST, "Unsuccessful-".$error . ". Please check razorpay dashboard for Payment id: ".$_POST['razorpay_payment_id']);
97+
logTransaction($gatewayParams["name"], $_POST, "Unsuccessful-".$error . ". Please check razorpay dashboard for Payment id: ".$_POST['razorpay_payment_id']);
11298
}
113-
114-
header( "Location: ".$GATEWAY['systemurl']."/viewinvoice.php?id=" . $merchant_order_id );
115-
?>
99+
header("Location: ".$gatewayParams['systemurl']."/viewinvoice.php?id=" . $merchant_order_id);

modules/gateways/razorpay.php

Lines changed: 114 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,140 @@
11
<?php
2-
function razorpay_config() {
32

4-
$configarray = array(
5-
"FriendlyName" => array("Type" => "System", "Value" => "Razorpay (Credit Card/Debit Card/Net Banking)"),
6-
"KeyId" => array("FriendlyName" => "Key Id", "Type" => "text", "Size" => "50", "Description" => "Enter your Razorpay Key Id here",),
7-
"KeySecret" => array("FriendlyName" => "Key Secret", "Type" => "text", "Size" => "50", "Description" => "Enter your Razorpay Key Secret here",),
3+
/**
4+
* WHMCS Razorpay Payment Gateway Module
5+
*/
6+
if (!defined("WHMCS")) {
7+
die("This file cannot be accessed directly");
8+
}
9+
/**
10+
* Define module related meta data.
11+
* @return array
12+
*/
13+
function razorpay_MetaData()
14+
{
15+
return array(
16+
'DisplayName' => 'Razorpay by KDC',
17+
'APIVersion' => '1.1', // Use API Version 1.1
18+
'DisableLocalCredtCardInput' => true,
19+
'TokenisedStorage' => false,
820
);
9-
return $configarray;
1021
}
11-
12-
function razorpay_link($params) {
13-
# Gateway Specific Variables
14-
$key_id = $params['KeyId'];
15-
$key_secret = $params['KeySecret'];
16-
17-
# Invoice Variables
18-
$order_id = $params['invoiceid'];
22+
/**
23+
* Define gateway configuration options.
24+
* @return array
25+
*/
26+
function razorpay_config()
27+
{
28+
return array(
29+
// the friendly display name for a payment gateway should be
30+
// defined here for backwards compatibility
31+
'FriendlyName' => array(
32+
'Type' => 'System',
33+
'Value' => 'Razorpay',
34+
),
35+
'keyId' => array(
36+
'FriendlyName' => 'Key Id',
37+
'Type' => 'text',
38+
'Size' => '50',
39+
//'Default' => 'rzp_',
40+
'Description' => 'Razorpay "Key Id". Available <a href="https://dashboard.razorpay.com/#/app/keys" target="_blank" style="bottom-border:1px dotted;">HERE</a>',
41+
),
42+
'keySecret' => array(
43+
'FriendlyName' => 'Key Secret',
44+
'Type' => 'text',
45+
'Size' => '50',
46+
//'Default' => '',
47+
'Description' => 'Razorpay "Key Secret" shared during activation API Key',
48+
),
49+
'themeLogo' => array(
50+
'FriendlyName' => 'Logo URL',
51+
'Type' => 'text',
52+
'Size' => '50',
53+
//'Default' => 'http://',
54+
'Description' => 'ONLY "http<strong>s</strong>://"; else leave blank.<br/><small>Size: 128px X 128px (or higher) | File Type: png/jpg/gif/ico</small>',
55+
),
56+
'themeColor' => array(
57+
'FriendlyName' => 'Theme Color',
58+
'Type' => 'text',
59+
'Size' => '15',
60+
'Default' => '#15A4D3',
61+
'Description' => 'The colour of checkout form elements',
62+
),
63+
);
64+
}
65+
/**
66+
* Payment link.
67+
* Required by third party payment gateway modules only.
68+
* Defines the HTML output displayed on an invoice. Typically consists of an
69+
* HTML form that will take the user to the payment gateway endpoint.
70+
* @param array $params Payment Gateway Module Parameters
71+
* @return string
72+
*/
73+
function razorpay_link($params)
74+
{
75+
// Gateway Configuration Parameters
76+
$keyId = $params['keyId'];
77+
$themeLogo = $params['themeLogo'];
78+
$themeColor = $params['themeColor'];
79+
// Invoice Parameters
80+
$invoiceId = $params['invoiceid'];
1981
$description = $params["description"];
20-
$amount = $params['amount']*100;
21-
$currency = $params['currency'];
22-
23-
# Client Variables
24-
$customer_name = $params['clientdetails']['firstname']." ".$params['clientdetails']['lastname'];
25-
$customer_email = $params['clientdetails']['email'];
26-
$customer_phone = $params['clientdetails']['phonenumber'];
82+
$amount = $params['amount']*100; // Required to be converted to Paisa.
83+
$currencyCode = $params['currency'];
84+
// Client Parameters
85+
$client_name = $params['clientdetails']['firstname'].' '.$params['clientdetails']['lastname'];
86+
$client_email = $params['clientdetails']['email'];
87+
$client_phone = $params['clientdetails']['phonenumber'];
88+
// System Parameters
89+
$companyName = $params['companyname'];
90+
$whmcsVersion = $params['whmcsVersion'];
91+
$callbackUrl = $params['systemurl'] . '/modules/gateways/callback/razorpay.php';
92+
$checkoutUrl = 'https://checkout.razorpay.com/v1/checkout.js';
2793

28-
# System Variables
29-
$name = $params['companyname'];
30-
$companyname = 'razorpay';
31-
$checkoutURL = 'https://checkout.razorpay.com/v1/checkout.js';
32-
$callbackURL = $params['systemurl'].'/modules/gateways/callback/razorpay.php';
33-
34-
35-
$html = '<form name="razorpay-form" id="razorpay-form" action="'.$callbackURL.'" method="POST" onSubmit="if(!razorpay_open) razorpaySubmit(); return razorpay_submit;">
94+
$html = '<form name="razorpay-form" id="razorpay-form" action="'.$callbackUrl.'" method="POST" onSubmit="if(!razorpay_open) razorpaySubmit(); return razorpay_submit;">
3695
<input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id" />
37-
<input type="hidden" name="merchant_order_id" id="order_id" value="'.$order_id.'"/>
96+
<input type="hidden" name="merchant_order_id" id="merchant_order_id" value="'.$invoiceId.'"/>
3897
<input type="button" value="Click Here to Pay" onClick="razorpaySubmit()"/>
3998
</form>';
4099

41-
$js = '<script src="'.$checkoutURL.'"></script>';
42-
100+
$js = '<script src="'.$checkoutUrl.'"></script>';
43101
$js .= "<script>
44102
var razorpay_open = false;
45103
var razorpay_submit = false;
46104
var razorpay_options = {
47-
'key': '".$key_id."',
105+
'key': '".$keyId."',
48106
'amount': '".$amount."',
49-
'currency': '".$currency."',
50-
'name': '".$name."',
51-
'description': '".$description."',
107+
'currency': '".$currencyCode."',
108+
'name': '".$companyName."',
109+
'description': 'Inv#".$invoiceId."',";
110+
111+
if (isset($themeLogo)&&$themeLogo!="") {
112+
if (strpos($theme_logo, 'https://')!== false) {
113+
$js .= "
114+
'image': '".$theme_logo."',";
115+
}
116+
}
117+
if (isset($themeColor)&&$themeColor!="") {
118+
$js .= "
119+
'theme': {
120+
'color': '".$themeColor."'
121+
},";
122+
}
123+
124+
$js .= "
52125
'handler': function (transaction) {
53126
razorpay_submit = true;
54127
document.getElementById('razorpay_payment_id').value = transaction.razorpay_payment_id;
55128
document.getElementById('razorpay-form').submit();
56129
},
57130
'prefill': {
58-
'name': '".$customer_name."',
59-
'email': '".$customer_email."',
60-
'contact': '".$customer_phone."'
131+
'name': '".$client_name."',
132+
'email': '".$client_email."',
133+
'contact': '".$client_phone."'
61134
},
62135
notes: {
63-
'whmcs_order_id': '".$order_id."'
136+
'whmcs_invoice_id': '".$invoiceId."',
137+
'whmcs_version': '".$whmcsVersion."'
64138
},
65139
netbanking: true
66140
};
@@ -71,10 +145,6 @@ function razorpaySubmit(){
71145
razorpay_open = true;
72146
rzp1.modal.options.backdropClose = false;
73147
}
74-
75148
</script>";
76-
77149
return $html.$js;
78-
79150
}
80-
?>

0 commit comments

Comments
 (0)