Skip to content

Commit

Permalink
Merge pull request #101 from bold-commerce/CHK-6101
Browse files Browse the repository at this point in the history
Constantly loading SPI when try completing order with more than 1 item.
  • Loading branch information
NickolasMalovanets authored Oct 24, 2024
2 parents 11c5888 + 2ca2ed1 commit bde752b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Model/Order/HydrateOrderFromQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
95 changes: 41 additions & 54 deletions Service/ExpressPay/QuoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,6 +49,10 @@ 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),
$this->convertLocale($quote),
Expand All @@ -65,17 +68,17 @@ public function convertFullQuote(Quote $quote, string $gatewayId): array
/**
* @return array<string, string>
*/
public function convertGatewayIdentifier(string $gatewayId): array
private function convertGatewayIdentifier(string $gatewayId): array
{
return [
'gateway_id' => $gatewayId
'gateway_id' => $gatewayId,
];
}

/**
* @return array<string, array<string, string>>
*/
public function convertLocale(Quote $quote): array
private function convertLocale(Quote $quote): array
{
/** @var string|null $locale */
$locale = $this->scopeConfig->getValue(
Expand All @@ -86,15 +89,15 @@ public function convertLocale(Quote $quote): array

return [
'order_data' => [
'locale' => str_replace('_', '-', $locale ?? '')
]
'locale' => str_replace('_', '-', $locale ?? ''),
],
];
}

/**
* @return array<string, array<string, array<string, string>>>
*/
public function convertCustomer(Quote $quote): array
private function convertCustomer(Quote $quote): array
{
$billingAddress = $quote->getBillingAddress();

Expand All @@ -107,16 +110,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, array<string, array<array<string, array<string, string>|string>|string>>>
*/
public function convertShippingInformation(Quote $quote, bool $includeAddress = true): array
private function convertShippingInformation(Quote $quote, bool $includeAddress = true): array
{
$shippingAddress = $quote->getShippingAddress();

Expand All @@ -126,9 +129,6 @@ public 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(
Expand All @@ -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());
Expand All @@ -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() ?? '',
];
}

Expand All @@ -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),
],
];
}
Expand All @@ -195,7 +195,7 @@ static function (Rate $rate) use ($currencyCode): array {
/**
* @return array<string, array<string, array<array<string, array<string, string>|bool|int|string>|string>>>
*/
public function convertQuoteItems(Quote $quote): array
private function convertQuoteItems(Quote $quote): array
{
$quoteItems = $quote->getItems();

Expand All @@ -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(
Expand Down Expand Up @@ -241,9 +241,9 @@ static function (CartItemInterface $cartItem) {
)
),
2
)
]
]
),
],
],
];

$this->convertCustomTotals($quote, $convertedQuote);
Expand All @@ -254,39 +254,32 @@ static function (CartItemInterface $cartItem) {
/**
* @return array<string, array<string, array<string, string>>>
*/
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<string, array<string, array<string, string>>>
*/
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()) {
Expand Down Expand Up @@ -316,17 +309,17 @@ static function (Item $item): float {
/**
* @return array<string, array<string, array<string, string>>>
*/
public function convertDiscount(Quote $quote): array
private function convertDiscount(Quote $quote): array
{
$currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : '';

return [
'order_data' => [
'discount' => [
'currency_code' => $currencyCode ?? '',
'value' => number_format((float)($quote->getSubtotal() - $quote->getSubtotalWithDiscount()), 2)
]
]
'value' => number_format((float)($quote->getSubtotal() - $quote->getSubtotalWithDiscount()), 2),
],
],
];
}

Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions view/frontend/web/js/model/fastlane.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
define([
'ko',
'Bold_CheckoutPaymentBooster/js/model/spi',
'prototype',
], function (
ko,
spi,
) {
'use strict';

Expand Down Expand Up @@ -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}`,
Expand Down
8 changes: 6 additions & 2 deletions view/frontend/web/js/model/spi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +30,8 @@ define([
payloadExtender,
customerData,
storage,
fastlane
fastlane,
fullScreenLoader
) {
'use strict';

Expand Down Expand Up @@ -93,6 +94,7 @@ define([

return { payment_data: { id: walletPayResult[0] }};
} catch (e) {
fullScreenLoader.stopLoader();
throw 'Unable to create order';
}
},
Expand All @@ -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}`);
}
},
Expand All @@ -126,6 +129,7 @@ define([
});
return {card: scaResult};
}
fullScreenLoader.stopLoader();
throw new Error('Unsupported payment type');
}.bind(this),
'onRequireOrderData' : async function (requirements) {
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/web/js/view/bold-express-pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bde752b

Please sign in to comment.