From bd9e2cee4322b2ee2fbd56c3933bcc837625df59 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Tue, 20 Aug 2024 15:26:39 +0200 Subject: [PATCH 1/7] fix --- Api/IdealQuoteCreateInterface.php | 2 - Model/Ideal/QuoteCreate.php | 263 +++++++++--------- .../view/checkout/ideal-fast-checkout/pay.js | 63 ++--- 3 files changed, 154 insertions(+), 174 deletions(-) diff --git a/Api/IdealQuoteCreateInterface.php b/Api/IdealQuoteCreateInterface.php index 8cb85b647..e9aa88e57 100644 --- a/Api/IdealQuoteCreateInterface.php +++ b/Api/IdealQuoteCreateInterface.php @@ -29,13 +29,11 @@ interface IdealQuoteCreateInterface /** * Get order breakdown after shipping is applied * - * @param ShippingAddressRequestInterface $shipping_address * @param string $page * @param string|null $order_data * @return QuoteCreateResponseInterface */ public function execute( - ShippingAddressRequestInterface $shipping_address, string $page, string $order_data = null ); diff --git a/Model/Ideal/QuoteCreate.php b/Model/Ideal/QuoteCreate.php index e56a7e427..4c308022f 100644 --- a/Model/Ideal/QuoteCreate.php +++ b/Model/Ideal/QuoteCreate.php @@ -26,60 +26,26 @@ use Buckaroo\Magento2\Model\Method\Ideal; use Magento\Quote\Api\ShipmentEstimationInterface; use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Customer\Api\AddressRepositoryInterface; use Magento\Checkout\Model\Session as CheckoutSession; use Magento\Customer\Model\Session as CustomerSession; use Buckaroo\Magento2\Api\IdealQuoteCreateInterface; use Buckaroo\Magento2\Model\Ideal\QuoteBuilderInterfaceFactory; -use Buckaroo\Magento2\Api\Data\Ideal\ShippingAddressRequestInterface; use Buckaroo\Magento2\Api\Data\QuoteCreateResponseInterfaceFactory; -use function Buckaroo\Magento2\Model\Ideal\__; +use Magento\Customer\Api\Data\CustomerInterface; +use Magento\Framework\Exception\NoSuchEntityException; class QuoteCreate implements IdealQuoteCreateInterface { - - /** - * @var \Buckaroo\Magento2\Api\Data\QuoteCreateResponseInterfaceFactory - */ protected $responseFactory; - - /** - * @var \Buckaroo\Magento2\Model\Ideal\QuoteBuilderInterfaceFactory - */ protected $quoteBuilderInterfaceFactory; - - /** - * @var \Magento\Customer\Model\Session - */ protected $customerSession; - - /** - * @var \Magento\Checkout\Model\Session - */ protected $checkoutSession; - - /** - * @var \Magento\Quote\Model\QuoteRepository - */ protected $quoteRepository; - - /** - * @var \Magento\Customer\Api\CustomerRepositoryInterface - */ protected $customerRepository; - - /** - * @var \Magento\Quote\Api\ShipmentEstimationInterface - */ + protected $addressRepository; protected $shipmentEstimation; - - /** - * @var \Buckaroo\Magento2\Logging\Log - */ protected $logger; - - /** - * @var \Magento\Quote\Model\Quote - */ protected $quote; public function __construct( @@ -87,6 +53,7 @@ public function __construct( QuoteBuilderInterfaceFactory $quoteBuilderInterfaceFactory, CustomerSession $customerSession, CustomerRepositoryInterface $customerRepository, + AddressRepositoryInterface $addressRepository, CheckoutSession $checkoutSession, QuoteRepository $quoteRepository, ShipmentEstimationInterface $shipmentEstimation, @@ -96,17 +63,23 @@ public function __construct( $this->quoteBuilderInterfaceFactory = $quoteBuilderInterfaceFactory; $this->customerSession = $customerSession; $this->customerRepository = $customerRepository; + $this->addressRepository = $addressRepository; $this->checkoutSession = $checkoutSession; $this->quoteRepository = $quoteRepository; $this->shipmentEstimation = $shipmentEstimation; $this->logger = $logger; } - /** @inheritDoc */ - public function execute( - ShippingAddressRequestInterface $shipping_address, - string $page, - string $form_data = null - ) { + + /** + * Execute quote creation and address handling + * + * @param string $page + * @param string|null $form_data + * @return \Buckaroo\Magento2\Api\Data\QuoteCreateResponseInterface + * @throws IdealException + */ + public function execute(string $page, string $form_data = null) + { if ($page === 'product' && is_string($form_data)) { $this->quote = $this->createQuote($form_data); } else { @@ -114,12 +87,20 @@ public function execute( } try { - $this->logger->addDebug('Adding address to quote', ['address' => $shipping_address]); - $this->addAddressToQuote($shipping_address); + if ($this->customerSession->isLoggedIn()) { + $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); + $defaultBillingAddress = $this->getAddress($customer->getDefaultBilling()); + $defaultShippingAddress = $this->getAddress($customer->getDefaultShipping()); + + $this->setAddresses($defaultShippingAddress, $defaultBillingAddress, $customer); + } else { + $this->setDefaultShippingAddress(); + } + $this->setPaymentMethod(); } catch (\Throwable $th) { $this->logger->addDebug(__METHOD__ . ' ' . $th->getMessage(), ['trace' => $th->getTraceAsString()]); - throw new IdealException(__("Failed to add address to quote"), 1, $th); + throw new IdealException(__("Failed to create quote"), 1, $th); } $this->calculateQuoteTotals(); @@ -127,133 +108,151 @@ public function execute( } /** - * Calculate quote totals, set store id required for quote masking, - * set customer email required for order validation - * @return void + * Get a customer's address by address ID + * + * @param int|null $addressId + * @return \Magento\Customer\Api\Data\AddressInterface|null */ - protected function calculateQuoteTotals() + protected function getAddress($addressId) { - $this->quote->setStoreId($this->quote->getStore()->getId()); - - if ($this->quote->getCustomerEmail() === null) { - $this->quote->setCustomerEmail('no-reply@example.com'); + if (!$addressId) { + return null; } - $this->quote - ->setTotalsCollectedFlag(false) - ->collectTotals(); - $this->quoteRepository->save($this->quote); + try { + return $this->addressRepository->getById($addressId); + } catch (NoSuchEntityException $e) { + $this->logger->error('Address not found', ['address_id' => $addressId]); + return null; + } } + /** - * Add address from Ideal Fast Checkout to quote + * Set the shipping and billing addresses for the quote * - * @param ShippingAddressRequestInterface $shipping_address + * @param \Magento\Customer\Api\Data\AddressInterface|null $shippingAddressData + * @param \Magento\Customer\Api\Data\AddressInterface|null $billingAddressData + * @param \Magento\Customer\Api\Data\CustomerInterface $customer + * @return void */ - protected function addAddressToQuote(ShippingAddressRequestInterface $shipping_address) + protected function setAddresses($shippingAddressData, $billingAddressData, $customer) { - if ($this->customerSession->isLoggedIn()) { - $this->quote->assignCustomerWithAddressChange( - $this->customerRepository->getById($this->customerSession->getCustomerId()) - ); - } - - $address = $this->quote->getShippingAddress(); + $shippingAddress = $this->quote->getShippingAddress(); + $billingAddress = $this->quote->getBillingAddress(); - $address->setCountryId($shipping_address->getCountryCode()); - $address->setPostcode($shipping_address->getPostalCode()); - $address->setCity($shipping_address->getCity()); - $address->setTelephone($shipping_address->getTelephone()); - $address->setFirstname($shipping_address->getFirstname()); - $address->setLastname($shipping_address->getLastname()); - $address->setEmail($shipping_address->getEmail()); - $address->setStreet($shipping_address->getStreet()); + if ($shippingAddressData) { + $shippingAddress->setFirstname($customer->getFirstname()); + $shippingAddress->setLastname($customer->getLastname()); + $shippingAddress->setEmail($customer->getEmail()); + $shippingAddress->setStreet($shippingAddressData->getStreet()); + $shippingAddress->setCity($shippingAddressData->getCity()); + $shippingAddress->setPostcode($shippingAddressData->getPostcode()); + $shippingAddress->setCountryId($shippingAddressData->getCountryId()); + $shippingAddress->setTelephone($shippingAddressData->getTelephone()); + } else { + $this->setPlaceholderAddress($shippingAddress); + } - $this->maybeFillAnyMissingAddressFields($shipping_address); + if ($billingAddressData) { + $billingAddress->setFirstname($customer->getFirstname()); + $billingAddress->setLastname($customer->getLastname()); + $billingAddress->setEmail($customer->getEmail()); + $billingAddress->setStreet($billingAddressData->getStreet()); + $billingAddress->setCity($billingAddressData->getCity()); + $billingAddress->setPostcode($billingAddressData->getPostcode()); + $billingAddress->setCountryId($billingAddressData->getCountryId()); + $billingAddress->setTelephone($billingAddressData->getTelephone()); + } else { + $this->setPlaceholderAddress($billingAddress); + } + $this->quote->setShippingAddress($shippingAddress); + $this->quote->setBillingAddress($billingAddress); $this->quoteRepository->save($this->quote); - $this->addFirstShippingMethod($address); + + $this->addFirstShippingMethod($shippingAddress); } - + /** - * Add first found shipping method to the shipping address & - * recalculate shipping totals - * - * @param Address $address + * Set default shipping and billing addresses for a guest * * @return void */ - protected function addFirstShippingMethod(Address $address) + protected function setDefaultShippingAddress() { - if (empty($address->getShippingMethod())) { - $shippingMethods = $this->shipmentEstimation->estimateByExtendedAddress( - $this->quote->getId(), - $this->quote->getShippingAddress() - ); - - if (count($shippingMethods)) { - $shippingMethod = array_shift($shippingMethods); - $address->setShippingMethod($shippingMethod->getCarrierCode(). '_' .$shippingMethod->getMethodCode()); - } - } - $address->setCollectShippingRates(true); - $address->collectShippingRates(); + $shippingAddress = $this->quote->getShippingAddress(); + $billingAddress = $this->quote->getBillingAddress(); + + $this->setPlaceholderAddress($shippingAddress); + $this->setPlaceholderAddress($billingAddress); + + $this->quote->setShippingAddress($shippingAddress); + $this->quote->setBillingAddress($billingAddress); + $this->quoteRepository->save($this->quote); + + $this->addFirstShippingMethod($shippingAddress); } /** - * Fill any fields missing from the addresses - * - * @param ShippingAddressRequestInterface $shipping_address + * Set placeholder values for the address if no customer information is available * + * @param \Magento\Quote\Model\Quote\Address $address * @return void */ - protected function maybeFillAnyMissingAddressFields(ShippingAddressRequestInterface $shipping_address) + protected function setPlaceholderAddress(Address $address) { - $this->maybeFillShippingAddressFields(); - $this->maybeFillBillingAddressFields($shipping_address); + $address->setFirstname('Guest'); + $address->setLastname('User'); + $address->setEmail('guest@example.com'); + $address->setStreet(['123 Placeholder St']); + $address->setCity('Placeholder City'); + $address->setPostcode('00000'); + $address->setCountryId('NL'); + $address->setTelephone('0000000000'); } /** - * If we didn't find any default shipping address we fill the empty fields - * required for quote validation + * Calculate quote totals and set store id and email if needed * * @return void */ - protected function maybeFillShippingAddressFields() + protected function calculateQuoteTotals() { - $address = $this->quote->getShippingAddress(); - if ($address->getId() === null) { - $address->setFirstname('unknown'); - $address->setLastname('unknown'); - $address->setEmail('no-reply@example.com'); - $address->setStreet('unknown'); + $this->quote->setStoreId($this->quote->getStore()->getId()); + + if ($this->quote->getCustomerEmail() === null) { + $this->quote->setCustomerEmail('no-reply@example.com'); } + + $this->quote->setTotalsCollectedFlag(false)->collectTotals(); + $this->quoteRepository->save($this->quote); } /** - * If we didn't find any default billing address we fill the empty fields - * required for quote validation - * - * @param ShippingAddressRequestInterface $shipping_address + * Add the first found shipping method to the shipping address * + * @param Address $address * @return void */ - protected function maybeFillBillingAddressFields(ShippingAddressRequestInterface $shipping_address) + protected function addFirstShippingMethod(Address $address) { - $address = $this->quote->getBillingAddress(); - if ($address->getId() === null) { - $address->setFirstname($shipping_address->getFirstname()); - $address->setLastname($shipping_address->getLastname()); - $address->setEmail($shipping_address->getEmail()); - $address->setStreet($shipping_address->getStreet()); - $address->setCountryId($shipping_address->getCountryCode()); - $address->setPostcode($shipping_address->getPostalCode()); - $address->setCity($shipping_address->getCity()); - $address->setTelephone($shipping_address->getTelephone()); + if (empty($address->getShippingMethod())) { + $shippingMethods = $this->shipmentEstimation->estimateByExtendedAddress( + $this->quote->getId(), + $this->quote->getShippingAddress() + ); + + if (count($shippingMethods)) { + $shippingMethod = array_shift($shippingMethods); + $address->setShippingMethod($shippingMethod->getCarrierCode() . '_' . $shippingMethod->getMethodCode()); + } } + $address->setCollectShippingRates(true); + $address->collectShippingRates(); } /** - * Set Ideal Fast Checkout payment method on quote + * Set the payment method on the quote * * @return Quote */ @@ -265,11 +264,11 @@ protected function setPaymentMethod() } /** - * Create quote if in product page + * Create a new quote if on the product page * * @param string $form_data - * * @return Quote + * @throws IdealException */ protected function createQuote(string $form_data) { @@ -278,7 +277,7 @@ protected function createQuote(string $form_data) $quoteBuilder->setFormData($form_data); return $quoteBuilder->build(); } catch (\Throwable $th) { - $this->logger->addDebug(__METHOD__.$th->getMessage()); + $this->logger->addDebug(__METHOD__ . ' ' . $th->getMessage()); throw new IdealException(__("Failed to create quote"), 1, $th); } } diff --git a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js index 8c1da01a6..9980e4b23 100644 --- a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js +++ b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js @@ -4,34 +4,30 @@ define([ 'Magento_Customer/js/customer-data', 'mage/translate', 'mage/storage', - 'Magento_Customer/js/model/customer', -], function ($, urlBuilder, customerData, $t, storage, customer) { + 'Magento_Checkout/js/model/full-screen-loader' +], function ($, urlBuilder, customerData, $t, storage, fullScreenLoader) { 'use strict'; return { createQuoteAndPlaceOrder: function (productData) { - // Add placeholders for required fields if not provided - productData.shipping_address = productData.shipping_address || this.getDefaultAddress(); this.page = productData.page; productData.order_data = this.getOrderData(); + var customerDataObject = customerData.get('customer'); + customerDataObject.subscribe(function (updatedCustomer) { + }.bind(this)); + + fullScreenLoader.startLoader(); // Show the loader + + this.processOrderFlow(productData); + }, + + processOrderFlow: function (productData) { // Create the quote $.post(urlBuilder.build("rest/V1/buckaroo/ideal/quote/create"), productData) .done(this.onQuoteCreateSuccess.bind(this, productData)) - .fail(this.onQuoteCreateFail.bind(this)); - }, - - getDefaultAddress: function () { - return { - firstname: 'unknown', - lastname: 'unknown', - email: 'no-reply@example.com', - street: 'unknown', - city: 'Placeholder', - country_code: 'NL', - postal_code: '00000', - telephone: '0000000000', - }; + .fail(this.onQuoteCreateFail.bind(this)) + .always(fullScreenLoader.stopLoader); // Hide the loader when the request is done }, getOrderData: function () { @@ -41,19 +37,20 @@ define([ onQuoteCreateSuccess: function (productData, quoteResponse) { var quoteId = quoteResponse.cart_id; - - // Proceed to place the order using the created quote ID this.placeOrder(quoteId, productData.paymentData); }, onQuoteCreateFail: function () { this.displayErrorMessage($t('Unable to create quote.')); + fullScreenLoader.stopLoader(); // Hide the loader on failure }, placeOrder: function (quoteId, paymentData) { var serviceUrl, payload; + var customerDataObject = customerData.get('customer'); - if (!customer.isLoggedIn()) { + // Determine the appropriate service URL and payload based on login status + if (!customerDataObject().firstname) { serviceUrl = urlBuilder.build(`rest/V1/guest-buckaroo/${quoteId}/payment-information`); payload = this.getPayload(quoteId, paymentData, 'guest'); } else { @@ -61,34 +58,20 @@ define([ payload = this.getPayload(quoteId, paymentData, 'customer'); } - storage.post( - serviceUrl, - JSON.stringify(payload) - ).done(this.onOrderPlaceSuccess.bind(this)) - .fail(this.onOrderPlaceFail.bind(this)); + storage.post(serviceUrl, JSON.stringify(payload)) + .done(this.onOrderPlaceSuccess.bind(this)) + .fail(this.onOrderPlaceFail.bind(this)) + .always(fullScreenLoader.stopLoader); // Hide the loader when the request is done }, getPayload: function (quoteId, paymentData, type) { - var billingAddress = { - city: 'Placeholder', - country_id: 'NL', - postcode: '00000', - street: ['Placeholder Street'], - telephone: '0000000000', - firstname: 'Placeholder', - lastname: 'Placeholder', - email: 'placeholder@example.com' - }; - return type === 'guest' ? { cartId: quoteId, - email: 'placeholder@example.com', + email: 'guest@example.com', paymentMethod: paymentData, - billingAddress: billingAddress } : { cartId: quoteId, paymentMethod: paymentData, - billingAddress: billingAddress }; }, From 208a72261d6e21ef5df2423aa2a42b6f2abe16f3 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Tue, 20 Aug 2024 15:59:18 +0200 Subject: [PATCH 2/7] add street addition --- Model/Push.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Model/Push.php b/Model/Push.php index 8de9e5385..da0e88cb6 100644 --- a/Model/Push.php +++ b/Model/Push.php @@ -332,6 +332,7 @@ private function extractAddress($addressType) 'lastname' => 'lastname', 'street' => 'street', 'housenumber' => 'housenumber', + 'addition' => 'addition', 'postalcode' => 'postcode', 'city' => 'city', 'countryname' => 'country_id', @@ -349,6 +350,12 @@ private function extractAddress($addressType) // Append house number to street if both are available if (isset($address['street']) && isset($address['housenumber'])) { $address['street'] .= ' ' . $address['housenumber']; + + // Add the addition (like 'A') if it exists + if (isset($address['addition'])) { + $address['street'] .= '-' . $address['addition']; + unset($address['addition']); + } unset($address['housenumber']); } From 50d9d027c15274b33128a03f23bb916b97b404e7 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Tue, 20 Aug 2024 16:43:28 +0200 Subject: [PATCH 3/7] add street addition --- Model/Push.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Push.php b/Model/Push.php index da0e88cb6..abb2a9596 100644 --- a/Model/Push.php +++ b/Model/Push.php @@ -353,7 +353,7 @@ private function extractAddress($addressType) // Add the addition (like 'A') if it exists if (isset($address['addition'])) { - $address['street'] .= '-' . $address['addition']; + $address['street'] .= $address['addition']; unset($address['addition']); } unset($address['housenumber']); From 43ad51f2c9d4ba69c3c4947559ec2335127ab1a9 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Tue, 20 Aug 2024 16:59:11 +0200 Subject: [PATCH 4/7] fix --- Model/Ideal/QuoteCreate.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Model/Ideal/QuoteCreate.php b/Model/Ideal/QuoteCreate.php index 4c308022f..926065dd9 100644 --- a/Model/Ideal/QuoteCreate.php +++ b/Model/Ideal/QuoteCreate.php @@ -19,6 +19,8 @@ */ namespace Buckaroo\Magento2\Model\Ideal; +use Buckaroo\Magento2\Api\Data\QuoteCreateResponseInterface; +use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Model\Quote; use Buckaroo\Magento2\Logging\Log; use Magento\Quote\Model\Quote\Address; @@ -75,18 +77,18 @@ public function __construct( * * @param string $page * @param string|null $form_data - * @return \Buckaroo\Magento2\Api\Data\QuoteCreateResponseInterface + * @return QuoteCreateResponseInterface * @throws IdealException */ public function execute(string $page, string $form_data = null) { - if ($page === 'product' && is_string($form_data)) { - $this->quote = $this->createQuote($form_data); - } else { - $this->quote = $this->checkoutSession->getQuote(); - } - try { + if ($page === 'product' && is_string($form_data)) { + $this->quote = $this->createQuote($form_data); + } else { + $this->quote = $this->checkoutSession->getQuote(); + } + if ($this->customerSession->isLoggedIn()) { $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); $defaultBillingAddress = $this->getAddress($customer->getDefaultBilling()); @@ -99,8 +101,7 @@ public function execute(string $page, string $form_data = null) $this->setPaymentMethod(); } catch (\Throwable $th) { - $this->logger->addDebug(__METHOD__ . ' ' . $th->getMessage(), ['trace' => $th->getTraceAsString()]); - throw new IdealException(__("Failed to create quote"), 1, $th); + throw new IdealException("Failed to create quote"); } $this->calculateQuoteTotals(); @@ -112,6 +113,7 @@ public function execute(string $page, string $form_data = null) * * @param int|null $addressId * @return \Magento\Customer\Api\Data\AddressInterface|null + * @throws LocalizedException */ protected function getAddress($addressId) { @@ -278,7 +280,7 @@ protected function createQuote(string $form_data) return $quoteBuilder->build(); } catch (\Throwable $th) { $this->logger->addDebug(__METHOD__ . ' ' . $th->getMessage()); - throw new IdealException(__("Failed to create quote"), 1, $th); + throw new IdealException("Failed to create quote"); } } } From eef17b51774ff7efc5d5fe804c32eea8b763a0d8 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Tue, 20 Aug 2024 17:10:24 +0200 Subject: [PATCH 5/7] fix --- .../view/checkout/ideal-fast-checkout/pay.js | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js index 9980e4b23..ce4c8400d 100644 --- a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js +++ b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js @@ -3,9 +3,8 @@ define([ 'mage/url', 'Magento_Customer/js/customer-data', 'mage/translate', - 'mage/storage', - 'Magento_Checkout/js/model/full-screen-loader' -], function ($, urlBuilder, customerData, $t, storage, fullScreenLoader) { + 'mage/storage' +], function ($, urlBuilder, customerData, $t, storage) { 'use strict'; return { @@ -17,17 +16,17 @@ define([ customerDataObject.subscribe(function (updatedCustomer) { }.bind(this)); - fullScreenLoader.startLoader(); // Show the loader - - this.processOrderFlow(productData); + this.processOrderFlow(productData) + .then(this.onQuoteCreateSuccess.bind(this, productData)) + .catch(this.onQuoteCreateFail.bind(this)); }, processOrderFlow: function (productData) { - // Create the quote - $.post(urlBuilder.build("rest/V1/buckaroo/ideal/quote/create"), productData) - .done(this.onQuoteCreateSuccess.bind(this, productData)) - .fail(this.onQuoteCreateFail.bind(this)) - .always(fullScreenLoader.stopLoader); // Hide the loader when the request is done + return new Promise((resolve, reject) => { + $.post(urlBuilder.build("rest/V1/buckaroo/ideal/quote/create"), productData) + .done((response) => resolve(response)) + .fail((error) => reject(error)); + }); }, getOrderData: function () { @@ -37,12 +36,13 @@ define([ onQuoteCreateSuccess: function (productData, quoteResponse) { var quoteId = quoteResponse.cart_id; - this.placeOrder(quoteId, productData.paymentData); + this.placeOrder(quoteId, productData.paymentData) + .then(this.onOrderPlaceSuccess.bind(this)) + .catch(this.onOrderPlaceFail.bind(this)); }, - onQuoteCreateFail: function () { + onQuoteCreateFail: function (error) { this.displayErrorMessage($t('Unable to create quote.')); - fullScreenLoader.stopLoader(); // Hide the loader on failure }, placeOrder: function (quoteId, paymentData) { @@ -58,10 +58,11 @@ define([ payload = this.getPayload(quoteId, paymentData, 'customer'); } - storage.post(serviceUrl, JSON.stringify(payload)) - .done(this.onOrderPlaceSuccess.bind(this)) - .fail(this.onOrderPlaceFail.bind(this)) - .always(fullScreenLoader.stopLoader); // Hide the loader when the request is done + return new Promise((resolve, reject) => { + storage.post(serviceUrl, JSON.stringify(payload)) + .done((response) => resolve(response)) + .fail((error) => reject(error)); + }); }, getPayload: function (quoteId, paymentData, type) { @@ -87,8 +88,8 @@ define([ this.updateOrder(jsonResponse); }, - onOrderPlaceFail: function (response) { - this.displayErrorMessage($t(response)); + onOrderPlaceFail: function (error) { + this.displayErrorMessage(error); }, updateOrder: function (jsonResponse) { @@ -106,8 +107,8 @@ define([ } else { message = $t("Cannot create payment"); } - } + customerData.set('messages', { messages: [{ type: 'error', From 37195c90009b35b91a92aade2f165aceaa70259c Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Wed, 21 Aug 2024 13:28:17 +0200 Subject: [PATCH 6/7] add loader --- .../js/view/checkout/ideal-fast-checkout/pay.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js index ce4c8400d..dc8be69c0 100644 --- a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js +++ b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js @@ -3,12 +3,15 @@ define([ 'mage/url', 'Magento_Customer/js/customer-data', 'mage/translate', - 'mage/storage' + 'mage/storage', + 'mage/loader' ], function ($, urlBuilder, customerData, $t, storage) { 'use strict'; return { createQuoteAndPlaceOrder: function (productData) { + this.showLoader(); + this.page = productData.page; productData.order_data = this.getOrderData(); @@ -42,6 +45,7 @@ define([ }, onQuoteCreateFail: function (error) { + this.hideLoader(); this.displayErrorMessage($t('Unable to create quote.')); }, @@ -49,7 +53,6 @@ define([ var serviceUrl, payload; var customerDataObject = customerData.get('customer'); - // Determine the appropriate service URL and payload based on login status if (!customerDataObject().firstname) { serviceUrl = urlBuilder.build(`rest/V1/guest-buckaroo/${quoteId}/payment-information`); payload = this.getPayload(quoteId, paymentData, 'guest'); @@ -77,6 +80,7 @@ define([ }, onOrderPlaceSuccess: function (response) { + this.hideLoader(); let jsonResponse; try { jsonResponse = $.parseJSON(response); @@ -89,6 +93,7 @@ define([ }, onOrderPlaceFail: function (error) { + this.hideLoader(); this.displayErrorMessage(error); }, @@ -115,6 +120,14 @@ define([ text: message }] }); + }, + + showLoader: function () { + $('body').loader('show'); + }, + + hideLoader: function () { + $('body').loader('hide'); } }; }); From 13eed5f57f6a3b14a7f530940197e9bd8c8608ba Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Wed, 21 Aug 2024 14:38:46 +0200 Subject: [PATCH 7/7] fix errors --- .../web/js/view/checkout/ideal-fast-checkout/pay.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js index dc8be69c0..5235ef3d0 100644 --- a/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js +++ b/view/frontend/web/js/view/checkout/ideal-fast-checkout/pay.js @@ -3,8 +3,7 @@ define([ 'mage/url', 'Magento_Customer/js/customer-data', 'mage/translate', - 'mage/storage', - 'mage/loader' + 'mage/storage' ], function ($, urlBuilder, customerData, $t, storage) { 'use strict'; @@ -114,12 +113,7 @@ define([ } } - customerData.set('messages', { - messages: [{ - type: 'error', - text: message - }] - }); + $('
' + message + '
').appendTo('.page.messages').show(); }, showLoader: function () {