Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added webhook notification at frontend order placement #295

Draft
wants to merge 1 commit into
base: master-2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 14 additions & 53 deletions Controller/Payment/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Order extends \Razorpay\Magento\Controller\BaseController

protected $orderRepository;

protected $logger;
public $logger;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
Expand Down Expand Up @@ -69,70 +69,31 @@ public function execute()

if (isset($_POST['order_check']))
{
if (empty($this->cache->load("quote_processing_".$receipt_id)) === false)
{
$responseContent = [
$responseContent = [
'success' => true,
'order_id' => false,
'parameters' => []
];

# fetch the related sales order and verify the payment ID with rzp payment id
# To avoid duplicate order entry for same quote
$collection = $this->_objectManager->get('Magento\Sales\Model\Order')
->getCollection()
->addFieldToSelect('entity_id')
->addFilter('quote_id', $receipt_id)
->getFirstItem();

$salesOrder = $collection->getData();

if (empty($salesOrder['entity_id']) === false)
{
$this->logger->info("Razorpay inside order already processed with webhook quoteID:" . $receipt_id
." and OrderID:".$salesOrder['entity_id']);

$this->checkoutSession
->setLastQuoteId($this->getQuote()->getId())
->setLastSuccessQuoteId($this->getQuote()->getId())
->clearHelperData();
];

$order = $this->orderRepository->get($salesOrder['entity_id']);
# fetch the related sales order and verify the payment ID with rzp payment id
# To avoid duplicate order entry for same quote
$orderLinkCollection = $this->_objectManager->get('Razorpay\Magento\Model\OrderLink')
->getCollection()
->addFilter('quote_id', $receipt_id)
->getFirstItem();

if ($order) {
$this->checkoutSession->setLastOrderId($order->getId())
->setLastRealOrderId($order->getIncrementId())
->setLastOrderStatus($order->getStatus());
}
$orderLink = $orderLinkCollection->getData();

$responseContent['order_id'] = true;
}
}
else
if (empty($orderLink['entity_id']) === false)
{
if(empty($receipt_id) === false)
{
//set the chache to stop webhook processing
$this->cache->save("started", "quote_Front_processing_$receipt_id", ["razorpay"], 30);

$this->logger->info("Razorpay front-end order processing started quoteID:" . $receipt_id);
$this->logger->info("Razorpay-frontend: Inside payment already notified with webhook for quoteID:" . $receipt_id);

$responseContent = [
'success' => false,
'parameters' => []
];
}
else
if (isset($orderLink['rzp_signature']) === true)
{
$this->logger->info("Razorpay order already processed with quoteID:" . $this->checkoutSession
->getLastQuoteId());

$responseContent = [
'success' => true,
'order_id' => true,
'success' => false,
'parameters' => []
];

}
}

Expand Down
15 changes: 12 additions & 3 deletions Model/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public function authorize(InfoInterface $payment, $amount)
{
$rzpOrderAmount = $order->getOrderCurrency()->formatTxt(number_format($rzp_order_amount_actual / 100, 2, ".", ""));

$this->order->logger->critical(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
throw new LocalizedException(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
}

Expand All @@ -305,6 +306,7 @@ public function authorize(InfoInterface $payment, $amount)
catch(\Razorpay\Api\Errors\Error $e)
{
$this->_logger->critical($e);
$this->order->logger->critical(__('Razorpay Error: %1.', $e->getMessage()));
throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage()));
}

Expand All @@ -318,6 +320,7 @@ public function authorize(InfoInterface $payment, $amount)
catch(\Razorpay\Api\Errors\Error $e)
{
$this->_logger->critical($e);
$this->order->logger->critical(__('Razorpay Error: %1.', $e->getMessage()));
throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage()));
}

Expand Down Expand Up @@ -353,25 +356,29 @@ public function authorize(InfoInterface $payment, $amount)
}

// Order processing through front-end
if(empty($request['paymentMethod']['additional_data']['rzp_payment_id']) === false)
if(empty($request['paymentMethod']['additional_data']) === false)
{
$payment_id = $request['paymentMethod']['additional_data']['rzp_payment_id'];
$payment_id = (empty($request['paymentMethod']['additional_data']['rzp_payment_id']) === false) ? $request['paymentMethod']['additional_data']['rzp_payment_id'] : $orderLink['rzp_payment_id'];

$rzp_order_id = $rzp_order_id;

$rzpOrderAmount = (int) $rzpOrderAmount;

$rzpSignature = (empty($request['paymentMethod']['additional_data']['rzp_signature']) === false) ? $request['paymentMethod']['additional_data']['rzp_signature'] : $orderLink['rzp_signature'];

if ($orderAmount !== $rzpOrderAmount)
{
$rzpOrderAmount = $order->getOrderCurrency()->formatTxt(number_format($rzpOrderAmount / 100, 2, ".", ""));

$this->order->logger->critical(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));

throw new LocalizedException(__("Cart order amount = %1 doesn't match with amount paid = %2", $order->getOrderCurrency()->formatTxt($order->getGrandTotal()), $rzpOrderAmount));
}

$this->validateSignature([
'razorpay_payment_id' => $payment_id,
'razorpay_order_id' => $rzp_order_id,
'razorpay_signature' => $request['paymentMethod']['additional_data']['rzp_signature']
'razorpay_signature' => $rzpSignature
]);
}
}
Expand All @@ -395,13 +402,15 @@ public function authorize(InfoInterface $payment, $amount)
$error = "Razorpay paymentId missing for payment verification.";

$this->_logger->critical($error);
$this->order->logger->critical($error);
throw new LocalizedException(__('Razorpay Error: %1.', $error));
}

}
catch (\Exception $e)
{
$this->_logger->critical($e);
$this->order->logger->critical($e->getMessage());
throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ define(
return this.razorpayDataFrameLoaded;
},

paymetClosed: false,

handleError: function (error) {
if (_.isObject(error)) {
this.messageContainer.addErrorMessage(error);
Expand Down Expand Up @@ -127,6 +129,8 @@ define(
getRzpOrderId: function () {
var self = this;

this.handleError("Please don't initiate new payment if already done once. Check with site Admin.");

//update shipping and billing before order into quotes
if(!quote.isVirtual()) {
shippingSaveProcessor.saveShippingInformation().success(
Expand Down Expand Up @@ -267,6 +271,8 @@ define(
checkRzpOrder: function (data) {
var self = this;

if(self.paymetClosed) return;

$.ajax({
type: 'POST',
url: url.build('razorpay/payment/order?' + Math.random().toString(36).substring(10)),
Expand All @@ -279,12 +285,7 @@ define(
success: function (response) {
//fullScreenLoader.stopLoader();
if (response.success) {
if(response.order_id){
$(location).attr('href', 'onepage/success?' + Math.random().toString(36).substring(10));
}else{
fullScreenLoader.startLoader();
setTimeout(function(){ self.checkRzpOrder(data); }, 1500);
}
setTimeout(function(){ self.checkRzpOrder(data); }, 3000);
} else {
self.placeOrder(data);
}
Expand Down Expand Up @@ -313,12 +314,14 @@ define(
handler: function (data) {
self.rzp_response = data;
fullScreenLoader.startLoader();
self.checkRzpOrder(data);
self.placeOrder(data);
},
order_id: data.rzp_order,
modal: {
ondismiss: function() {
self.paymetClosed = true;
self.isPaymentProcessing.reject("Payment Closed");
fullScreenLoader.stopLoader();
}
},
notes: {
Expand Down Expand Up @@ -349,6 +352,8 @@ define(
customerData.invalidate(['cart']);

this.rzp.open();

this.checkRzpOrder(data);
},

getData: function() {
Expand Down