From 3716475ab256da10e2138eb78eb7e5e9451145fa Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:01:22 +0200 Subject: [PATCH 1/8] PLUG-3910 - Fast checkout patch --- Controller/Checkout/Exchange.php | 5 ++++- Controller/Checkout/FastCheckoutStart.php | 4 ++-- Model/CreateFastCheckoutOrder.php | 16 ++++++++++++++-- view/frontend/web/css/payFastCheckout.css | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Controller/Checkout/Exchange.php b/Controller/Checkout/Exchange.php index 0dbf9f14..e0fbbeb8 100755 --- a/Controller/Checkout/Exchange.php +++ b/Controller/Checkout/Exchange.php @@ -204,7 +204,10 @@ public function execute() $order = $this->createFastCheckoutOrder->create($params); $orderEntityId = $order->getId(); } catch (\Exception $e) { - $this->payHelper->logCritical($e->getMessage(), $params); + $this->payHelper->logCritical('Fast checkout: ' . $e->getMessage(), $params); + if ($e->getCode() == 404) { + return $this->result->setContents('TRUE| Error creating fast checkout order. ' . $e->getMessage()); + } return $this->result->setContents('FALSE| Error creating fast checkout order. ' . $e->getMessage()); } } elseif (strpos($params['extra3'] ?? '', "fastcheckout") !== false) { diff --git a/Controller/Checkout/FastCheckoutStart.php b/Controller/Checkout/FastCheckoutStart.php index 014ba40b..7a76e6ea 100644 --- a/Controller/Checkout/FastCheckoutStart.php +++ b/Controller/Checkout/FastCheckoutStart.php @@ -160,12 +160,12 @@ private function getProducts() $productArr = []; foreach ($products as $key => $product) { - if ($product->getPrice() > 0) { + if ($product->getPriceInclTax() > 0) { $productArr[] = [ 'id' => $product->getProductId(), 'quantity' => $product->getQty(), 'description' => $product->getName(), - 'price' => $product->getPrice() * 100, + 'price' => $product->getPriceInclTax() * 100, 'currecny' => $this->storeManager->getStore()->getCurrentCurrencyCode(), 'type' => \Paynl\Transaction::PRODUCT_TYPE_ARTICLE, 'vatPercentage' => ($product->getPriceInclTax() - $product->getBasePrice()) / $product->getBasePrice() * 100, diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index 9fa63d74..54901c39 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -12,6 +12,7 @@ use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\OrderFactory; use Magento\Store\Model\StoreManagerInterface; +use Paynl\Payment\Helper\PayHelper; class CreateFastCheckoutOrder { @@ -65,6 +66,11 @@ class CreateFastCheckoutOrder */ private $searchCriteriaBuilder; + /** + * @var \Paynl\Payment\Helper\PayHelper; + */ + private $payHelper; + /** * @param StoreManagerInterface $storeManager * @param QuoteFactory $quote @@ -75,6 +81,7 @@ class CreateFastCheckoutOrder * @param ShippingMethodManagementInterface $shippingMethodManagementInterface * @param OrderRepositoryInterface $orderRepositoryInterface * @param SearchCriteriaBuilder $searchCriteriaBuilder + * @param PayHelper $payHelper */ public function __construct( StoreManagerInterface $storeManager, @@ -85,7 +92,8 @@ public function __construct( OrderFactory $orderFactory, ShippingMethodManagementInterface $shippingMethodManagementInterface, OrderRepositoryInterface $orderRepositoryInterface, - SearchCriteriaBuilder $searchCriteriaBuilder + SearchCriteriaBuilder $searchCriteriaBuilder, + PayHelper $payHelper ) { $this->storeManager = $storeManager; $this->quote = $quote; @@ -96,6 +104,7 @@ public function __construct( $this->shippingMethodManagementInterface = $shippingMethodManagementInterface; $this->orderRepositoryInterface = $orderRepositoryInterface; $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->payHelper = $payHelper; } /** @@ -111,6 +120,7 @@ public function create($params) $shippingAddressData = $checkoutData['shippingAddress'] ?? null; if (empty($customerData) || empty($billingAddressData) || empty($shippingAddressData)) { + $this->payHelper->logCritical("Fast checkout: Missing data, cannot create order.", ['customerData' => $customerData, 'billingAddressData' => $billingAddressData, 'shippingAddressData' => $shippingAddressData]); throw new \Exception("Missing data, cannot create order."); } @@ -235,6 +245,7 @@ public function create($params) $order->addStatusHistoryComment(__('PAY. - Created iDEAL Fast Checkout order'))->save(); } catch (NoSuchEntityException $e) { + $this->payHelper->logDebug('Fast checkout: Quote not found', ['quoteId' => $quoteId]); $order = $this->getExistingOrder($quoteId); } return $order; @@ -254,7 +265,8 @@ public function getExistingOrder($quoteId) $order = array_shift($searchResult); } if (empty($order)) { - throw new \Exception("Order can't be found."); + $this->payHelper->logCritical('Fast checkout: Both order & quote not found', ['quoteId' => $quoteId, 'searchCriteria' => $searchCriteria, 'searchResult' => $searchResult]); + throw new \Exception("Order & Quote can't be found. " . $quoteId, 404); } return $order; } diff --git a/view/frontend/web/css/payFastCheckout.css b/view/frontend/web/css/payFastCheckout.css index d9671629..92c8194e 100644 --- a/view/frontend/web/css/payFastCheckout.css +++ b/view/frontend/web/css/payFastCheckout.css @@ -19,6 +19,7 @@ background-repeat: no-repeat; background-size: 51px; background-position: 8px center; + color:white; } #top-cart-btn-fastcheckout { @@ -36,6 +37,7 @@ background-repeat: no-repeat; background-size: 51px; background-position: 8px center; + color:white; } #paynl_fast_checkout_fallback button { From 120e99fa0644b18b57e993370bcbd40ad1445f45 Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:25:46 +0200 Subject: [PATCH 2/8] Autoformat --- view/frontend/web/css/payFastCheckout.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/frontend/web/css/payFastCheckout.css b/view/frontend/web/css/payFastCheckout.css index 92c8194e..91be031d 100644 --- a/view/frontend/web/css/payFastCheckout.css +++ b/view/frontend/web/css/payFastCheckout.css @@ -19,7 +19,7 @@ background-repeat: no-repeat; background-size: 51px; background-position: 8px center; - color:white; + color: white; } #top-cart-btn-fastcheckout { @@ -37,7 +37,7 @@ background-repeat: no-repeat; background-size: 51px; background-position: 8px center; - color:white; + color: white; } #paynl_fast_checkout_fallback button { @@ -62,4 +62,4 @@ #paynl_fast_checkout_fallback button { max-width: 250px; } -} +} \ No newline at end of file From 4f99b452d05dcecfa7f9d3de8768867ecfd8d803 Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:29:14 +0200 Subject: [PATCH 3/8] Improve Catch --- Controller/Checkout/Exchange.php | 2 +- Model/CreateFastCheckoutOrder.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Controller/Checkout/Exchange.php b/Controller/Checkout/Exchange.php index e0fbbeb8..1633df58 100755 --- a/Controller/Checkout/Exchange.php +++ b/Controller/Checkout/Exchange.php @@ -205,7 +205,7 @@ public function execute() $orderEntityId = $order->getId(); } catch (\Exception $e) { $this->payHelper->logCritical('Fast checkout: ' . $e->getMessage(), $params); - if ($e->getCode() == 404) { + if ($e->getCode() == 404) { return $this->result->setContents('TRUE| Error creating fast checkout order. ' . $e->getMessage()); } return $this->result->setContents('FALSE| Error creating fast checkout order. ' . $e->getMessage()); diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index 54901c39..b8e7d867 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -245,8 +245,11 @@ public function create($params) $order->addStatusHistoryComment(__('PAY. - Created iDEAL Fast Checkout order'))->save(); } catch (NoSuchEntityException $e) { - $this->payHelper->logDebug('Fast checkout: Quote not found', ['quoteId' => $quoteId]); + $this->payHelper->logDebug('Fast checkout: Quote not found', ['quoteId' => $quoteId, 'error' => $e->getMessage()]); $order = $this->getExistingOrder($quoteId); + } catch (\Throwable $t) { + $this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $t->getMessage()]); + throw new \Exception("Exception on create. " . $t->getMessage()); } return $order; } From 9afd3c6d98c3427955e7de86a18fc1acf8847699 Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:18:13 +0200 Subject: [PATCH 4/8] Recover quote in case order doesn't exist --- Model/CreateFastCheckoutOrder.php | 50 +++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index b8e7d867..efbda05e 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -120,7 +120,7 @@ public function create($params) $shippingAddressData = $checkoutData['shippingAddress'] ?? null; if (empty($customerData) || empty($billingAddressData) || empty($shippingAddressData)) { - $this->payHelper->logCritical("Fast checkout: Missing data, cannot create order.", ['customerData' => $customerData, 'billingAddressData' => $billingAddressData, 'shippingAddressData' => $shippingAddressData]); + $this->payHelper->logCritical("Fast checkout: Missing data, cannot create order.", ['customerData' => $customerData, 'billingAddressData' => $billingAddressData, 'shippingAddressData' => $shippingAddressData]); // phpcs:ignore throw new \Exception("Missing data, cannot create order."); } @@ -129,8 +129,25 @@ public function create($params) $orderId = explode('fastcheckout', $params['orderId']); $quoteId = $orderId[1] ?? ''; + $this->payHelper->logDebug('Start fast checkout order create', ['quoteId' => $quoteId]); + try { $quote = $this->quote->create()->loadByIdWithoutStore($quoteId); + $this->payHelper->logDebug('Fast checkout: $quote->getId()', ['quoteId' => $quoteId, 'magentoQuoteId' => $quote->getId() ?? 'null']); + + if ($quote['is_active'] == false) { + $this->payHelper->logDebug('Fast checkout: Quote inactive', ['quoteId' => $quoteId]); + + $exsistingOrder = $this->getExistingOrder($quoteId); + if (empty($exsistingOrder)) { + $this->payHelper->logDebug('Fast checkout: Reactivating quote', ['quoteId' => $quoteId]); + $quote->setIsActive(true); + $quote->save(); + } else { + return $exsistingOrder; + } + } + $storeId = $quote->getStoreId(); $shippingMethodQuote = $quote->getShippingAddress()->getShippingMethod(); @@ -158,7 +175,7 @@ public function create($params) $quote->assignCustomer($customer); $quote->setSendConfirmation(1); - $billingAddress = $quote->getBillingAddress()->addData(array( + $quote->getBillingAddress()->addData(array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => $billingAddressData['firstName'] ?? $customerData['firstName'], @@ -180,7 +197,7 @@ public function create($params) 'save_in_address_book' => 1, )); - $shippingAddress = $quote->getShippingAddress()->addData(array( + $quote->getShippingAddress()->addData(array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => $shippingAddressData['firstName'] ?? $customerData['firstName'], @@ -206,22 +223,26 @@ public function create($params) $shippingAddress->setCollectShippingRates(true)->collectShippingRates(); $shippingData = $this->shippingMethodManagementInterface->getList($quote->getId()); - $shippingMethodsAvaileble = []; + $shippingMethodsAvailable = []; foreach ($shippingData as $shipping) { $code = $shipping->getCarrierCode() . '_' . $shipping->getMethodCode(); - $shippingMethodsAvaileble[$code] = $code; + $shippingMethodsAvailable[$code] = $code; } - if (!empty($shippingMethodsAvaileble[$shippingMethodQuote])) { + $this->payHelper->logDebug('Fast checkout: Available shipping methods', $shippingMethodsAvailable); + + if (!empty($shippingMethodsAvailable[$shippingMethodQuote])) { $shippingMethod = $shippingMethodQuote; - } elseif (!empty($shippingMethodsAvaileble[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping')])) { + } elseif (!empty($shippingMethodsAvailable[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping')])) { $shippingMethod = $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping'); - } elseif (!empty($shippingMethodsAvaileble[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')])) { + } elseif (!empty($shippingMethodsAvailable[$store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')])) { $shippingMethod = $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup'); } + $this->payHelper->logDebug('Fast checkout: Shipping options', ['quote' => $shippingMethodQuote, 'setting' => $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping'), 'setting_backup' => $store->getConfig('payment/paynl_payment_ideal/fast_checkout_shipping_backup')]); // phpcs:ignore + if (empty($shippingMethod)) { - throw new \Exception("No shipping method availeble"); + throw new \Exception("No shipping method available"); } $shippingAddress->setShippingMethod($shippingMethod); @@ -236,12 +257,14 @@ public function create($params) $service = $this->quoteManagement->submit($quote); $increment_id = $service->getRealOrderId(); + $this->payHelper->logDebug('Fast checkout: Reserved increment_id', [$increment_id]); $order = $this->orderFactory->create()->loadByIncrementId($increment_id); $additionalData = $order->getPayment()->getAdditionalInformation(); $additionalData['transactionId'] = $payOrderId; $order->getPayment()->setAdditionalInformation($additionalData); $order->save(); + $this->payHelper->logDebug('Fast checkout: Created order_id', [$order->getId()]); $order->addStatusHistoryComment(__('PAY. - Created iDEAL Fast Checkout order'))->save(); } catch (NoSuchEntityException $e) { @@ -251,12 +274,16 @@ public function create($params) $this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $t->getMessage()]); throw new \Exception("Exception on create. " . $t->getMessage()); } + if (empty($order)) { + $this->payHelper->logCritical('Fast checkout: Both order & quote not found', ['quoteId' => $quoteId, 'searchCriteria' => $searchCriteria, 'searchResult' => $searchResult]); + throw new \Exception("Order & Quote can't be found. " . $quoteId, 404); + } return $order; } /** * @param string $quoteId - * @return Order + * @return Order|boolean * @throws \Exception * @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing */ @@ -268,8 +295,7 @@ public function getExistingOrder($quoteId) $order = array_shift($searchResult); } if (empty($order)) { - $this->payHelper->logCritical('Fast checkout: Both order & quote not found', ['quoteId' => $quoteId, 'searchCriteria' => $searchCriteria, 'searchResult' => $searchResult]); - throw new \Exception("Order & Quote can't be found. " . $quoteId, 404); + return false; } return $order; } From 5bc36ed2fcdd0e5fb1c749d221eecdf9f1b878bf Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:19:55 +0200 Subject: [PATCH 5/8] Change catch --- Model/CreateFastCheckoutOrder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index efbda05e..9f0f2dfd 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -270,8 +270,8 @@ public function create($params) } catch (NoSuchEntityException $e) { $this->payHelper->logDebug('Fast checkout: Quote not found', ['quoteId' => $quoteId, 'error' => $e->getMessage()]); $order = $this->getExistingOrder($quoteId); - } catch (\Throwable $t) { - $this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $t->getMessage()]); + } catch (\Exception $e) { + $this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $e->getMessage()]); throw new \Exception("Exception on create. " . $t->getMessage()); } if (empty($order)) { From 0fca269703e8c2bce54aa623c1dbc74b2d806929 Mon Sep 17 00:00:00 2001 From: kevinverschoor <61683999+kevinverschoor@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:21:25 +0200 Subject: [PATCH 6/8] Fix catch --- Model/CreateFastCheckoutOrder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index 9f0f2dfd..c25e08a9 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -272,7 +272,7 @@ public function create($params) $order = $this->getExistingOrder($quoteId); } catch (\Exception $e) { $this->payHelper->logDebug('Fast checkout: Exception on create', ['quoteId' => $quoteId, 'error' => $e->getMessage()]); - throw new \Exception("Exception on create. " . $t->getMessage()); + throw new \Exception("Exception on create. " . $e->getMessage()); } if (empty($order)) { $this->payHelper->logCritical('Fast checkout: Both order & quote not found', ['quoteId' => $quoteId, 'searchCriteria' => $searchCriteria, 'searchResult' => $searchResult]); From 5f2f62a11c771ca0a0e8550a61af35e592173b38 Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 25 Oct 2024 10:41:13 +0200 Subject: [PATCH 7/8] Code polish --- Model/CreateFastCheckoutOrder.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Model/CreateFastCheckoutOrder.php b/Model/CreateFastCheckoutOrder.php index c25e08a9..1ad25da0 100644 --- a/Model/CreateFastCheckoutOrder.php +++ b/Model/CreateFastCheckoutOrder.php @@ -111,6 +111,7 @@ public function __construct( * @param array $params * @return Order * @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing + * @throws \Exception */ public function create($params) { @@ -133,18 +134,18 @@ public function create($params) try { $quote = $this->quote->create()->loadByIdWithoutStore($quoteId); - $this->payHelper->logDebug('Fast checkout: $quote->getId()', ['quoteId' => $quoteId, 'magentoQuoteId' => $quote->getId() ?? 'null']); + $this->payHelper->logDebug('Fast checkout: quote->getId()', ['quoteId' => $quoteId, 'magentoQuoteId' => $quote->getId() ?? 'null']); - if ($quote['is_active'] == false) { + if (empty($quote['is_active'])) { $this->payHelper->logDebug('Fast checkout: Quote inactive', ['quoteId' => $quoteId]); - $exsistingOrder = $this->getExistingOrder($quoteId); - if (empty($exsistingOrder)) { + $existingOrder = $this->getExistingOrder($quoteId); + if (empty($existingOrder)) { $this->payHelper->logDebug('Fast checkout: Reactivating quote', ['quoteId' => $quoteId]); $quote->setIsActive(true); $quote->save(); } else { - return $exsistingOrder; + return $existingOrder; } } From 13bc42c8a7d1d5c04644e032dcfcb2037a607d4a Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 26 Oct 2024 11:51:53 +0200 Subject: [PATCH 8/8] Code polish --- Controller/Checkout/Redirect.php | 8 +++----- composer.json | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Controller/Checkout/Redirect.php b/Controller/Checkout/Redirect.php index d3ffc6a8..527d4e29 100644 --- a/Controller/Checkout/Redirect.php +++ b/Controller/Checkout/Redirect.php @@ -88,13 +88,12 @@ public function execute() $payment = $order->getPayment(); if (empty($payment)) { - $this->_redirect('checkout/cart'); - return; + throw new \Exception('No payment found'); } $methodInstance = $this->paymentHelper->getMethodInstance($payment->getMethod()); if ($methodInstance instanceof \Paynl\Payment\Model\Paymentmethod\Paymentmethod) { - $this->payHelper->logNotice('Start new payment for order ' . $order->getId(), array(), $order->getStore()); + $this->payHelper->logNotice('Start new payment for order ' . $order->getId() . '. PayProfileId: ' . $methodInstance->getPaymentOptionId(), array(), $order->getStore()); $redirectUrl = $methodInstance->startTransaction($order); $this->getResponse()->setNoCacheHeaders(); $this->getResponse()->setRedirect($redirectUrl); @@ -105,8 +104,7 @@ public function execute() $this->_getCheckoutSession()->restoreQuote(); // phpcs:ignore $this->messageManager->addExceptionMessage($e, __('Something went wrong, please try again later')); $this->messageManager->addExceptionMessage($e, $e->getMessage()); - $this->payHelper->logCritical($e, array(), $order->getStore()); - + $this->payHelper->logCritical($e->getMessage(), array(), $order->getStore()); $this->_redirect('checkout/cart'); } } diff --git a/composer.json b/composer.json index a212131c..c134f5bc 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "paynl/magento2-plugin", "description": "PAY. Payment methods for Magento2", "type": "magento2-module", - "version": "3.14.3", + "version": "3.14.4", "require": { "magento/module-sales": "^102.0.0 || ^103.0.0", "magento/module-payment": "^100.3.0",