From 1895d0cdf6a07ec41e5ce6de1a8989eacd507d27 Mon Sep 17 00:00:00 2001 From: Nickolas Malovanets Date: Thu, 24 Oct 2024 11:58:52 +0300 Subject: [PATCH 1/2] Constantly loading SPI when try completing order with more than 1 item. --- Model/Order/HydrateOrderFromQuote.php | 2 +- Service/ExpressPay/QuoteConverter.php | 89 ++++++++----------- view/frontend/web/js/model/fastlane.js | 5 +- view/frontend/web/js/model/spi.js | 8 +- view/frontend/web/js/view/bold-express-pay.js | 2 +- 5 files changed, 48 insertions(+), 58 deletions(-) diff --git a/Model/Order/HydrateOrderFromQuote.php b/Model/Order/HydrateOrderFromQuote.php index 1334563..21abcf7 100644 --- a/Model/Order/HydrateOrderFromQuote.php +++ b/Model/Order/HydrateOrderFromQuote.php @@ -121,7 +121,7 @@ public function hydrate(CartInterface $quote, string $publicOrderId): void 'fees' => $fees, 'shipping_line' => [ 'rate_name' => $shippingDescription ?? '', - 'cost' => $this->convertToCents($totals['shipping']['value']), + 'cost' => $this->convertToCents((float)$shippingTotalNoTax), ], 'totals' => [ 'sub_total' => $this->convertToCents((float)$subtotal), diff --git a/Service/ExpressPay/QuoteConverter.php b/Service/ExpressPay/QuoteConverter.php index 85da882..cae021c 100644 --- a/Service/ExpressPay/QuoteConverter.php +++ b/Service/ExpressPay/QuoteConverter.php @@ -35,11 +35,10 @@ class QuoteConverter * @var ScopeConfigInterface */ private $scopeConfig; + /** - * @var bool + * @param ScopeConfigInterface $scopeConfig */ - private $areTotalsCollected = false; - public function __construct(ScopeConfigInterface $scopeConfig) { $this->scopeConfig = $scopeConfig; @@ -50,6 +49,7 @@ public function __construct(ScopeConfigInterface $scopeConfig) */ public function convertFullQuote(Quote $quote, string $gatewayId): array { + $quote->collectTotals(); return array_merge_recursive( $this->convertGatewayIdentifier($gatewayId), $this->convertLocale($quote), @@ -65,17 +65,17 @@ public function convertFullQuote(Quote $quote, string $gatewayId): array /** * @return array */ - public function convertGatewayIdentifier(string $gatewayId): array + private function convertGatewayIdentifier(string $gatewayId): array { return [ - 'gateway_id' => $gatewayId + 'gateway_id' => $gatewayId, ]; } /** * @return array> */ - public function convertLocale(Quote $quote): array + private function convertLocale(Quote $quote): array { /** @var string|null $locale */ $locale = $this->scopeConfig->getValue( @@ -86,15 +86,15 @@ public function convertLocale(Quote $quote): array return [ 'order_data' => [ - 'locale' => str_replace('_', '-', $locale ?? '') - ] + 'locale' => str_replace('_', '-', $locale ?? ''), + ], ]; } /** * @return array>> */ - public function convertCustomer(Quote $quote): array + private function convertCustomer(Quote $quote): array { $billingAddress = $quote->getBillingAddress(); @@ -107,16 +107,16 @@ public function convertCustomer(Quote $quote): array 'customer' => [ 'first_name' => $billingAddress->getFirstname() ?? '', 'last_name' => $billingAddress->getLastname() ?? '', - 'email' => $billingAddress->getEmail() ?? '' - ] - ] + 'email' => $billingAddress->getEmail() ?? '', + ], + ], ]; } /** * @return array|string>|string>>> */ - public function convertShippingInformation(Quote $quote, bool $includeAddress = true): array + private function convertShippingInformation(Quote $quote, bool $includeAddress = true): array { $shippingAddress = $quote->getShippingAddress(); @@ -155,13 +155,13 @@ static function (Rate $rate) use ($currencyCode): array { 'type' => 'SHIPPING', 'amount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)$rate->getPrice(), 2) - ] + 'value' => number_format((float)$rate->getPrice(), 2), + ], ]; }, $shippingRates - ) - ] + ), + ], ]; $hasRequiredAddressData = ($shippingAddress->getCity() && $shippingAddress->getCountryId()); @@ -173,7 +173,7 @@ static function (Rate $rate) use ($currencyCode): array { 'city' => $shippingAddress->getCity() ?? '', 'country_code' => $shippingAddress->getCountryId() ?? '', 'postal_code' => $shippingAddress->getPostcode() ?? '', - 'state' => $shippingAddress->getRegion() ?? '' + 'state' => $shippingAddress->getRegion() ?? '', ]; } @@ -184,7 +184,7 @@ static function (Rate $rate) use ($currencyCode): array { 'type' => 'SHIPPING', 'amount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)$shippingAddress->getShippingAmount(), 2) + 'value' => number_format((float)$shippingAddress->getShippingAmount(), 2), ], ]; } @@ -195,7 +195,7 @@ static function (Rate $rate) use ($currencyCode): array { /** * @return array|bool|int|string>|string>>> */ - public function convertQuoteItems(Quote $quote): array + private function convertQuoteItems(Quote $quote): array { $quoteItems = $quote->getItems(); @@ -213,7 +213,7 @@ static function (CartItemInterface $cartItem) use ($currencyCode): array { 'sku' => $cartItem->getSku() ?? '', 'unit_amount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)$cartItem->getPrice(), 2) + 'value' => number_format((float)$cartItem->getPrice(), 2), ], 'quantity' => (int)(ceil($cartItem->getQty()) ?: $cartItem->getQty()), 'is_shipping_required' => !in_array( @@ -241,9 +241,9 @@ static function (CartItemInterface $cartItem) { ) ), 2 - ) - ] - ] + ), + ], + ], ]; $this->convertCustomTotals($quote, $convertedQuote); @@ -254,39 +254,32 @@ static function (CartItemInterface $cartItem) { /** * @return array>> */ - public function convertTotal(Quote $quote): array + private function convertTotal(Quote $quote): array { $currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : ''; - - if (!$this->areTotalsCollected) { - $quote->collectTotals(); // Ensure that we have the correct grand total for the quote - - $this->areTotalsCollected = true; - } - return [ 'order_data' => [ 'amount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)$quote->getGrandTotal(), 2) - ] - ] + 'value' => number_format((float)$quote->getGrandTotal(), 2), + ], + ], ]; } /** * @return array>> */ - public function convertTaxes(Quote $quote): array + private function convertTaxes(Quote $quote): array { $currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : ''; $convertedQuote = [ 'order_data' => [ 'tax_total' => [ 'currency_code' => $currencyCode ?? '', - 'value' => '' - ] - ] + 'value' => '', + ], + ], ]; if ($quote->getIsVirtual()) { @@ -316,7 +309,7 @@ static function (Item $item): float { /** * @return array>> */ - public function convertDiscount(Quote $quote): array + private function convertDiscount(Quote $quote): array { $currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : ''; @@ -324,9 +317,9 @@ public function convertDiscount(Quote $quote): array 'order_data' => [ 'discount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)($quote->getSubtotal() - $quote->getSubtotalWithDiscount()), 2) - ] - ] + 'value' => number_format((float)($quote->getSubtotal() - $quote->getSubtotalWithDiscount()), 2), + ], + ], ]; } @@ -337,12 +330,6 @@ public function convertDiscount(Quote $quote): array */ private function convertCustomTotals(Quote $quote, array &$convertedQuote): void { - if (!$this->areTotalsCollected) { - $quote->collectTotals(); - - $this->areTotalsCollected = true; - } - $currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : ''; $excludedTotals = ['subtotal', 'shipping', 'tax', 'grand_total']; $customTotals = array_filter( @@ -376,10 +363,10 @@ static function (Total $total) use ($currencyCode, &$customTotalsValue): ?array 'sku' => $total->getCode() ?? '', 'unit_amount' => [ 'currency_code' => $currencyCode ?? '', - 'value' => number_format((float)$value, 2) + 'value' => number_format((float)$value, 2), ], 'quantity' => 1, - 'is_shipping_required' => false + 'is_shipping_required' => false, ]; }, array_values($customTotals) diff --git a/view/frontend/web/js/model/fastlane.js b/view/frontend/web/js/model/fastlane.js index 37fe576..2aca1da 100644 --- a/view/frontend/web/js/model/fastlane.js +++ b/view/frontend/web/js/model/fastlane.js @@ -1,9 +1,8 @@ define([ 'ko', - 'Bold_CheckoutPaymentBooster/js/model/spi', + 'prototype', ], function ( ko, - spi, ) { 'use strict'; @@ -158,7 +157,7 @@ define([ if (gatewayData.is_test_mode) { debugMode = '&debug=true'; } - if (!require.defined('bold_paypal_fastlane')){ + if (!require.defined('bold_paypal_fastlane')) { require.config({ paths: { bold_paypal_fastlane: `https://www.paypal.com/sdk/js?client-id=${gatewayData.client_id}&components=fastlane${debugMode}`, diff --git a/view/frontend/web/js/model/spi.js b/view/frontend/web/js/model/spi.js index 558c6ce..f05fd86 100644 --- a/view/frontend/web/js/model/spi.js +++ b/view/frontend/web/js/model/spi.js @@ -14,7 +14,7 @@ define([ 'Magento_Customer/js/customer-data', 'mage/storage', 'Bold_CheckoutPaymentBooster/js/model/fastlane', - 'prototype' + 'Magento_Checkout/js/model/full-screen-loader', ], function ( registry, convertMagentoAddress, @@ -30,7 +30,8 @@ define([ payloadExtender, customerData, storage, - fastlane + fastlane, + fullScreenLoader ) { 'use strict'; @@ -93,6 +94,7 @@ define([ return { payment_data: { id: walletPayResult[0] }}; } catch (e) { + fullScreenLoader.stopLoader(); throw 'Unable to create order'; } }, @@ -104,6 +106,7 @@ define([ try { await this.updateOrder(paymentData['order_id']); } catch (e) { + fullScreenLoader.stopLoader(); throw new Error(`Update Payment Order Error ${e.message}`); } }, @@ -126,6 +129,7 @@ define([ }); return {card: scaResult}; } + fullScreenLoader.stopLoader(); throw new Error('Unsupported payment type'); }.bind(this), 'onRequireOrderData' : async function (requirements) { diff --git a/view/frontend/web/js/view/bold-express-pay.js b/view/frontend/web/js/view/bold-express-pay.js index 243ae20..065e804 100644 --- a/view/frontend/web/js/view/bold-express-pay.js +++ b/view/frontend/web/js/view/bold-express-pay.js @@ -46,7 +46,7 @@ define([ * @private */ _setVisibility: function () { - const expressPayEnabled = window.checkoutConfig.bold.isExpressPayEnabled; + const expressPayEnabled = window.checkoutConfig.bold?.isExpressPayEnabled; const onShippingStep = window.location.hash === '#shipping'; this.isVisible(onShippingStep && expressPayEnabled); From 2ca2ed1c6005545db06dbe8ed1abd71d3f4f4d47 Mon Sep 17 00:00:00 2001 From: Nickolas Malovanets Date: Thu, 24 Oct 2024 14:31:56 +0300 Subject: [PATCH 2/2] Constantly loading SPI when try completing order with more than 1 item. --- Service/ExpressPay/QuoteConverter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Service/ExpressPay/QuoteConverter.php b/Service/ExpressPay/QuoteConverter.php index cae021c..adb95bb 100644 --- a/Service/ExpressPay/QuoteConverter.php +++ b/Service/ExpressPay/QuoteConverter.php @@ -49,6 +49,9 @@ public function __construct(ScopeConfigInterface $scopeConfig) */ public function convertFullQuote(Quote $quote, string $gatewayId): array { + if (!$quote->isVirtual()) { + $quote->getShippingAddress()->setCollectShippingRates(true); + } $quote->collectTotals(); return array_merge_recursive( $this->convertGatewayIdentifier($gatewayId), @@ -126,9 +129,6 @@ private function convertShippingInformation(Quote $quote, bool $includeAddress = $currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : ''; - $shippingAddress->setCollectShippingRates(true); - $shippingAddress->collectShippingRates(); - $usedRateCodes = []; /** @var Rate[] $shippingRates */ $shippingRates = array_values(array_filter(