Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHK-4297] - Add Digital Wallets to cart and product pages #169

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Api/Quote/GetQuoteInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\Api\Quote;

/**
* Hydrate Bold order from Magento quote.
*/
interface GetQuoteInterface
{
/**
* Gets Current Session Quote ID
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getQuote();
}
20 changes: 20 additions & 0 deletions Block/ShortcutButtons/ExpressPayShortcutButtons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\Block\ShortcutButtons;

use Magento\Catalog\Block\ShortcutInterface;
use Magento\Framework\View\Element\Template;

class ExpressPayShortcutButtons extends Template implements ShortcutInterface
{
public const BLOCK_ALIAS = 'shortcut.buttons.express_pay';

protected $_template = 'Bold_CheckoutPaymentBooster::express-pay.phtml';

public function getAlias(): string
{
return self::BLOCK_ALIAS;
}
}
57 changes: 57 additions & 0 deletions CustomerData/BoldCheckoutData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\CustomerData;

use Magento\Customer\CustomerData\SectionSourceInterface;
use Bold\CheckoutPaymentBooster\UI\PaymentBoosterConfigProvider;

class BoldCheckoutData implements SectionSourceInterface
{
/**
* @var PaymentBoosterConfigProvider
*/
private $paymentBoosterConfig;

public function __construct(PaymentBoosterConfigProvider $paymentBoosterConfig)
{
$this->paymentBoosterConfig = $paymentBoosterConfig;
}

/**
* @return array{
* epsAuthToken: string,
* configurationGroupLabel: string,
* epsUrl: string,
* epsStaticUrl: string,
* gatewayId: int,
* jwtToken: string,
* url: string,
* shopId: string,
* publicOrderId: string,
* countries: array{
* is_region_visible: bool,
* label: string,
* value: string
* },
* origin: string,
* epsUrl: string,
* shopUrl: string,
* shopName: string,
* isPhoneRequired: bool,
* isExpressPayEnabled: bool,
* isCartWalletPayEnabled: bool,
* paymentBooster: array{
* payment: object{
* method: string
* }
* }
* }
*/
public function getSectionData(): array
{
$boldConfig = $this->paymentBoosterConfig->getConfig();
return $boldConfig['bold'] ?? [];
}
}
3 changes: 0 additions & 3 deletions Model/CheckoutData.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public function __construct(
public function initCheckoutData()
{
$quote = $this->checkoutSession->getQuote();
if (!$quote->getId()) {
throw new Exception('Quote is not found');
}
if (!$this->isPaymentBoosterAvailable->isAvailable()) {
return;
}
Expand Down
32 changes: 32 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Config
private const PATH_CONFIGURATION_GROUP_LABEL = 'checkout/bold_checkout_payment_booster/configuration_group_label';
private const PATH_BOLD_BOOSTER_FLOW_ID = 'checkout/bold_checkout_payment_booster/bold_booster_flow_id';
private const PATH_IS_EXPRESS_PAY_ENABLED = 'checkout/bold_checkout_payment_booster/is_express_pay_enabled';
private const PATH_IS_CART_WALLET_PAY_ENABLED = 'checkout/bold_checkout_payment_booster/is_cart_wallet_pay_enabled';
private const PATH_IS_PRODUCT_WALLET_PAY_ENABLED = 'checkout/bold_checkout_payment_booster/is_product_wallet_pay_enabled';

/**
* @var ScopeConfigInterface
Expand Down Expand Up @@ -316,5 +318,35 @@ public function isExpressPayEnabled(int $websiteId): bool
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
}

/**
* Check if Wallet Express Pay buttons are enabled On the cart and mini cart pages.
*
* @param int $websiteId
* @return bool
*/
public function isCartWalletPayEnabled(int $websiteId): bool
{
return $this->scopeConfig->isSetFlag(
self::PATH_IS_CART_WALLET_PAY_ENABLED,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
}

/**
* Check if Wallet Express Pay buttons are enabled on the product pages.
*
* @param int $websiteId
* @return bool
*/
public function isProductWalletPayEnabled(int $websiteId): bool
{
return $this->scopeConfig->isSetFlag(
self::PATH_IS_PRODUCT_WALLET_PAY_ENABLED,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
}
}
4 changes: 2 additions & 2 deletions Model/InitOrderFromQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public function init(CartInterface $quote): array
$body = [
'flow_id' => $flowId,
'order_type' => 'simple_order',
'cart_id' => $quote->getId(),
'cart_id' => $quote->getId() ?? '',
];
$orderData = $this->client->post(
(int)$quote->getStore()->getWebsiteId(),
(int)$websiteId,
self::INIT_SIMPLE_ORDER_URI,
$body
);
Expand Down
50 changes: 0 additions & 50 deletions Observer/Checkout/InitializeBoldOrderObserver.php

This file was deleted.

60 changes: 60 additions & 0 deletions Observer/Product/ExpressPayBeforeAddToCartObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\Observer\Product;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Checkout\Model\Session;
use Magento\Quote\Api\CartRepositoryInterface;

/**
* Observer for `controller_action_predispatch_checkout_cart_add` event
*
* @see \Magento\Framework\App\FrontController::dispatchPreDispatchEvents
* @see \Magento\Checkout\Controller\Cart\Add::execute
*/
class ExpressPayBeforeAddToCartObserver implements ObserverInterface
{
/**
* @var Session
*/
protected $checkoutSession;

/**
* @var CartRepositoryInterface
*/
protected $cartRepository;

public function __construct(Session $checkoutSession, CartRepositoryInterface $cartRepository)
{
$this->checkoutSession = $checkoutSession;
$this->cartRepository = $cartRepository;
}

/**
* Clear the cart before checking out with Express Pay from the product detail page
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer): void
{
$request = $observer->getEvent()->getRequest();
$isExpressPayOrder = $request->getParam('source') === 'expresspay';

$quote = $this->checkoutSession->getQuote();

if (!$isExpressPayOrder) {
return;
}
$this->checkoutSession->setCheckoutState(true);
$quote->removeAllItems();
$quote->setTotalsCollectedFlag(false);
$this->checkoutSession->clearQuote();

$this->cartRepository->save($quote);
$this->checkoutSession->setQuoteId($quote->getId());
}
}
73 changes: 73 additions & 0 deletions Observer/ShortcutButtons/AddExpressPayButtonsObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Bold\CheckoutPaymentBooster\Observer\ShortcutButtons;

use Bold\CheckoutPaymentBooster\Block\ShortcutButtons\ExpressPayShortcutButtons;
use Bold\CheckoutPaymentBooster\ViewModel\ExpressPayFactory;
use Bold\CheckoutPaymentBooster\UI\PaymentBoosterConfigProvider;
use Magento\Catalog\Block\ShortcutButtons;
use Magento\Framework\Event;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

/**
* Observes the `shortcut_buttons_container` event
*
* @see ShortcutButtons::_beforeToHtml
*/
class AddExpressPayButtonsObserver implements ObserverInterface
{
/**
* @var ExpressPayFactory
*/
private $expressPayFactory;

public function __construct(ExpressPayFactory $expressPayFactory)
{
$this->expressPayFactory = $expressPayFactory;
}

public function execute(Observer $observer): void
{
$event = $observer->getEvent();
/** @var ShortcutButtons $container */
$container = $event->getData('container');

$layout = $container->getLayout();

if ($layout->getBlock(ExpressPayShortcutButtons::BLOCK_ALIAS) !== false) {
return;
}

/** @var ExpressPayShortcutButtons $expressPayShortcutButtons */
$expressPayShortcutButtons = $layout->createBlock(
ExpressPayShortcutButtons::class,
'',
[
'data' => [
'express_pay_view_model' => $this->expressPayFactory->create(),
'render_page_source' => $this->getPageType($observer->getEvent())
]
]
);

$container->addShortcut($expressPayShortcutButtons);
}

/**
* @param Event $event
* @return string
*/
private function getPageType(Event $event): string
{
if ($event->getIsCatalogProduct()) {
return PaymentBoosterConfigProvider::PAGE_SOURCE_PRODUCT;
}
if ($event->getIsShoppingCart()) {
return PaymentBoosterConfigProvider::PAGE_SOURCE_CART;
}
return PaymentBoosterConfigProvider::PAGE_SOURCE_MINICART;
}
}
Loading