From 685ed20e8eeae989abe6f2576da1c7483a9f48fa Mon Sep 17 00:00:00 2001 From: pawankumarbrst Date: Mon, 29 May 2023 17:18:32 +0530 Subject: [PATCH] Callback updates --- Controller/Payment/Callback.php | 66 ++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/Controller/Payment/Callback.php b/Controller/Payment/Callback.php index 5b2ae8d..ab5a034 100644 --- a/Controller/Payment/Callback.php +++ b/Controller/Payment/Callback.php @@ -54,8 +54,9 @@ public function __construct( \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\DB\Transaction $transaction, - \Magento\Checkout\Model\Session $checkoutSession, - QuoteFactory $quoteFactory + QuoteFactory $quoteFactory, + \Magento\Store\Model\StoreManagerInterface $storeManager, + \Magento\Customer\Model\CustomerFactory $customerFactory ) { parent::__construct($context); $this->helper = $helper; @@ -64,7 +65,8 @@ public function __construct( $this->invoiceService = $invoiceService; $this->transaction = $transaction; $this->quoteFactory = $quoteFactory; - $this->checkoutSession = $checkoutSession; + $this->_storeManager = $storeManager; + $this->customerFactory = $customerFactory; } public function execute() @@ -87,12 +89,29 @@ public function execute() if (isset($payload["event_type"]) && $payload["event_type"] === "ORDER.PAYMENT.DETECTED") { if ($flow) { $quote = $this->quoteFactory->create()->load($payload["resource"]["reference"]); - if ($quote->getData('customer_id') == null) { - $quote->setCustomerId(null) - ->setCustomerEmail($quote->getBillingAddress()->getEmail()) - ->setCustomerIsGuest(true) - ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID); + if (is_null($quote->getCustomerId())) { + $customerEmail = $quote->getBillingAddress()->getEmail(); + $store=$this->_storeManager->getStore(); + $websiteId = $this->_storeManager->getStore()->getWebsiteId(); + $customer=$this->customerFactory->create(); + $customer->setWebsiteId($websiteId); + $customer->loadByEmail($customerEmail); + if ($customer->getEntityId()) { + $quote->setCustomerId($customer->getEntityId()); + $quote->setCustomerFirstname($customer->getFirstname()); + $quote->setCustomerLastname($customer->getLastname()); + $quote->setCustomerEmail($customerEmail); + $quote->setCustomerIsGuest(false); + }else{ + $quote->setCustomerFirstname($quote->getBillingAddress()->getFirstname()); + $quote->setCustomerLastname($quote->getBillingAddress()->getLastname()); + $quote->setCustomerEmail($customerEmail); + $quote->setCustomerIsGuest(true); + } } + // Save Quote + $quote->save(); + $quote->collectTotals()->save(); $result = $this->helper->createOrder($quote); if ($result['success']) { $order = $this->orderFactory->create()->loadByIncrementId($result['success']); @@ -103,8 +122,35 @@ public function execute() elseif (isset($payload["event_type"]) && $payload["event_type"] === "ORDER.PAYMENT.RECEIVED") { if ($flow) { $quote = $this->quoteFactory->create()->load($payload["resource"]["reference"]); - $quoteOrder = $this->quoteFactory->create()->load($quote->getId()); - $order = $this->orderFactory->create()->loadByIncrementId($quoteOrder->getReservedOrderId()); + if($quote->getIsActive()){ + if (is_null($quote->getCustomerId())) { + $customerEmail = $quote->getBillingAddress()->getEmail(); + $store=$this->_storeManager->getStore(); + $websiteId = $this->_storeManager->getStore()->getWebsiteId(); + $customer=$this->customerFactory->create(); + $customer->setWebsiteId($websiteId); + $customer->loadByEmail($customerEmail); + if ($customer->getEntityId()) { + $quote->setCustomerId($customer->getEntityId()); + $quote->setCustomerFirstname($customer->getFirstname()); + $quote->setCustomerLastname($customer->getLastname()); + $quote->setCustomerEmail($customerEmail); + $quote->setCustomerIsGuest(false); + }else{ + $quote->setCustomerFirstname($quote->getBillingAddress()->getFirstname()); + $quote->setCustomerLastname($quote->getBillingAddress()->getLastname()); + $quote->setCustomerEmail($customerEmail); + $quote->setCustomerIsGuest(true); + } + } + // Save Quote + $quote->save(); + $quote->collectTotals()->save(); + $result = $this->helper->createOrder($quote); + if ($result['success']) { + $order = $this->orderFactory->create()->loadByIncrementId($result['success']); + } + } } if ($order) { $payment = $order->getPayment();