/**
 *
 */
package com.braintree.controllers.handler;


import static com.braintree.constants.BraintreeConstants.US_BANK_ACCOUNT;
import static com.braintree.constants.Braintreeb2baddonWebConstants.CREDIT_MESSAGE_COMPONENT;
import static com.braintree.constants.Braintreeb2baddonWebConstants.IS_ANONYMOUS_USER;
import static com.braintree.constants.Braintreeb2baddonWebConstants.IS_CREDIT_MESSAGE_ENABLED;
import static com.braintree.constants.Braintreeb2baddonWebConstants.PAYMENT_INFOS;
import static com.braintree.constants.Braintreeb2baddonWebConstants.BRAINTREE_CHECKOUT_DATA;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.CLIENT_TOKEN;
import static com.braintree.constants.Braintreeb2baddonWebConstants.BRAINTREE_CONFIG_DATA;
import com.braintree.facade.BraintreeConfigurationFacade;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.Views.Pages.Checkout.CheckoutConfirmationPage;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.Views.Pages.Checkout.ReplenishmentCheckoutConfirmationPage;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.Views.Pages.MultiStepCheckout.CheckoutSummaryPage;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.Views.Pages.MultiStepCheckout.FallbackPage;
import static com.braintree.controllers.Braintreeb2baddonControllerConstants.Views.Pages.MultiStepCheckout.SilentOrderPostPage;
import static org.apache.commons.lang.StringUtils.isNotBlank;

import com.braintree.configuration.BrainTreeConfigurationListener;
import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.constants.BraintreeControllerConstants;
import com.braintree.controllers.Braintreeb2baddonControllerConstants;
import com.braintree.enums.BraintreePageType;
import com.braintree.facade.BrainTreeB2BOrderFacade;
import com.braintree.facade.BrainTreeSetUpPasswordFacade;
import com.braintree.facade.BrainTreeUserFacade;
import com.braintree.facade.impl.DefaultBrainTreeCheckoutFacade;
import com.braintree.facade.impl.DefaultBrainTreePaymentFacade;
import com.braintree.hybris.data.BrainTreePaymentInfoData;
import com.braintree.hybris.data.BraintreeLocalPaymentMethodData;
import com.braintree.hybris.data.BraintreeUserData;
import com.braintree.hybris.data.BraintreeCheckoutData;
import com.braintree.model.PayPalCreditMessageComponentB2BModel;
import com.braintree.util.BrainTreeUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.braintree.facade.BrainTreeStylesConfigurationFacade;
import de.hybris.platform.acceleratorstorefrontcommons.controllers.util.GlobalMessages;
import de.hybris.platform.addonsupport.interceptors.BeforeViewHandlerAdaptee;
import de.hybris.platform.b2bacceleratorfacades.order.data.ScheduledCartData;
import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;
import de.hybris.platform.cms2.servicelayer.services.CMSComponentService;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.hybris.platform.commercefacades.i18n.I18NFacade;
import de.hybris.platform.commercefacades.order.data.CCPaymentInfoData;
import com.braintree.hybris.data.PaymentMethodsLimitationData;
import de.hybris.platform.commercefacades.order.data.OrderData;
import de.hybris.platform.core.Registry;
import de.hybris.platform.servicelayer.session.SessionService;
import de.hybris.platform.util.config.ConfigIntf;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;


public class BrainTreeBeforeViewHandler implements BeforeViewHandlerAdaptee {

    private static final Logger LOG = Logger.getLogger(BrainTreeBeforeViewHandler.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private static final String CART_PAGE = "pages/cart/cartPage";
    private static final String BRAIN_TREE_PAYMENT_DATA = "brainTreePaymentInfoData";
    private static final String ORDER_DATA = "orderData";
    private static final String B2C_ADD_TO_CART_POPUP_PAGE = "fragments/cart/addToCartPopup";
    private static final String B2C_CART_POPUP_PAGE = "fragments/cart/cartPopup";
    private static final String ACCOUNT_LAYOUT_PAGE = "pages/account/accountLayoutPage";
    private static final String PAYPAL_BUTTON_CONFIG = "payPalButtonConfig";
    private static final String GOOGLE_PAY_BUTTON_CONFIG = "googlePayButtonConfig";
    private static final String APPLE_PAY_BUTTON_CONFIG = "applePayButtonConfig";
    private static final String B2C_CHECKOUT_CONFIRMATION_LAYOUT_PAGE = "pages/checkout/checkoutConfirmationLayoutPage";
    private static final String B2C_CHECKOUT_DELIVERY_ADDRESS_PAGE = "pages/checkout/multi/addEditDeliveryAddressPage";
    private static final String IS_STORE_IN_VAULT = "isStoreInVault";
    private static final String IS_REPLENISHMENT_AVAILABLE = "isReplenishmentAvailable";
    private static final String ARE_ALL_PAYMENTS_ON_LIMIT = "areAllPaymentsOnLimit";
    private static final String LPM_IDS = "lpmids";
    private static final String USER_DATA = "userData";
    private static final String REGIONS = "regions";
    private static final String CURRENT_DATE = "currentDate";
    private static final String US = "US";
    private static final String MERCHANT_NAME = "merchantName";
    private static final String B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN = "showWarningMessageAboutReplenishmentDropIn";
    private static final String BRAINTREE_GENERAL_ERROR_SHIPPING_ADDRESS = "braintree.general.error.shippingAddress";
    private static final String SHOULD_SET_UP_PASSWORD_FORM_DISPLAYED ="shouldSetUpPasswordFormDisplayed";
    private static final String ACH_BILLING_ADDRESSES = "achBillingAddresses";

    @Resource(name = "brainTreeCheckoutFacade")
    private DefaultBrainTreeCheckoutFacade brainTreeCheckoutFacade;
    @Resource(name = "brainTreeSetUpPasswordFacade")
    private BrainTreeSetUpPasswordFacade brainTreeSetUpPasswordFacade;
    @Resource(name = "defaultBraintreeConfigurationFacade")
    private BraintreeConfigurationFacade brainTreeConfigFacade;
    @Resource(name = "brainTreePaymentFacadeImpl")
    private DefaultBrainTreePaymentFacade brainTreePaymentFacade;
    @Resource(name = "brainTreeUserFacade")
    private BrainTreeUserFacade brainTreeUserFacade;
    @Resource(name = "brainTreeConfigService")
    private BrainTreeConfigService brainTreeConfigService;
    @Resource(name = "cmsComponentService")
    private CMSComponentService cmsComponentService;
    @Resource(name = "i18NFacade")
    private I18NFacade i18NFacade;
    @Resource(name = "brainTreeB2BOrderFacade")
    private BrainTreeB2BOrderFacade brainTreeB2BOrderFacade;

    @Autowired
    private SessionService sessionService;
    @Resource
    private BrainTreeStylesConfigurationFacade brainTreeStylesConfigurationFacade;

    private ConfigIntf.ConfigChangeListener configurationChangeListener;

    @Override
    public String beforeView(final HttpServletRequest request, final HttpServletResponse response, final ModelMap model,
        final String viewName) throws Exception {

        if ((!B2C_CART_POPUP_PAGE.equals(viewName) && !B2C_ADD_TO_CART_POPUP_PAGE.equals(viewName))
            && !isPaymentMethodsAvailable(viewName)) {
            return viewName;
        }
        model.addAttribute(SHOULD_SET_UP_PASSWORD_FORM_DISPLAYED,brainTreeSetUpPasswordFacade.isSetUpPasswordForm());
        return handleBrainTreeCheckoutScenario(model, viewName);

    }

    private String handleBrainTreeCheckoutScenario(final ModelMap model, final String viewName)
        throws CMSItemNotFoundException, JsonProcessingException {

        if (configurationChangeListener == null) {
            registerConfigChangeLister();
        }
        model.addAttribute(BRAINTREE_CONFIG_DATA, brainTreeConfigFacade.getBraintreePluginConfigurationData());

        if (CART_PAGE.equals(viewName)) {
            fillPaymentMethodsInfo(model, BraintreePageType.CART);
            model.addAttribute(IS_ANONYMOUS_USER, brainTreeUserFacade.isAnonymousUser());
            brainTreeStylesConfigurationFacade.getPayPalStyleConfiguration(BraintreePageType.CART)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(PAYPAL_BUTTON_CONFIG, jsonConfig));
            brainTreeStylesConfigurationFacade.getGooglePayStyleConfiguration(BraintreePageType.CART)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(GOOGLE_PAY_BUTTON_CONFIG, jsonConfig));
            brainTreeStylesConfigurationFacade.getApplePayStyleConfigurationFor(BraintreePageType.CART)
                    .ifPresent(config -> model.addAttribute(APPLE_PAY_BUTTON_CONFIG, config));
            model.addAttribute(B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN,
                brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.SilentOrderPostPage.equals(viewName)) {
            fillPaymentMethodsInfo(model, BraintreePageType.MARK);
            model.addAttribute(ACH_BILLING_ADDRESSES, brainTreeUserFacade.getAddressBook().stream()
                    .filter(address -> address.getCountry().getIsocode().equalsIgnoreCase("US"))
                    .collect(Collectors.toList()));
            brainTreeStylesConfigurationFacade.getPayPalStyleConfiguration(BraintreePageType.MARK)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(PAYPAL_BUTTON_CONFIG, jsonConfig));
            brainTreeStylesConfigurationFacade.getGooglePayStyleConfiguration(BraintreePageType.MARK)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(GOOGLE_PAY_BUTTON_CONFIG, jsonConfig));
            brainTreeStylesConfigurationFacade.getApplePayStyleConfigurationFor(BraintreePageType.MARK)
                    .ifPresent(config -> model.addAttribute(APPLE_PAY_BUTTON_CONFIG, config));
            model.addAttribute(B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN,
                brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
            if (brainTreeConfigService.isLocalPaymentsEnabled()) {
                BraintreeUserData userData = brainTreeUserFacade.getUserData();
                model.addAttribute(USER_DATA, userData);
            }
            model.addAttribute(REGIONS, i18NFacade.getRegionsForCountryIso(US));
            model.addAttribute(CURRENT_DATE, new SimpleDateFormat("MM-dd-yyyy").format(new Date()));
            return SilentOrderPostPage;
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.CheckoutSummaryPage.equals(viewName)) {
            final BrainTreePaymentInfoData brainTreePaymentInfoData = brainTreePaymentFacade
                .getBrainTreePaymentInfoData();

            if (brainTreePaymentInfoData != null) {
                model.addAttribute(BRAIN_TREE_PAYMENT_DATA, brainTreePaymentInfoData);
                if (brainTreePaymentInfoData.getPaymentType().equals(US_BANK_ACCOUNT)) {
                    model.addAttribute(MERCHANT_NAME, brainTreeConfigService.getUsBankAccountMerchantName());
                }
            }

            model.addAttribute(IS_REPLENISHMENT_AVAILABLE, brainTreeConfigService.isStoreInVault());
            model.addAttribute(IS_STORE_IN_VAULT, brainTreeConfigService.isStoreInVault());
            return CheckoutSummaryPage;
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.CheckoutConfirmationPage
            .equals(viewName)) {
            final String orderCode = ((OrderData) model.get(ORDER_DATA)).getCode();
            if (isNotBlank(orderCode)) {
                final BrainTreePaymentInfoData brainTreePaymentInfoData = brainTreePaymentFacade
                    .getBrainTreePaymentInfoData(orderCode);
                if (brainTreePaymentInfoData != null) {
                    model.addAttribute(BRAIN_TREE_PAYMENT_DATA, brainTreePaymentInfoData);
                }
            }
            return CheckoutConfirmationPage;
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.ReplenishmentCheckoutConfirmationPage
            .equals(viewName)) {
            final String cartCode = ((ScheduledCartData) model.get(ORDER_DATA)).getCode();
            if (isNotBlank(cartCode)) {
                model.addAttribute(BRAIN_TREE_PAYMENT_DATA,
                    brainTreePaymentFacade.getBrainTreePaymentInfoDataByCart(cartCode));
            }
            return ReplenishmentCheckoutConfirmationPage;
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.AccountLayoutPage.equals(viewName)) {
            // add BRAIN_TREE_PAYMENT_DATA for order view page
            if (model.get(ORDER_DATA) instanceof ScheduledCartData) {
                final String cartCode = ((ScheduledCartData) model.get(ORDER_DATA)).getCode();
                if (isNotBlank(cartCode)) {
                    model.addAttribute(BRAIN_TREE_PAYMENT_DATA,
                        brainTreePaymentFacade.getBrainTreePaymentInfoDataByCart(cartCode));
                }
            } else {
                final OrderData orderData = (OrderData) model.get(ORDER_DATA);
                if (orderData != null && isNotBlank(orderData.getCode())) {
                    final String orderCode = orderData.getCode();
                    model.addAttribute(BRAIN_TREE_PAYMENT_DATA,
                        brainTreePaymentFacade.getBrainTreePaymentInfoData(orderCode));
                    return viewName;
                }
            }

            if (!isPaymentMethodsAvailable(viewName)) {
                return viewName;
            }
            model.addAttribute(BRAINTREE_CHECKOUT_DATA, brainTreeCheckoutFacade.getBraintreeCheckoutData(BraintreePageType.ACCOUNT));
            model.addAttribute(PAYMENT_INFOS, brainTreeUserFacade.getBrainTreeCCPaymentInfos(true));
            model.addAttribute(B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN,
                brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
            brainTreeStylesConfigurationFacade.getPayPalStyleConfiguration(BraintreePageType.ACCOUNT)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(PAYPAL_BUTTON_CONFIG, jsonConfig));
            brainTreeStylesConfigurationFacade.getGooglePayStyleConfiguration(BraintreePageType.ACCOUNT)
                    .map(this::serializeToJson)
                    .ifPresent(jsonConfig -> model.addAttribute(GOOGLE_PAY_BUTTON_CONFIG, jsonConfig));
            model.addAttribute(ARE_ALL_PAYMENTS_ON_LIMIT, areAllPaymentsOnLimit(brainTreeCheckoutFacade.getBraintreeCheckoutData(BraintreePageType.ACCOUNT).getPaymentMethodsLimitation(), brainTreeConfigService.isDropInEnabled()));
            return ACCOUNT_LAYOUT_PAGE;
        } else if (B2C_CHECKOUT_CONFIRMATION_LAYOUT_PAGE.equals(viewName)) {
            if (model.get(ORDER_DATA) instanceof ScheduledCartData) {
                final String cartCode = ((ScheduledCartData) model.get(ORDER_DATA)).getCode();
                if (isNotBlank(cartCode)) {
                    model.addAttribute(BRAIN_TREE_PAYMENT_DATA,
                        brainTreePaymentFacade.getBrainTreePaymentInfoDataByCart(cartCode));
                }
            } else {
                final String orderCode = ((OrderData) model.get(ORDER_DATA)).getCode();
                if (isNotBlank(orderCode)) {
                    model.addAttribute(BRAIN_TREE_PAYMENT_DATA,
                        brainTreePaymentFacade.getBrainTreePaymentInfoData(orderCode));
                }
            }
            return viewName;
        } else if (B2C_ADD_TO_CART_POPUP_PAGE.equals(viewName)) {
            if (isPaymentMethodsAvailable(viewName)) {
                fillPaymentMethodsInfo(model, BraintreePageType.MINICART);
                model.addAttribute(IS_ANONYMOUS_USER, brainTreeUserFacade.isAnonymousUser());
                brainTreeStylesConfigurationFacade.getPayPalStyleConfiguration(BraintreePageType.MINICART)
                        .map(this::serializeToJson)
                        .ifPresent(jsonConfig -> model.addAttribute(PAYPAL_BUTTON_CONFIG, jsonConfig));
                brainTreeStylesConfigurationFacade.getGooglePayStyleConfiguration(BraintreePageType.MINICART)
                        .map(this::serializeToJson)
                        .ifPresent(jsonConfig -> model.addAttribute(GOOGLE_PAY_BUTTON_CONFIG, jsonConfig));
                brainTreeStylesConfigurationFacade.getApplePayStyleConfigurationFor(BraintreePageType.MINICART)
                        .ifPresent(config -> model.addAttribute(APPLE_PAY_BUTTON_CONFIG, config));
                model.addAttribute(B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN,
                    brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
                return Braintreeb2baddonControllerConstants.Views.Fragments.Cart.AddToCartPopup;
            }
            return viewName;
        } else if (B2C_CART_POPUP_PAGE.equals(viewName)) {
            if (isPaymentMethodsAvailable(viewName)) {
                fillPaymentMethodsInfo(model, BraintreePageType.MINICART);
                model.addAttribute(IS_ANONYMOUS_USER, brainTreeUserFacade.isAnonymousUser());
                brainTreeStylesConfigurationFacade.getPayPalStyleConfiguration(BraintreePageType.MINICART)
                        .map(this::serializeToJson)
                        .ifPresent(jsonConfig -> model.addAttribute(PAYPAL_BUTTON_CONFIG, jsonConfig));
                brainTreeStylesConfigurationFacade.getGooglePayStyleConfiguration(BraintreePageType.MINICART)
                        .map(this::serializeToJson)
                        .ifPresent(jsonConfig -> model.addAttribute(GOOGLE_PAY_BUTTON_CONFIG, jsonConfig));
                brainTreeStylesConfigurationFacade.getApplePayStyleConfigurationFor(BraintreePageType.MINICART)
                        .ifPresent(config -> model.addAttribute(APPLE_PAY_BUTTON_CONFIG, config));
                model.addAttribute(B2B_SHOW_WARNING_MESSAGE_ON_EDIT_DROP_IN,
                    brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
                return Braintreeb2baddonControllerConstants.Views.Fragments.Cart.CartPopup;
            }
            return viewName;
        } else if (B2C_CHECKOUT_DELIVERY_ADDRESS_PAGE.equals(viewName)
            && sessionService.getAttribute(BRAINTREE_GENERAL_ERROR_SHIPPING_ADDRESS) != null) {
            model.addAttribute("accErrorMsgs", sessionService.getAttribute(BRAINTREE_GENERAL_ERROR_SHIPPING_ADDRESS));
            sessionService.removeAttribute(BRAINTREE_GENERAL_ERROR_SHIPPING_ADDRESS);
        } else if (BraintreeControllerConstants.Views.Pages.MultiStepCheckout.FallbackPage.equals(viewName)) {
            model.addAttribute(CLIENT_TOKEN, brainTreeCheckoutFacade.generateClientToken(Boolean.TRUE));
            return FallbackPage;
        }
        return viewName;
    }

    private void fillPaymentMethodsInfo(final ModelMap model, BraintreePageType pageType) {
        ObjectMapper om = new ObjectMapper();
        final BraintreeCheckoutData braintreeCheckoutData = brainTreeCheckoutFacade.getBraintreeCheckoutData(pageType);
        model.addAttribute(BRAINTREE_CHECKOUT_DATA, braintreeCheckoutData);
        List<CCPaymentInfoData> brainTreeCCPaymentInfos = brainTreeUserFacade.getBrainTreeCCPaymentInfos(true);
        model.addAttribute("payPalShouldBeSaved", !isPayPalAccountSaved(brainTreeCCPaymentInfos));
        model.addAttribute(PAYMENT_INFOS, brainTreeCCPaymentInfos);
        if (pageType.equals(BraintreePageType.MARK)) {
            List<String> lpmids = new ArrayList<>();
            for (BraintreeLocalPaymentMethodData lpm :
                braintreeCheckoutData.getLocalPaymentMethod().getLocalPaymentMethods()) {
                lpmids.add(lpm.getCode());
            }
            try {
                model.addAttribute(LPM_IDS, om.writeValueAsString(lpmids));
            } catch (IOException e) {
                LOG.error("Error during write value", e);
            }
        }
    }

    private boolean isPayPalAccountSaved(List<CCPaymentInfoData> savedPayments) {
        for (CCPaymentInfoData payment : savedPayments) {
            if (BrainTreeUtils.isPayPalPayment(payment.getSubscriptionId())) {
                return true;
            }
        }
        return false;
    }

    private boolean isPaymentMethodsAvailable(String viewName) {
        if (B2C_CART_POPUP_PAGE.equals(viewName) || B2C_ADD_TO_CART_POPUP_PAGE.equals(viewName)) {
            return isPaymentMethodsAvailableForCartPopup();
        } else if (ACCOUNT_LAYOUT_PAGE.equals(viewName)) {
            if (brainTreeConfigService.isDropInEnabled()) {
                return brainTreeConfigService.isPayPalEnabled() || brainTreeConfigService
                    .isHostedFieldEnabled();
            } else {
                return isPaymentMethodsAvailableForAccountLayout();
            }
        } else {
            return brainTreeConfigService.isOneOfPaymentMethodsEnabled();
        }
    }


    private boolean areAllPaymentsOnLimit(PaymentMethodsLimitationData limitationData, boolean isDropInEnabled) {
    return limitationData.isPayPalOnLimit()
            && limitationData.isGooglePayOnLimit()
            && limitationData.isCreditCardOnLimit()
            && limitationData.isUsBankAccountOnLimit()
            && limitationData.isVenmoOnLimit()
            && limitationData.isSRCOnLimit()
            && !isDropInEnabled;
    }

    private String serializeToJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            LOG.error("Error during serializing styles configuration object", e);
            return null;
        }
    }

    private boolean isPaymentMethodsAvailableForAccountLayout() {
        return brainTreeConfigService.isHostedFieldEnabled() || brainTreeConfigService.isSrcEnabled()
            || isAvailableAccountPaymentMethods();
    }

    private boolean isAvailableAccountPaymentMethods() {
        return brainTreeConfigService.isPayPalEnabled() || brainTreeConfigService.isVenmoEnabled()
                || brainTreeConfigService.isGooglePayEnabled();
    }

    private boolean isPaymentMethodsAvailableForCartPopup() {
        return brainTreeConfigService.isPayPalExpressEnabled() || brainTreeConfigService.isApplePayOnExpressCheckoutEnabled()
                || brainTreeConfigService.isGooglePayOnExpressCheckoutEnabled() || brainTreeConfigService.isPayLaterEnabled();
    }

    private void registerConfigChangeLister() {
        final ConfigIntf config = Registry.getMasterTenant().getConfig();
        configurationChangeListener = new BrainTreeConfigurationListener();
        config.registerConfigChangeListener(configurationChangeListener);
    }
}
