diff --git a/Block/ExpressCheckout/AbstractExpressCheckoutBlock.php b/Block/ExpressCheckout/AbstractExpressCheckoutBlock.php index 3a1a386..c353d13 100644 --- a/Block/ExpressCheckout/AbstractExpressCheckoutBlock.php +++ b/Block/ExpressCheckout/AbstractExpressCheckoutBlock.php @@ -46,6 +46,12 @@ abstract class AbstractExpressCheckoutBlock extends Template * @var string|null */ private ?string $state = null; + /** + * Memoized merchant-configured button style blob for the current request. + * + * @var string|null + */ + private ?string $buttonStyle = null; /** * @param ScopeResolverInterface $scopeResolver @@ -109,6 +115,18 @@ public function getUnavailableMessage(): string return (string)__('SeQura is not available for your account.'); } + /** + * The merchant-configured button style blob, forwarded verbatim to the checkout library. + * + * @return string|null + */ + public function getButtonStyle(): ?string + { + $this->resolveState(); + + return $this->buttonStyle; + } + /** * The storefront solicit URL the CDN-library button fetches (GET, raw HTML response). * @@ -161,7 +179,7 @@ private function resolveState(): string ? (string)$this->shippingResolver->getResolvableShippingCountry($quote) : ''; - $this->state = $this->availabilityEvaluator->evaluate( + $result = $this->availabilityEvaluator->evaluate( (string)$this->_storeManager->getStore()->getId(), $this->getExpressCheckoutPage(), $isLoggedIn, @@ -172,6 +190,9 @@ private function resolveState(): string $this->getCartCategoryIds($quote) ); + $this->state = $result['state']; + $this->buttonStyle = $result['buttonStyle']; + return $this->state; } catch (Exception $e) { Logger::logError('Checking Express Checkout availability failed: ' . $e->getMessage() . diff --git a/Block/ExpressCheckout/ProductPage.php b/Block/ExpressCheckout/ProductPage.php index 86e17d2..93626c3 100644 --- a/Block/ExpressCheckout/ProductPage.php +++ b/Block/ExpressCheckout/ProductPage.php @@ -58,6 +58,12 @@ class ProductPage extends Template * @var string|null */ private ?string $state = null; + /** + * Memoized merchant-configured button style blob for the current request. + * + * @var string|null + */ + private ?string $buttonStyle = null; /** * Memoized product for the current request (null also means "resolved to nothing"). * @@ -109,6 +115,18 @@ public function isAvailable(): bool return $this->resolveState() === AvailabilityEvaluator::STATE_BUTTON; } + /** + * The merchant-configured button style blob, forwarded verbatim to the checkout library. + * + * @return string|null + */ + public function getButtonStyle(): ?string + { + $this->resolveState(); + + return $this->buttonStyle; + } + /** * The viewed product's entity ID, for the JS controller. * @@ -171,7 +189,7 @@ private function resolveState(): string // Always the guest (customer-agnostic) evaluation: the page is full-page cached, so // the session is depersonalized during render and the HTML is shared by all shoppers. - $this->state = $this->availabilityEvaluator->evaluate( + $result = $this->availabilityEvaluator->evaluate( (string)$this->_storeManager->getStore()->getId(), ExpressCheckoutPage::product()->getPage(), false, @@ -182,6 +200,9 @@ private function resolveState(): string $this->getProductCategoryIds($product) ); + $this->state = $result['state']; + $this->buttonStyle = $result['buttonStyle']; + return $this->state; } catch (Exception $e) { Logger::logError('Checking Express Checkout availability for product failed: ' . $e->getMessage() . diff --git a/Model/ExpressCheckout/AvailabilityEvaluator.php b/Model/ExpressCheckout/AvailabilityEvaluator.php index d12bd62..415bacb 100644 --- a/Model/ExpressCheckout/AvailabilityEvaluator.php +++ b/Model/ExpressCheckout/AvailabilityEvaluator.php @@ -48,7 +48,7 @@ class AvailabilityEvaluator * @param string[] $productIds Product entity IDs in context. * @param string[] $categoryIds Category IDs in context. * - * @return string One of self::STATE_*. + * @return array{state: string, buttonStyle: string|null} State is one of self::STATE_*. */ public function evaluate( string $storeId, @@ -59,7 +59,7 @@ public function evaluate( string $ipAddress, array $productIds, array $categoryIds - ): string { + ): array { try { if ($isLoggedIn) { /** @var ExpressCheckoutAvailabilityResponse $response */ @@ -73,31 +73,38 @@ public function evaluate( $categoryIds ) ); - - $available = $response->isSuccessful() && !empty($response->toArray()['available']); - - return $available ? self::STATE_BUTTON : self::STATE_MESSAGE; + $unavailableState = self::STATE_MESSAGE; + } else { + /** @var GuestExpressCheckoutAvailabilityResponse $response */ + $response = CheckoutAPI::get()->expressCheckout($storeId)->isAvailableForGuest( + new GuestExpressCheckoutAvailabilityRequest( + $page, + $currency, + $ipAddress, + $productIds, + $categoryIds + ) + ); + $unavailableState = self::STATE_HIDDEN; } - /** @var GuestExpressCheckoutAvailabilityResponse $response */ - $response = CheckoutAPI::get()->expressCheckout($storeId)->isAvailableForGuest( - new GuestExpressCheckoutAvailabilityRequest( - $page, - $currency, - $ipAddress, - $productIds, - $categoryIds - ) - ); + $data = $response->toArray(); + $buttonStyle = $data['buttonStyle'] ?? null; - return ($response->isSuccessful() && !empty($response->toArray()['available'])) - ? self::STATE_BUTTON - : self::STATE_HIDDEN; + return [ + 'state' => ($response->isSuccessful() && !empty($data['available'])) + ? self::STATE_BUTTON + : $unavailableState, + 'buttonStyle' => is_string($buttonStyle) ? $buttonStyle : null, + ]; } catch (Exception $e) { Logger::logError('Checking Express Checkout availability failed: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString()); - return self::STATE_HIDDEN; + return [ + 'state' => self::STATE_HIDDEN, + 'buttonStyle' => null, + ]; } } } diff --git a/composer.json b/composer.json index 5adae92..dd0abe3 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "prefer-stable": true, "require": { "php": ">=7.4 <8.6", - "sequra/integration-core": "v5.5.0", + "sequra/integration-core": "v5.7.0", "ext-json": "*" }, "autoload": { diff --git a/view/frontend/templates/express/cart.phtml b/view/frontend/templates/express/cart.phtml index 2dc818e..eecd308 100644 --- a/view/frontend/templates/express/cart.phtml +++ b/view/frontend/templates/express/cart.phtml @@ -3,9 +3,13 @@ /** @var \Magento\Framework\Escaper $escaper */ if ($block->isAvailable()): + $buttonStyle = (string)$block->getButtonStyle(); ?>