diff --git a/Controller/Payment/Order.php b/Controller/Payment/Order.php index c2a760e5..ad213e7a 100644 --- a/Controller/Payment/Order.php +++ b/Controller/Payment/Order.php @@ -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 @@ -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' => [] ]; - } } diff --git a/Model/PaymentMethod.php b/Model/PaymentMethod.php index 519b15d0..ca5f3974 100644 --- a/Model/PaymentMethod.php +++ b/Model/PaymentMethod.php @@ -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)); } @@ -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())); } @@ -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())); } @@ -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 ]); } } @@ -395,6 +402,7 @@ 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)); } @@ -402,6 +410,7 @@ public function authorize(InfoInterface $payment, $amount) catch (\Exception $e) { $this->_logger->critical($e); + $this->order->logger->critical($e->getMessage()); throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage())); } diff --git a/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js b/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js index f66fa6e4..48b029c2 100644 --- a/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js @@ -51,6 +51,8 @@ define( return this.razorpayDataFrameLoaded; }, + paymetClosed: false, + handleError: function (error) { if (_.isObject(error)) { this.messageContainer.addErrorMessage(error); @@ -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( @@ -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)), @@ -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); } @@ -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: { @@ -349,6 +352,8 @@ define( customerData.invalidate(['cart']); this.rzp.open(); + + this.checkRzpOrder(data); }, getData: function() {