Skip to content

Commit

Permalink
Revert changes made to quote conversion logic in PR #101 (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephLeedy authored Oct 25, 2024
1 parent bde752b commit 6b4f8e8
Showing 1 changed file with 54 additions and 41 deletions.
95 changes: 54 additions & 41 deletions Service/ExpressPay/QuoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class QuoteConverter
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @param ScopeConfigInterface $scopeConfig
* @var bool
*/
private $areTotalsCollected = false;

public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
Expand All @@ -49,10 +50,6 @@ 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 @@ -68,17 +65,17 @@ public function convertFullQuote(Quote $quote, string $gatewayId): array
/**
* @return array<string, string>
*/
private function convertGatewayIdentifier(string $gatewayId): array
public function convertGatewayIdentifier(string $gatewayId): array
{
return [
'gateway_id' => $gatewayId,
'gateway_id' => $gatewayId
];
}

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

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

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

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

Expand All @@ -129,6 +126,9 @@ 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(
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>>>
*/
private function convertQuoteItems(Quote $quote): array
public 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,32 +254,39 @@ static function (CartItemInterface $cartItem) {
/**
* @return array<string, array<string, array<string, string>>>
*/
private function convertTotal(Quote $quote): array
public 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>>>
*/
private function convertTaxes(Quote $quote): array
public 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 @@ -309,17 +316,17 @@ static function (Item $item): float {
/**
* @return array<string, array<string, array<string, string>>>
*/
private function convertDiscount(Quote $quote): array
public 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 @@ -330,6 +337,12 @@ private 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 @@ -363,10 +376,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

0 comments on commit 6b4f8e8

Please sign in to comment.