From 31f921c5546fe3ee74be68c75ee72c4fd234f0fa Mon Sep 17 00:00:00 2001 From: Robert Schulze Dieckhoff Date: Mon, 10 Mar 2025 14:27:53 +0100 Subject: [PATCH] Aligned creation of card component between session and advanced flow --- .../components/card/BaseCardComponent.swift | 19 +++++++++++--- .../card/advanced/CardAdvancedComponent.swift | 25 ++++++++----------- .../card/session/CardSessionComponent.swift | 13 +++------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ios/Classes/components/card/BaseCardComponent.swift b/ios/Classes/components/card/BaseCardComponent.swift index 5b98f6d3..d0fa4dd6 100644 --- a/ios/Classes/components/card/BaseCardComponent.swift +++ b/ios/Classes/components/card/BaseCardComponent.swift @@ -57,17 +57,28 @@ class BaseCardComponent: NSObject, FlutterPlatformView, UIScrollViewDelegate { } func buildCardComponent( - paymentMethodString: String, + paymentMethodString: String?, isStoredPaymentMethod: Bool, - cardComponentConfiguration: CardComponentConfigurationDTO, - adyenContext: AdyenContext + cardComponentConfiguration: CardComponentConfigurationDTO?, + componentDelegate: PaymentComponentDelegate?, + cardDelegate: CardComponentDelegate? ) throws -> CardComponent { + guard let paymentMethodString = paymentMethod else { throw PlatformError(errorDescription: "Payment method not found") } + guard let cardComponentConfiguration else { throw PlatformError(errorDescription: "Card configuration not found") } + let adyenContext = try cardComponentConfiguration.createAdyenContext() let cardConfiguration = cardComponentConfiguration.cardConfiguration.mapToCardComponentConfiguration( shopperLocale: cardComponentConfiguration.shopperLocale) let paymentMethod: AnyCardPaymentMethod = isStoredPaymentMethod ? try JSONDecoder().decode(StoredCardPaymentMethod.self, from: Data(paymentMethodString.utf8)) : try JSONDecoder().decode(CardPaymentMethod.self, from: Data(paymentMethodString.utf8)) - return CardComponent(paymentMethod: paymentMethod, context: adyenContext, configuration: cardConfiguration) + let cardComponent = CardComponent( + paymentMethod: paymentMethod, + context: adyenContext, + configuration: cardConfiguration + ) + cardComponent.delegate = componentDelegate + cardComponent.cardComponentDelegate = cardDelegate + return cardComponent } func showCardComponent(cardComponent: CardComponent) { diff --git a/ios/Classes/components/card/advanced/CardAdvancedComponent.swift b/ios/Classes/components/card/advanced/CardAdvancedComponent.swift index de260751..a0edb0de 100644 --- a/ios/Classes/components/card/advanced/CardAdvancedComponent.swift +++ b/ios/Classes/components/card/advanced/CardAdvancedComponent.swift @@ -7,7 +7,7 @@ class CardAdvancedComponent: BaseCardComponent { private var actionComponentDelegate: ActionComponentDelegate? private var actionComponent: AdyenActionComponent? private var presentationDelegate: PresentationDelegate? - private var cardDelegate: PaymentComponentDelegate? + private var componentDelegate: PaymentComponentDelegate? override init( frame: CGRect, @@ -38,6 +38,7 @@ class CardAdvancedComponent: BaseCardComponent { private func setupCardComponentView() { do { let cardComponent = try setupCardComponent() + actionComponent = buildActionComponent(adyenContext: cardComponent.context) showCardComponent(cardComponent: cardComponent) componentPlatformApi.onActionCallback = { [weak self] jsonActionResponse in self?.onAction(actionResponse: jsonActionResponse) @@ -51,21 +52,17 @@ class CardAdvancedComponent: BaseCardComponent { } private func setupCardComponent() throws -> CardComponent { - guard let cardComponentConfiguration else { throw PlatformError(errorDescription: "Card configuration not found") } - guard let paymentMethodString = paymentMethod else { throw PlatformError(errorDescription: "Payment method not found") } - let adyenContext = try cardComponentConfiguration.createAdyenContext() - let cardComponent = try buildCardComponent( - paymentMethodString: paymentMethodString, + componentDelegate = CardAdvancedFlowDelegate( + componentFlutterApi: componentFlutterApi, + componentId: componentId + ) + return try buildCardComponent( + paymentMethodString: paymentMethod, isStoredPaymentMethod: isStoredPaymentMethod, cardComponentConfiguration: cardComponentConfiguration, - adyenContext: adyenContext - ) - - actionComponent = buildActionComponent(adyenContext: adyenContext) - cardDelegate = CardAdvancedFlowDelegate(componentFlutterApi: componentFlutterApi, componentId: componentId) - cardComponent.delegate = cardDelegate - cardComponent.cardComponentDelegate = self - return cardComponent + componentDelegate: componentDelegate, + cardDelegate: self + ) } private func buildActionComponent(adyenContext: AdyenContext) -> AdyenActionComponent { diff --git a/ios/Classes/components/card/session/CardSessionComponent.swift b/ios/Classes/components/card/session/CardSessionComponent.swift index 16ef8af0..a3e4b18d 100644 --- a/ios/Classes/components/card/session/CardSessionComponent.swift +++ b/ios/Classes/components/card/session/CardSessionComponent.swift @@ -39,19 +39,14 @@ class CardSessionComponent: BaseCardComponent { } private func setupCardComponent() throws -> CardComponent { - guard let cardComponentConfiguration else { throw PlatformError(errorDescription: "Card configuration not found") } - guard let paymentMethodString = paymentMethod else { throw PlatformError(errorDescription: "Payment method not found") } guard let session = sessionHolder.session else { throw PlatformError(errorDescription: "Session not found") } - let adyenContext = try cardComponentConfiguration.createAdyenContext() - let cardComponent = try buildCardComponent( - paymentMethodString: paymentMethodString, + return try buildCardComponent( + paymentMethodString: paymentMethod, isStoredPaymentMethod: isStoredPaymentMethod, cardComponentConfiguration: cardComponentConfiguration, - adyenContext: adyenContext + componentDelegate: session, + cardDelegate: self ) - cardComponent.delegate = session - cardComponent.cardComponentDelegate = self - return cardComponent } private func setupSessionFlowDelegate() {