Skip to content

Commit

Permalink
Merge pull request #31 from razorpay/redirect_issue_fix
Browse files Browse the repository at this point in the history
Fix the redirect issue and amount in paise stored in invoice issue
  • Loading branch information
ChetanGN committed May 13, 2021
2 parents 4201ef6 + 299dc67 commit 3406528
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ Visit [https://razorpay.com](https://razorpay.com) for support requests or email
This is licensed under the [MIT License][mit]

[mit]: https://opensource.org/licenses/MIT
[6]: https://github.com/razorpay/razorpay-whmcs/releases/tag/2.0.1
[6]: https://github.com/razorpay/razorpay-whmcs/releases/tag/2.1.0
[5]: https://github.com/razorpay/razorpay-whmcs/releases/tag/v1.0.3
2 changes: 1 addition & 1 deletion modules/gateways/razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Razorpay\Api\Api;
use Razorpay\Api\Errors;

const RAZORPAY_WHMCS_VERSION= '2.0.1';
const RAZORPAY_WHMCS_VERSION= '2.1.0';
const RAZORPAY_PAYMENT_ID = 'razorpay_payment_id';
const RAZORPAY_ORDER_ID = 'razorpay_order_id';
const RAZORPAY_SIGNATURE = 'razorpay_signature';
Expand Down
52 changes: 31 additions & 21 deletions modules/gateways/razorpay/razorpay-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,34 @@ function orderPaid(array $data, $gatewayParams)
// Order entity should be sent as part of the webhook payload
//
$orderId = $data['payload']['order']['entity']['notes']['whmcs_order_id'];
$razorpayPaymentId = $data['payload']['payment']['entity']['id'];

// Validate Callback Invoice ID.
$merchant_order_id = checkCbInvoiceID($orderId, $gatewayParams['name']);

// Check Callback Transaction ID.
checkCbTransID($razorpayPaymentId);

$orderTableId = mysql_fetch_assoc(select_query('tblorders', 'id', array("invoiceid"=>$orderId)));

$command = 'GetOrders';

$postData = array(
'id' => $orderId,
'id' => $orderTableId['id'],
);

$order = localAPI($command, $postData);

// If order detail not found then ignore.
// If it is already marked as paid or failed ignore the event
if($order['totalresults'] === 0 or $order['orders']['order'][0]['paymentstatus'] === 'Paid')
if($order['totalresults'] == 0 or $order['orders']['order'][0]['paymentstatus'] === 'Paid')
{
return;
}

$success = false;
$error = "";
$errorMessage = 'The payment has failed.';

$razorpayPaymentId = $data['payload']['payment']['entity']['id'];
$error = 'The payment has failed.';

$amount = getOrderAmountAsInteger($order);

Expand All @@ -148,24 +155,27 @@ function orderPaid(array $data, $gatewayParams)
$error = 'WHMCS_ERROR: Payment to Razorpay Failed. Amount mismatch.';
}

$log = [
'merchant_order_id' => $orderId,
'razorpay_payment_id' => $razorpayPaymentId
];
$log = [
'merchant_order_id' => $orderId,
'razorpay_payment_id' => $razorpayPaymentId,
'webhook' => true
];

if ($success === true)
{
# Successful
# Apply Payment to Invoice: invoiceid, transactionid, amount paid, fees, modulename
addInvoicePayment($orderId, $razorpayPaymentId, $amount, 0, $gatewayParams["name"]);
logTransaction($gatewayParams["name"], $log, "Successful"); # Save to Gateway Log: name, data array, status
}
else
{
# Unsuccessful
# Save to Gateway Log: name, data array, status
logTransaction($gatewayParams["name"], $log, "Unsuccessful-".$error . ". Please check razorpay dashboard for Payment id: ".$razorpayPaymentId);
}
{
# Successful
# Apply Payment to Invoice: invoiceid, transactionid, amount paid, fees, modulename
$orderAmount=$order['orders']['order'][0]['amount'];

addInvoicePayment($orderId, $razorpayPaymentId, $orderAmount, 0, $gatewayParams["name"]);
logTransaction($gatewayParams["name"], $log, "Successful"); # Save to Gateway Log: name, data array, status
}
else
{
# Unsuccessful
# Save to Gateway Log: name, data array, status
logTransaction($gatewayParams["name"], $log, "Unsuccessful-".$error . ". Please check razorpay dashboard for Payment id: ".$razorpayPaymentId);
}

// Graceful exit since payment is now processed.
exit;
Expand Down
13 changes: 9 additions & 4 deletions modules/gateways/razorpay/razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@
// Validate Callback Invoice ID.
$merchant_order_id = checkCbInvoiceID($merchant_order_id, $gatewayParams['name']);

// Check Callback Transaction ID.
checkCbTransID($razorpay_payment_id);

/**
* Fetch amount to verify transaction
*/
# Fetch invoice to get the amount and userid
$result = mysql_fetch_assoc(select_query('tblinvoices', 'total', array("id"=>$merchant_order_id)));
$result = mysql_fetch_assoc(select_query('tblinvoices', '*', array("id"=>$merchant_order_id)));

#check whether order is already paid or not, if paid then redirect to complete page
if($result['status'] === 'Paid')
{
header("Location: ".$gatewayParams['systemurl']."/viewinvoice.php?id=" . $merchant_order_id);

exit;
}

$amount = $result['total'];

Expand Down

0 comments on commit 3406528

Please sign in to comment.