From 42eab9d1ad508ec4b5ea7bf81890ed870eef133a Mon Sep 17 00:00:00 2001 From: Prem Chand Date: Wed, 18 Aug 2021 18:29:23 +0530 Subject: [PATCH] Resolve issue of resource id aready exist --- Controller/Standard/Notify.php | 3 ++- Controller/Standard/Response.php | 35 ++++++++++++++++++++++++-------- Model/Cfcheckout.php | 8 ++++++-- composer.json | 2 +- etc/module.xml | 2 +- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Controller/Standard/Notify.php b/Controller/Standard/Notify.php index 6ef7451..054d0ef 100755 --- a/Controller/Standard/Notify.php +++ b/Controller/Standard/Notify.php @@ -163,7 +163,8 @@ public function execute() { $status = $paymentMethod->validateResponse($params); $referenceId = $params['referenceId']; if ($status == "SUCCESS") { - $quoteId = $params['orderId']; + $quoteId = strip_tags($params["orderId"]); + list($quoteId) = explode('_', $quoteId); if (empty($this->cache->load("quote_Front_processing_".$quoteId)) === false) { $this->logger->info("Cashfree Webhook: Order processing is active for quoteID: $quoteId"); diff --git a/Controller/Standard/Response.php b/Controller/Standard/Response.php index 750f71a..09af0fe 100755 --- a/Controller/Standard/Response.php +++ b/Controller/Standard/Response.php @@ -38,6 +38,7 @@ public function __construct( \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Store\Model\StoreManagerInterface $storeManagement, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, + \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepo, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory ) { @@ -49,6 +50,7 @@ public function __construct( $this->storeManagement = $storeManagement; $this->customerRepo = $customerRepo; $this->customerFactory = $customerFactory; + $this->orderRepository = $orderRepository; $this->objectManagement = \Magento\Framework\App\ObjectManager::getInstance(); parent::__construct( @@ -74,7 +76,8 @@ public function __construct( public function execute() { $returnUrl = $this->getCheckoutHelper()->getUrl('checkout'); $params = $this->getRequest()->getParams(); - $quoteId = $params['orderId']; + $quoteId = strip_tags($params["orderId"]); + list($quoteId) = explode('_', $quoteId); $quote = $this->getQuoteObject($params, $quoteId); if (!$this->getCustomerSession()->isLoggedIn()) { $customerId = $quote->getCustomer()->getId(); @@ -88,19 +91,33 @@ public function execute() { $status = $paymentMethod->validateResponse($params); $debugLog = ""; if ($status == "SUCCESS") { - $order = $this->quoteManagement->submit($quote); - - $payment = $order->getPayment(); + # fetch the related sales order + # To avoid duplicate order entry for same quote + $collection = $this->objectManagement->get('Magento\Sales\Model\Order') + ->getCollection() + ->addFieldToSelect('entity_id') + ->addFilter('quote_id', $quoteId) + ->getFirstItem(); + $salesOrder = $collection->getData(); + + if (empty($salesOrder['entity_id']) === true) { + $order = $this->quoteManagement->submit($quote); + $payment = $order->getPayment(); - $paymentMethod->postProcessing($order, $payment, $params); + $paymentMethod->postProcessing($order, $payment, $params); + } else { + $order = $this->orderRepository->get($salesOrder['entity_id']); + } $this->_checkoutSession ->setLastQuoteId($quote->getId()) ->setLastSuccessQuoteId($quote->getId()) ->clearHelperData(); - - $this->_checkoutSession->setLastOrderId($order->getId()) - ->setLastRealOrderId($order->getIncrementId()) - ->setLastOrderStatus($order->getStatus()); + + if ($order) { + $this->_checkoutSession->setLastOrderId($order->getId()) + ->setLastRealOrderId($order->getIncrementId()) + ->setLastOrderStatus($order->getStatus()); + } $returnUrl = $this->getCheckoutHelper()->getUrl('checkout/onepage/success'); $this->messageManager->addSuccess(__('Your payment was successful')); diff --git a/Model/Cfcheckout.php b/Model/Cfcheckout.php index 3181275..41d240e 100755 --- a/Model/Cfcheckout.php +++ b/Model/Cfcheckout.php @@ -49,6 +49,7 @@ public function __construct( \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory ) { @@ -57,6 +58,7 @@ public function __construct( $this->checkoutSession = $checkoutSession; $this->invoiceService = $invoiceService; $this->transaction = $transaction; + $this->orderSender = $orderSender; parent::__construct( $context, @@ -120,7 +122,7 @@ public function buildCheckoutRequest() { $params = array(); $params["appId"] = $this->getConfigData("app_id"); - $params["orderId"] = $quote->getEntityId(); + $params["orderId"] = $quote->getEntityId()."_".time(); $params["orderAmount"] = round($quote->getGrandTotal(), 2); $params["orderCurrency"] = $quote->getQuoteCurrencyCode(); $params["customerName"] = $billing_address->getFirstName(). " ". $billing_address->getLastName(); @@ -129,6 +131,7 @@ public function buildCheckoutRequest() { $params["customerPhone"] = $billing_address->getTelephone(); $params["notifyUrl"] = $this->getNotifyUrl(); + // $params["paymentModes"] = "upi"; $params["returnUrl"] = $this->getReturnUrl(); $params["source"] = "magento"; @@ -191,7 +194,7 @@ public function validateResponse($returnParams) { */ public function postProcessing(\Magento\Sales\Model\Order $order, \Magento\Framework\DataObject $payment, $response) { - + $payment->setTransactionId($response['referenceId']); $payment->setTransactionAdditionalInfo('Transaction Message', $response['txMsg']); $payment->setAdditionalInformation('cashfree_payment_status', 'approved'); @@ -201,6 +204,7 @@ public function postProcessing(\Magento\Sales\Model\Order $order, $order->setStatus('processing'); $order->setState('processing'); $order->save(); + $this->orderSender->send($order, true); $this->generateInvoice($order); } diff --git a/composer.json b/composer.json index 967db76..dad12eb 100755 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "docs": "https://docs.cashfree.com/docs/" }, "type": "magento2-module", - "version": "2.0.1", + "version": "2.1.0", "license": [ "MIT" ], diff --git a/etc/module.xml b/etc/module.xml index aab037e..b1ab8bd 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - +