Skip to content

Commit

Permalink
Fix braking issue with no available shipping options
Browse files Browse the repository at this point in the history
AD4CR22I-28
  • Loading branch information
Tamara committed Jan 31, 2025
1 parent 8d7c655 commit 83b08e8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public function getExpressCheckoutConfig(
$formattedHandlerIdentifier
);

if (array_key_exists('error', $config)) {
if ($config['error'] === 'ResolveCountryException') {
throw new ResolveCountryException($config['message']);
}

if ($config['error'] === 'ResolveShippingMethodException') {
throw new ResolveShippingMethodException($config['message']);
}
}

return new JsonResponse($config);
} catch (ResolveCountryException $e) {
return new JsonResponse([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

{% block page_checkout_cart_action_proceed %}
{{ parent() }}
{% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %}
{% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %}
{% if adyenFrontendData.expressCheckoutConfigurationAvailable %}
{% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %}
{% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %}
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% block page_product_detail_buy_container %}
{{ parent() }}
{% if buyable %}
{% if buyable and adyenFrontendData.expressCheckoutConfigurationAvailable %}
{% sw_include '@AdyenPaymentShopware6/storefront/component/adyencheckout.html.twig' %}
{% sw_include '@AdyenPaymentShopware6/storefront/component/checkout/expresscheckout.html.twig' %}
{% endif %}
Expand Down
85 changes: 47 additions & 38 deletions src/Service/ExpressCheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public function __construct(
* @param array $newShipping Optional new shipping method details.
* @param string $formattedHandlerIdentifier
* @return array The configuration for express checkout.
* @throws ResolveCountryException
* @throws ResolveShippingMethodException
*/
public function getExpressCheckoutConfig(
string $productId,
Expand All @@ -117,47 +115,58 @@ public function getExpressCheckoutConfig(
array $newShipping = [],
string $formattedHandlerIdentifier = ''
): array {
// Creating new cart
$cartData = $this->createCart(
$productId,
$quantity,
$salesChannelContext,
$newAddress,
$newShipping,
$formattedHandlerIdentifier
);
try {
$cartData = $this->createCart(
$productId,
$quantity,
$salesChannelContext,
$newAddress,
$newShipping,
$formattedHandlerIdentifier
);

/** @var Cart $cart */
$cart = $cartData['cart'];
/** @var Cart $cart */
$cart = $cartData['cart'];

$currency = $salesChannelContext->getCurrency()->getIsoCode();
$amountInMinorUnits = $this->currencyUtil->sanitize($cart->getPrice()->getTotalPrice(), $currency);
$currency = $salesChannelContext->getCurrency()->getIsoCode();
$amountInMinorUnits = $this->currencyUtil->sanitize($cart->getPrice()->getTotalPrice(), $currency);

// Available shipping methods for the given address
$shippingMethods = $this->getFormatedShippingMethods($cartData, $currency);
// Available shipping methods for the given address
$shippingMethods = $this->getFormatedShippingMethods($cartData, $currency);

if (empty($shippingMethods)) {
throw new ResolveShippingMethodException('No shipping method found!');
}
if (empty($shippingMethods)) {
throw new ResolveShippingMethodException('No shipping method found!');
}

// Available payment methods
$paymentMethods = $cartData['paymentMethods'];
// Available payment methods
$paymentMethods = $cartData['paymentMethods'];

// Delete temporary cart for product
if ($productId !== '-1') {
$this->cartService->deleteCart($cartData['updatedSalesChannelContext']);
}
// Delete temporary cart for product
if ($productId !== '-1') {
$this->cartService->deleteCart($cartData['updatedSalesChannelContext']);
}

return [
'currency' => $currency,
'amount' => $amountInMinorUnits,
'countryCode' => $this->getCountryCode(
$salesChannelContext->getCustomer(),
$salesChannelContext
),
'paymentMethodsResponse' => json_encode($paymentMethods),
'shippingMethodsResponse' => array_values($shippingMethods),
];
return [
'currency' => $currency,
'amount' => $amountInMinorUnits,
'countryCode' => $this->getCountryCode(
$salesChannelContext->getCustomer(),
$salesChannelContext
),
'paymentMethodsResponse' => json_encode($paymentMethods),
'shippingMethodsResponse' => array_values($shippingMethods),
];
} catch (ResolveCountryException $exception) {
return [
'error' => 'ResolveCountryException',
'message' => $exception->getMessage()
];
} catch (ResolveShippingMethodException $exception) {
return [
'error' => 'ResolveShippingMethodException',
'message' => $exception->getMessage()
];
}
}

/**
Expand Down Expand Up @@ -756,8 +765,8 @@ private function getFormatedShippingMethods(array $cartData, string $currency):

$cart = $this->cartService->recalculate($cart, $salesChannelWithCurrentShippingMethod);
/** @var Delivery $delivery */
$delivery =$cart->getDeliveries()->first();
$price = $delivery->getShippingCosts()->getTotalPrice();
$delivery = $cart->getDeliveries()->first();
$price = $delivery ? $delivery->getShippingCosts()->getTotalPrice() : 0;
$value = $this->currencyUtil->sanitize($price, $currency);

return [
Expand Down
38 changes: 27 additions & 11 deletions src/Subscriber/PaymentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ public function onShoppingCartLoaded(PageLoadedEvent $event)
$page->getCart()->getPrice()->getTotalPrice()
);

$expressCheckoutConfigurationAvailable = true;
$expressCheckoutConfiguration = $this->expressCheckoutService->getExpressCheckoutConfig(
'-1',
-1,
$salesChannelContext
);
if (array_key_exists('error', $expressCheckoutConfiguration)) {
$expressCheckoutConfigurationAvailable = false;
$expressCheckoutConfiguration = [];
}

$page->addExtension(
self::ADYEN_DATA_EXTENSION_ID,
new ArrayEntity(
Expand Down Expand Up @@ -342,12 +353,9 @@ public function onShoppingCartLoaded(PageLoadedEvent $event)
'userLoggedIn' => json_encode($userLoggedIn),
'affiliateCode' => $affiliateCode,
'campaignCode' => $campaignCode,
'expressCheckoutConfigurationAvailable' => $expressCheckoutConfigurationAvailable
],
$this->expressCheckoutService->getExpressCheckoutConfig(
'-1',
-1,
$salesChannelContext
)
$expressCheckoutConfiguration
)
)
);
Expand All @@ -371,6 +379,17 @@ public function onProductPageLoaded(PageLoadedEvent $event): void
$affiliateCode = $this->requestStack->getSession()->get(AffiliateTrackingListener::AFFILIATE_CODE_KEY);
$campaignCode = $this->requestStack->getSession()->get(AffiliateTrackingListener::CAMPAIGN_CODE_KEY);

$expressCheckoutConfigurationAvailable = true;
$expressCheckoutConfiguration = $this->expressCheckoutService->getExpressCheckoutConfig(
'-1',
-1,
$salesChannelContext
);
if (array_key_exists('error', $expressCheckoutConfiguration)) {
$expressCheckoutConfigurationAvailable = false;
$expressCheckoutConfiguration = [];
}

$page->addExtension(
self::ADYEN_DATA_EXTENSION_ID,
new ArrayEntity(
Expand Down Expand Up @@ -413,13 +432,10 @@ public function onProductPageLoaded(PageLoadedEvent $event): void
'googleMerchantId' => $this->configurationService
->getGooglePayMerchantId($salesChannelContext->getSalesChannelId()),
'gatewayMerchantId' => $this->configurationService
->getMerchantAccount($salesChannelContext->getSalesChannelId())
->getMerchantAccount($salesChannelContext->getSalesChannelId()),
'expressCheckoutConfigurationAvailable' => $expressCheckoutConfigurationAvailable
],
$this->expressCheckoutService->getExpressCheckoutConfig(
$productId,
1,
$salesChannelContext
)
$expressCheckoutConfiguration
)
)
);
Expand Down

0 comments on commit 83b08e8

Please sign in to comment.