|
1 | 1 | <?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']) { |
15 | 20 | 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 |
22 | 25 | $merchant_order_id = $_POST["merchant_order_id"]; |
23 | 26 | $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 | + */ |
31 | 34 | # 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))); |
33 | 36 | $amount = $result['total']; |
34 | | - |
35 | 37 | # Check if amount is INR, convert if not. |
36 | 38 | $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 { |
43 | 44 | $converted_amount = $amount; |
44 | 45 | } |
45 | | - |
46 | 46 | # Amount in Paisa |
47 | 47 | $converted_amount = 100*$converted_amount; |
48 | | - |
49 | 48 | $success = true; |
50 | 49 | $error = ""; |
51 | | - |
52 | 50 | try { |
53 | 51 | $url = 'https://api.razorpay.com/v1/payments/'.$razorpay_payment_id.'/capture'; |
54 | 52 | $fields_string="amount=$converted_amount"; |
55 | | - |
56 | 53 | //cURL Request |
57 | 54 | $ch = curl_init(); |
58 | | - |
59 | 55 | //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); |
67 | 62 | //execute post |
68 | 63 | $result = curl_exec($ch); |
69 | 64 | $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
70 | | - |
71 | | - |
72 | | - if($result === false) { |
| 65 | + if ($result === false) { |
73 | 66 | $success = false; |
74 | 67 | $error = 'Curl error: ' . curl_error($ch); |
75 | | - } |
76 | | - else { |
| 68 | + } else { |
77 | 69 | $response_array = json_decode($result, true); |
78 | 70 | //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 { |
83 | 74 | $success = false; |
84 | | - |
85 | | - if(!empty($response_array['error']['code'])) { |
| 75 | + if (!empty($response_array['error']['code'])) { |
86 | 76 | $error = $response_array['error']['code'].":".$response_array['error']['description']; |
87 | | - } |
88 | | - else { |
| 77 | + } else { |
89 | 78 | $error = "RAZORPAY_ERROR:Invalid Response <br/>".$result; |
90 | 79 | } |
91 | 80 | } |
92 | 81 | } |
93 | 82 |
|
94 | 83 | //close connection |
95 | 84 | curl_close($ch); |
96 | | -} |
97 | | -catch (Exception $e) { |
| 85 | +} catch (Exception $e) { |
98 | 86 | $success = false; |
99 | 87 | $error ="WHMCS_ERROR:Request to Razorpay Failed"; |
100 | 88 | } |
101 | | - |
102 | 89 | if ($success === true) { |
103 | | - # Successful |
| 90 | + # Successful |
104 | 91 | # 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 { |
109 | 95 | # Unsuccessful |
110 | 96 | # 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']); |
112 | 98 | } |
113 | | - |
114 | | -header( "Location: ".$GATEWAY['systemurl']."/viewinvoice.php?id=" . $merchant_order_id ); |
115 | | -?> |
| 99 | +header("Location: ".$gatewayParams['systemurl']."/viewinvoice.php?id=" . $merchant_order_id); |
0 commit comments