diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.spec.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.spec.ts index dfadf77bca5..a529bd7369e 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.spec.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.spec.ts @@ -7,7 +7,6 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { Cart } from '@spartacus/cart/base/root'; import { Product } from '@spartacus/core'; -import { OpfActiveConfiguration } from '@spartacus/opf/base/root'; import { OpfPaymentErrorHandlerService } from '@spartacus/opf/payment/core'; import { OpfQuickBuyTransactionService } from '@spartacus/opf/quick-buy/core'; import { @@ -33,17 +32,6 @@ const mockCart: Cart = { code: '123', }; -const mockActiveConfiguration: OpfActiveConfiguration = { - digitalWalletQuickBuy: [ - { - merchantId: 'merchant.com.adyen.upscale.test', - provider: OpfQuickBuyProviderType.APPLE_PAY, - countryCode: 'US', - }, - { merchantId: 'merchant.test.example' }, - ], -}; - describe('ApplePayComponent', () => { let component: ApplePayComponent; let fixture: ComponentFixture; @@ -110,14 +98,17 @@ describe('ApplePayComponent', () => { provider: OpfQuickBuyProviderType.APPLE_PAY, countryCode: mockCountryCode, merchantId: 'merchant.com.adyen.upscale.test', + enabled: true, + }; + component.activeConfiguration = { + digitalWalletQuickBuy: [digitalWallet], }; - component.activeConfiguration = { digitalWalletQuickBuy: [digitalWallet] }; const mockObservable = of(true); mockApplePayService.isApplePaySupported.and.returnValue(mockObservable); fixture.detectChanges(); - expect(component.isApplePaySupported$).toBe(mockObservable); + expect(component.isApplePaySupported$).toEqual(mockObservable); }); it('should not initialize isApplePaySupported$ provider is not Apple pay', () => { @@ -125,13 +116,14 @@ describe('ApplePayComponent', () => { provider: OpfQuickBuyProviderType.GOOGLE_PAY, countryCode: mockCountryCode, merchantId: 'merchant.com.adyen.upscale.test', + enabled: true, + }; + component.activeConfiguration = { + digitalWalletQuickBuy: [digitalWallet], }; - component.activeConfiguration = { digitalWalletQuickBuy: [digitalWallet] }; - - const mockObservable = of(true); - mockApplePayService.isApplePaySupported.and.returnValue(mockObservable); fixture.detectChanges(); + expect(component.isApplePaySupported$).toBeUndefined(); expect(mockApplePayService.isApplePaySupported).not.toHaveBeenCalled(); }); @@ -139,21 +131,21 @@ describe('ApplePayComponent', () => { mockApplePayService.start.and.returnValue( of({ status: 1 }) ); + + const digitalWallet: OpfQuickBuyDigitalWallet = { + provider: OpfQuickBuyProviderType.APPLE_PAY, + countryCode: mockCountryCode, + merchantId: 'merchant.com.adyen.upscale.test', + enabled: true, + }; + component.activeConfiguration = { - digitalWalletQuickBuy: [ - { - provider: OpfQuickBuyProviderType.APPLE_PAY, - countryCode: mockCountryCode, - merchantId: 'merchant.com.adyen.upscale.test', - }, - ], + digitalWalletQuickBuy: [digitalWallet], }; - component.activeConfiguration = mockActiveConfiguration; + fixture.detectChanges(); component.initTransaction(); - expect( - mockOpfPaymentErrorHandlerService.handlePaymentError - ).not.toHaveBeenCalled(); + expect(mockApplePayService.start).toHaveBeenCalled(); }); }); diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.ts index 8e23008e6c5..11a7dd25690 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/apple-pay/apple-pay.component.ts @@ -31,7 +31,9 @@ import { ApplePayService } from './apple-pay.service'; imports: [NgIf, AsyncPipe], }) export class ApplePayComponent implements OnInit { - @Input() activeConfiguration: OpfActiveConfiguration; + @Input() activeConfiguration: + | OpfActiveConfiguration + | OpfActiveConfiguration[]; protected applePayService = inject(ApplePayService); protected currentProductService = inject(CurrentProductService); @@ -43,10 +45,15 @@ export class ApplePayComponent implements OnInit { applePayDigitalWallet?: OpfQuickBuyDigitalWallet; ngOnInit(): void { - this.applePayDigitalWallet = - this.activeConfiguration?.digitalWalletQuickBuy?.find( + const activeConfigurations = Array.isArray(this.activeConfiguration) + ? this.activeConfiguration + : [this.activeConfiguration]; + this.applePayDigitalWallet = activeConfigurations + ?.flatMap((config) => config.digitalWalletQuickBuy || []) + .find( (digitalWallet) => - digitalWallet.provider === OpfQuickBuyProviderType.APPLE_PAY + digitalWallet.provider === OpfQuickBuyProviderType.APPLE_PAY && + digitalWallet.enabled ); if ( !this.applePayDigitalWallet?.merchantId || diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.spec.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.spec.ts index 2f0df53a7dc..ed06a707d46 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.spec.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.spec.ts @@ -10,6 +10,7 @@ import { ElementRef, } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; + import { OpfGooglePayComponent } from './google-pay.component'; import { OpfGooglePayService } from './google-pay.service'; @@ -64,9 +65,9 @@ describe('OpfGooglePayComponent', () => { await detectChanges(); expect(mockOpfGooglePayService.loadResources).toHaveBeenCalled(); - expect(mockOpfGooglePayService.initClient).toHaveBeenCalledWith( - component.activeConfiguration - ); + expect(mockOpfGooglePayService.initClient).toHaveBeenCalledWith([ + component.activeConfiguration, + ]); }); it('should update ready to pay state when Google Pay is ready', async () => { diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.ts index 35eddd31506..17a5343a033 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.component.ts @@ -29,15 +29,20 @@ export class OpfGooglePayComponent implements OnInit { protected opfGooglePayService = inject(OpfGooglePayService); protected changeDetectionRef = inject(ChangeDetectorRef); - @Input() activeConfiguration: OpfActiveConfiguration; + @Input() activeConfiguration: + | OpfActiveConfiguration + | OpfActiveConfiguration[]; @ViewChild('googlePayButtonContainer') googlePayButtonContainer: ElementRef; isReadyToPayState$: BehaviorSubject = new BehaviorSubject(false); ngOnInit(): void { + const activeConfigurations = Array.isArray(this.activeConfiguration) + ? this.activeConfiguration + : [this.activeConfiguration]; this.opfGooglePayService.loadResources().then(() => { - this.opfGooglePayService.initClient(this.activeConfiguration); + this.opfGooglePayService.initClient(activeConfigurations); this.opfGooglePayService.isReadyToPay().then((response: any) => { this.isReadyToPayState$.next(!!response?.result); this.changeDetectionRef.detectChanges(); diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.spec.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.spec.ts index 1c567313dfe..bca5e28ea74 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.spec.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.spec.ts @@ -4,11 +4,15 @@ * SPDX-License-Identifier: Apache-2.0 */ +/// import { ElementRef } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { Cart } from '@spartacus/cart/base/root'; import { Address, PriceType } from '@spartacus/core'; -import { OpfResourceLoaderService } from '@spartacus/opf/base/root'; +import { + OpfActiveConfiguration, + OpfResourceLoaderService, +} from '@spartacus/opf/base/root'; import { OpfPaymentFacade } from '@spartacus/opf/payment/root'; import { OpfQuickBuyTransactionService } from '@spartacus/opf/quick-buy/core'; import { @@ -86,7 +90,7 @@ describe('OpfGooglePayService', () => { ]); mockQuickBuyButtonsService = jasmine.createSpyObj( 'OpfQuickBuyButtonsService', - ['getQuickBuyProviderConfig'] + ['getQuickBuyProviderConfig', 'getActiveConfigurationForProvider'] ); mockOpfQuickBuyConfig = { @@ -145,7 +149,7 @@ describe('OpfGooglePayService', () => { describe('getClient', () => { it('should return the Google Payment client instance', () => { - const activeConfiguration = {}; + const activeConfiguration: OpfActiveConfiguration[] = []; service.initClient(activeConfiguration); const client = service['getClient'](); @@ -225,7 +229,7 @@ describe('OpfGooglePayService', () => { describe('isReadyToPay', () => { it('should return info about readiness to pay from the Google Pay API', async () => { - const activeConfiguration = {}; + const activeConfiguration: OpfActiveConfiguration[] = []; service.initClient(activeConfiguration); @@ -246,8 +250,20 @@ describe('OpfGooglePayService', () => { }); describe('initClient', () => { - it('should initialize the Google Payment client with configurations', () => { - const activeConfiguration = {}; + it('should initialize the Google Payment client with configurations (array)', () => { + const activeConfiguration: any[] = []; + service.initClient(activeConfiguration); + + const client = service['googlePaymentClient']; + + expect(client).toBeDefined(); + }); + + it('should initialize the Google Payment client with single configuration', () => { + const activeConfiguration: OpfActiveConfiguration = { + merchantId: 'test-merchant', + providerType: 'PAYMENT_GATEWAY', + } as any; service.initClient(activeConfiguration); const client = service['googlePaymentClient']; diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.ts index 8b3802da025..dcb8afa3115 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/google-pay/google-pay.service.ts @@ -169,8 +169,20 @@ export class OpfGooglePayService { ]); } - initClient(activeConfiguration: OpfActiveConfiguration): void { - this.setAllowedPaymentMethodsConfig(activeConfiguration); + initClient( + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] + ): void { + const activeConfigurations = Array.isArray(activeConfiguration) + ? activeConfiguration + : [activeConfiguration]; + const googlePayGateway = + this.opfQuickBuyButtonsService.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.GOOGLE_PAY, + activeConfigurations + ); + if (googlePayGateway) { + this.setAllowedPaymentMethodsConfig(googlePayGateway); + } this.updateGooglePaymentClient(); } @@ -498,9 +510,8 @@ export class OpfGooglePayService { const googlePayConfig = this.opfQuickBuyButtonsService.getQuickBuyProviderConfig( OpfQuickBuyProviderType.GOOGLE_PAY, - activeConfiguration + [activeConfiguration] ); - this.googlePaymentRequest.allowedPaymentMethods = [ { parameters: { diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.spec.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.spec.ts index 474d3a636a9..d7878cb33ff 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.spec.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.spec.ts @@ -11,6 +11,7 @@ import { OpfQuickBuyProviderType } from '../../root/model'; import { OpfQuickBuyButtonsComponent } from './opf-quick-buy-buttons.component'; import { OpfQuickBuyButtonsService } from './opf-quick-buy-buttons.service'; import createSpy = jasmine.createSpy; +import { OpfActiveConfiguration } from '@spartacus/opf/base/root'; const routerStateSubject = new BehaviorSubject({ state: { @@ -33,6 +34,8 @@ describe('OpfQuickBuyButtonsComponent', () => { opfQuickBuyButtonsServiceMock = jasmine.createSpyObj('OpfQuickBuyService', [ 'getPaymentGatewayConfiguration', 'isQuickBuyProviderEnabled', + 'getQuickBuyProviderConfig', + 'getActiveConfigurationForProvider', ]); await TestBed.configureTestingModule({ @@ -70,7 +73,7 @@ describe('OpfQuickBuyButtonsComponent', () => { it('should determine if a payment method is enabled', () => { const provider = OpfQuickBuyProviderType.APPLE_PAY; - const activeConfiguration = {}; + const activeConfiguration: OpfActiveConfiguration[] = []; opfQuickBuyButtonsServiceMock.isQuickBuyProviderEnabled.and.returnValue( true ); diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.ts index 0130fe61636..daa5c221ded 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.component.ts @@ -26,7 +26,9 @@ import { OpfQuickBuyButtonsService } from './opf-quick-buy-buttons.service'; }) export class OpfQuickBuyButtonsComponent implements OnInit { protected opfQuickBuyButtonsService = inject(OpfQuickBuyButtonsService); - protected paymentGatewayConfig$: Observable; + protected paymentGatewayConfig$: Observable< + OpfActiveConfiguration | OpfActiveConfiguration[] + >; PAYMENT_METHODS = OpfQuickBuyProviderType; @@ -37,7 +39,7 @@ export class OpfQuickBuyButtonsComponent implements OnInit { isPaymentMethodEnabled( provider: OpfQuickBuyProviderType, - activeConfiguration: OpfActiveConfiguration + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] ): boolean { return this.opfQuickBuyButtonsService.isQuickBuyProviderEnabled( provider, diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.spec.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.spec.ts index 774eab36a26..4ce9cc2d771 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.spec.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.spec.ts @@ -52,7 +52,7 @@ describe('OpfQuickBuyButtonsService', () => { }); describe('getPaymentGatewayConfiguration', () => { - it('should return the first PAYMENT_GATEWAY configuration when available', () => { + it('should return all PAYMENT_GATEWAY configurations when available', () => { const mockConfigurations = [ { providerType: OpfPaymentProviderType.PAYMENT_METHOD }, { providerType: OpfPaymentProviderType.PAYMENT_GATEWAY }, @@ -65,7 +65,10 @@ describe('OpfQuickBuyButtonsService', () => { ); service.getPaymentGatewayConfiguration().subscribe((result) => { - expect(result).toEqual(mockConfigurations[1]); + expect(result).toEqual([ + mockConfigurations[1], + mockConfigurations[2], + ] as any); }); }); @@ -77,7 +80,7 @@ describe('OpfQuickBuyButtonsService', () => { ); service.getPaymentGatewayConfiguration().subscribe((result) => { - expect(result).toBeUndefined(); + expect(result).toEqual([]); }); }); @@ -90,7 +93,7 @@ describe('OpfQuickBuyButtonsService', () => { ); service.getPaymentGatewayConfiguration().subscribe((result) => { - expect(result).toBeUndefined(); + expect(result).toEqual([]); }); }); @@ -110,12 +113,28 @@ describe('OpfQuickBuyButtonsService', () => { describe('isQuickBuyProviderEnabled', () => { const provider = OpfQuickBuyProviderType.APPLE_PAY; - it('should return true when the provider is enabled', () => { + it('should return true when the provider is enabled (array)', () => { + const activeConfiguration = [ + { + digitalWalletQuickBuy: [ + { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: true }, + ], + } as any, + ]; + + const result = service.isQuickBuyProviderEnabled( + provider, + activeConfiguration + ); + expect(result).toBeTruthy(); + }); + + it('should return true when the provider is enabled (single object)', () => { const activeConfiguration = { digitalWalletQuickBuy: [ { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: true }, ], - }; + } as any; const result = service.isQuickBuyProviderEnabled( provider, @@ -125,11 +144,13 @@ describe('OpfQuickBuyButtonsService', () => { }); it('should return false when the provider is disabled', () => { - const activeConfiguration = { - digitalWalletQuickBuy: [ - { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: false }, - ], - }; + const activeConfiguration = [ + { + digitalWalletQuickBuy: [ + { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: false }, + ], + } as any, + ]; const result = service.isQuickBuyProviderEnabled( provider, @@ -139,11 +160,13 @@ describe('OpfQuickBuyButtonsService', () => { }); it('should return false when the provider is not found', () => { - const activeConfiguration = { - digitalWalletQuickBuy: [ - { provider: 'otherProvider' as any, enabled: true }, - ], - }; + const activeConfiguration = [ + { + digitalWalletQuickBuy: [ + { provider: 'otherProvider' as any, enabled: true }, + ], + } as any, + ]; const result = service.isQuickBuyProviderEnabled( provider, @@ -167,9 +190,11 @@ describe('OpfQuickBuyButtonsService', () => { it('should return false when digitalWalletQuickBuy is null or empty', () => { const provider = OpfQuickBuyProviderType.APPLE_PAY; - const activeConfiguration = { - digitalWalletQuickBuy: null as any, - }; + const activeConfiguration = [ + { + digitalWalletQuickBuy: null as any, + } as any, + ]; const result = service.isQuickBuyProviderEnabled( provider, @@ -188,13 +213,30 @@ describe('OpfQuickBuyButtonsService', () => { enabled: true, }; - it('should return config for specific provider', () => { + it('should return config for specific provider (array)', () => { + const activeConfiguration = [ + { + digitalWalletQuickBuy: [ + { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: true }, + config, + ], + } as any, + ]; + + const result = service.getQuickBuyProviderConfig( + OpfQuickBuyProviderType.GOOGLE_PAY, + activeConfiguration + ); + expect(result).toBe(config); + }); + + it('should return config for specific provider (single object)', () => { const activeConfiguration = { digitalWalletQuickBuy: [ { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: true }, config, ], - }; + } as any; const result = service.getQuickBuyProviderConfig( OpfQuickBuyProviderType.GOOGLE_PAY, @@ -202,5 +244,95 @@ describe('OpfQuickBuyButtonsService', () => { ); expect(result).toBe(config); }); + + it('should return undefined when activeConfiguration is null', () => { + const result = service.getQuickBuyProviderConfig( + OpfQuickBuyProviderType.GOOGLE_PAY, + null as any + ); + expect(result).toBeUndefined(); + }); + + it('should return undefined when activeConfiguration is undefined', () => { + const result = service.getQuickBuyProviderConfig( + OpfQuickBuyProviderType.GOOGLE_PAY, + undefined as any + ); + expect(result).toBeUndefined(); + }); + }); + + describe('getActiveConfigurationForProvider', () => { + const mockConfig = { + digitalWalletQuickBuy: [ + { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: true }, + ], + } as any; + + it('should return configuration for specific provider (array)', () => { + const activeConfigurations = [mockConfig]; + + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.APPLE_PAY, + activeConfigurations + ); + expect(result).toBe(mockConfig); + }); + + it('should return configuration for specific provider (single object)', () => { + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.APPLE_PAY, + mockConfig + ); + expect(result).toBe(mockConfig); + }); + + it('should return undefined when provider is not found (array)', () => { + const activeConfigurations = [mockConfig]; + + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.GOOGLE_PAY, + activeConfigurations + ); + expect(result).toBeUndefined(); + }); + + it('should return undefined when provider is not found (single object)', () => { + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.GOOGLE_PAY, + mockConfig + ); + expect(result).toBeUndefined(); + }); + + it('should return undefined when activeConfiguration is null', () => { + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.APPLE_PAY, + null as any + ); + expect(result).toBeUndefined(); + }); + + it('should return undefined when activeConfiguration is undefined', () => { + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.APPLE_PAY, + undefined as any + ); + expect(result).toBeUndefined(); + }); + + it('should return undefined when provider is disabled', () => { + const disabledConfig = { + digitalWalletQuickBuy: [ + { provider: OpfQuickBuyProviderType.APPLE_PAY, enabled: false }, + ], + } as any; + + const result = service.getActiveConfigurationForProvider( + OpfQuickBuyProviderType.APPLE_PAY, + disabledConfig + ); + expect(result).toBeUndefined(); + }); }); }); diff --git a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.ts b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.ts index e1b48996e83..e731310e5ca 100644 --- a/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.ts +++ b/integration-libs/opf/quick-buy/components/opf-quick-buy-buttons/opf-quick-buy-buttons.service.ts @@ -34,47 +34,62 @@ export class OpfQuickBuyButtonsService { protected activeCartFacade = inject(ActiveCartFacade); protected multiCartFacade = inject(MultiCartFacade); - getPaymentGatewayConfiguration(): Observable { + getPaymentGatewayConfiguration(): Observable< + OpfActiveConfiguration | OpfActiveConfiguration[] + > { return this.opfBaseFacade .getActiveConfigurationsState() .pipe( - map( - (config) => - (config?.data?.value || []).filter( - (item) => - item?.providerType === OpfPaymentProviderType.PAYMENT_GATEWAY - )[0] + map((config) => + (config?.data?.value || []).filter( + (item) => + item?.providerType === OpfPaymentProviderType.PAYMENT_GATEWAY + ) ) ); } getQuickBuyProviderConfig( provider: OpfQuickBuyProviderType, - activeConfiguration: OpfActiveConfiguration + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] ): OpfQuickBuyDigitalWallet | undefined { - let config; - if (activeConfiguration && activeConfiguration.digitalWalletQuickBuy) { - config = activeConfiguration?.digitalWalletQuickBuy.find( - (item) => item.provider === provider - ); - } - - return config; + const configs = this.normalizeConfigurations(activeConfiguration); + return configs + ?.flatMap((config) => config?.digitalWalletQuickBuy ?? []) + .find((item) => item.provider === provider && item.enabled); } isQuickBuyProviderEnabled( provider: OpfQuickBuyProviderType, - activeConfiguration: OpfActiveConfiguration + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] ): boolean { - let isEnabled = false; - if (activeConfiguration && activeConfiguration.digitalWalletQuickBuy) { - isEnabled = Boolean( - activeConfiguration?.digitalWalletQuickBuy.find( - (item) => item.provider === provider - )?.enabled - ); - } + return !!this.getQuickBuyProviderConfig(provider, activeConfiguration); + } - return isEnabled; + getActiveConfigurationForProvider( + provider: OpfQuickBuyProviderType, + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] + ): OpfActiveConfiguration | undefined { + const configs = this.normalizeConfigurations(activeConfiguration); + return configs?.find((config) => + config?.digitalWalletQuickBuy?.some( + (item) => item.provider === provider && item.enabled + ) + ); + } + + /** + * Normalizes ActiveConfiguration to always return an array. + * Returns empty array if configurations are null/undefined. + */ + protected normalizeConfigurations( + activeConfiguration: OpfActiveConfiguration | OpfActiveConfiguration[] + ): OpfActiveConfiguration[] | undefined { + if (!activeConfiguration) { + return undefined; + } + return Array.isArray(activeConfiguration) + ? activeConfiguration + : [activeConfiguration]; } }