From 8f0b6abfcf658cace4501904e1632c0437f6c425 Mon Sep 17 00:00:00 2001 From: Robert Schulze Dieckhoff Date: Mon, 16 Oct 2023 16:12:48 +0200 Subject: [PATCH] Moved dropin configuration mapping into dto mapper --- example/lib/main.dart | 11 +++- lib/adyen_checkout.dart | 1 + lib/src/adyen_checkout.dart | 61 +------------------ lib/src/models/drop_in_configuration.dart | 12 ---- .../stored_payment_method_configuration.dart | 11 ++++ lib/src/utils/dto_mapper.dart | 56 +++++++++-------- 6 files changed, 53 insertions(+), 99 deletions(-) create mode 100644 lib/src/models/stored_payment_method_configuration.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 3847f5c1..4cb4a8a8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -106,6 +106,11 @@ class _MyAppState extends State { final CashAppPayConfiguration cashAppPayConfiguration = await _createCashAppPayConfiguration(); + final ApplePayConfiguration applePayConfiguration = ApplePayConfiguration( + merchantId: Config.merchantAccount, + merchantName: Config.merchantName, + ); + final DropInConfiguration dropInConfiguration = DropInConfiguration( environment: Environment.test, clientKey: Config.clientKey, @@ -115,6 +120,7 @@ class _MyAppState extends State { cardsConfiguration: cardsConfiguration, storedPaymentMethodConfiguration: storedPaymentMethodConfiguration, cashAppPayConfiguration: cashAppPayConfiguration, + applePayConfiguration: applePayConfiguration, ); return await _adyenCheckout.startPayment( @@ -129,8 +135,9 @@ class _MyAppState extends State { final String paymentMethodsResponse = await _adyenSessionRepository.fetchPaymentMethods(); - final CardsConfiguration cardsConfiguration = - CardsConfiguration(showStorePaymentField: true); + final CardsConfiguration cardsConfiguration = CardsConfiguration( + showStorePaymentField: true, + ); final ApplePayConfiguration applePayConfiguration = ApplePayConfiguration( merchantId: Config.merchantAccount, diff --git a/lib/adyen_checkout.dart b/lib/adyen_checkout.dart index 921f1045..e8b8cc26 100644 --- a/lib/adyen_checkout.dart +++ b/lib/adyen_checkout.dart @@ -20,3 +20,4 @@ export 'src/models/payment_method_configurations/cash_app_pay_configuration.dart export 'src/models/payment_method_configurations/google_pay_configuration.dart'; export 'src/models/payment_result.dart'; export 'src/models/session.dart'; +export 'src/models/stored_payment_method_configuration.dart'; diff --git a/lib/src/adyen_checkout.dart b/lib/src/adyen_checkout.dart index a3e8ea4c..c49a2385 100644 --- a/lib/src/adyen_checkout.dart +++ b/lib/src/adyen_checkout.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'package:adyen_checkout/adyen_checkout.dart'; import 'package:adyen_checkout/src/adyen_checkout_interface.dart'; @@ -48,36 +47,9 @@ class AdyenCheckout implements AdyenCheckoutInterface { DropInSession dropInSession) async { _adyenLogger.print("Start Drop-in session"); final dropInSessionCompleter = Completer(); - DropInConfigurationDTO dropInConfiguration = DropInConfigurationDTO( - environment: dropInSession.dropInConfiguration.environment, - clientKey: dropInSession.dropInConfiguration.clientKey, - countryCode: dropInSession.dropInConfiguration.countryCode, - amount: dropInSession.dropInConfiguration.amount.toDTO(), - shopperLocale: dropInSession.dropInConfiguration.shopperLocale ?? - Platform.localeName, - cardsConfigurationDTO: - dropInSession.dropInConfiguration.cardsConfiguration?.toDTO(), - applePayConfigurationDTO: - dropInSession.dropInConfiguration.applePayConfiguration?.toDTO(), - googlePayConfigurationDTO: - dropInSession.dropInConfiguration.googlePayConfiguration?.toDTO(), - cashAppPayConfigurationDTO: - dropInSession.dropInConfiguration.cashAppPayConfiguration?.toDTO(), - analyticsOptionsDTO: - dropInSession.dropInConfiguration.analyticsOptions?.toDTO(), - isRemoveStoredPaymentMethodEnabled: - isRemoveStoredPaymentMethodEnabled(dropInSession.dropInConfiguration), - showPreselectedStoredPaymentMethod: dropInSession - .dropInConfiguration - .storedPaymentMethodConfiguration - ?.showPreselectedStoredPaymentMethod ?? - true, - skipListWhenSinglePaymentMethod: - dropInSession.dropInConfiguration.skipListWhenSinglePaymentMethod, - ); AdyenCheckoutPlatformInterface.instance.startDropInSessionPayment( session: dropInSession.session.toDTO(), - dropInConfiguration: dropInConfiguration, + dropInConfiguration: dropInSession.dropInConfiguration.toDTO(), ); _resultApi.dropInSessionPlatformCommunicationStream = @@ -124,39 +96,10 @@ class AdyenCheckout implements AdyenCheckoutInterface { DropInAdvancedFlow dropInAdvancedFlow) async { _adyenLogger.print("Start Drop-in advanced flow"); final dropInAdvancedFlowCompleter = Completer(); - DropInConfigurationDTO dropInConfiguration = DropInConfigurationDTO( - environment: dropInAdvancedFlow.dropInConfiguration.environment, - clientKey: dropInAdvancedFlow.dropInConfiguration.clientKey, - countryCode: dropInAdvancedFlow.dropInConfiguration.countryCode, - amount: dropInAdvancedFlow.dropInConfiguration.amount.toDTO(), - shopperLocale: dropInAdvancedFlow.dropInConfiguration.shopperLocale ?? - Platform.localeName, - cardsConfigurationDTO: - dropInAdvancedFlow.dropInConfiguration.cardsConfiguration?.toDTO(), - applePayConfigurationDTO: - dropInAdvancedFlow.dropInConfiguration.applePayConfiguration?.toDTO(), - googlePayConfigurationDTO: dropInAdvancedFlow - .dropInConfiguration.googlePayConfiguration - ?.toDTO(), - cashAppPayConfigurationDTO: dropInAdvancedFlow - .dropInConfiguration.cashAppPayConfiguration - ?.toDTO(), - analyticsOptionsDTO: - dropInAdvancedFlow.dropInConfiguration.analyticsOptions?.toDTO(), - showPreselectedStoredPaymentMethod: dropInAdvancedFlow - .dropInConfiguration - .storedPaymentMethodConfiguration - ?.showPreselectedStoredPaymentMethod ?? - true, - isRemoveStoredPaymentMethodEnabled: isRemoveStoredPaymentMethodEnabled( - dropInAdvancedFlow.dropInConfiguration), - skipListWhenSinglePaymentMethod: dropInAdvancedFlow - .dropInConfiguration.skipListWhenSinglePaymentMethod, - ); AdyenCheckoutPlatformInterface.instance.startDropInAdvancedFlowPayment( paymentMethodsResponse: dropInAdvancedFlow.paymentMethodsResponse, - dropInConfiguration: dropInConfiguration, + dropInConfiguration: dropInAdvancedFlow.dropInConfiguration.toDTO(), ); _resultApi.dropInAdvancedFlowPlatformCommunicationStream = diff --git a/lib/src/models/drop_in_configuration.dart b/lib/src/models/drop_in_configuration.dart index 0eb5e13b..4e436055 100644 --- a/lib/src/models/drop_in_configuration.dart +++ b/lib/src/models/drop_in_configuration.dart @@ -30,15 +30,3 @@ class DropInConfiguration { this.skipListWhenSinglePaymentMethod = false, }); } - -class StoredPaymentMethodConfiguration { - final bool showPreselectedStoredPaymentMethod; - final bool isRemoveStoredPaymentMethodEnabled; - final Future Function(String)? deleteStoredPaymentMethodCallback; - - StoredPaymentMethodConfiguration({ - this.showPreselectedStoredPaymentMethod = true, - this.isRemoveStoredPaymentMethodEnabled = false, - this.deleteStoredPaymentMethodCallback, - }); -} diff --git a/lib/src/models/stored_payment_method_configuration.dart b/lib/src/models/stored_payment_method_configuration.dart new file mode 100644 index 00000000..13f4c07b --- /dev/null +++ b/lib/src/models/stored_payment_method_configuration.dart @@ -0,0 +1,11 @@ +class StoredPaymentMethodConfiguration { + final bool showPreselectedStoredPaymentMethod; + final bool isRemoveStoredPaymentMethodEnabled; + final Future Function(String)? deleteStoredPaymentMethodCallback; + + StoredPaymentMethodConfiguration({ + this.showPreselectedStoredPaymentMethod = true, + this.isRemoveStoredPaymentMethodEnabled = false, + this.deleteStoredPaymentMethodCallback, + }); +} \ No newline at end of file diff --git a/lib/src/utils/dto_mapper.dart b/lib/src/utils/dto_mapper.dart index eaa882e5..0a0db92c 100644 --- a/lib/src/utils/dto_mapper.dart +++ b/lib/src/utils/dto_mapper.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:adyen_checkout/adyen_checkout.dart'; import 'package:adyen_checkout/src/generated/platform_api.g.dart'; import 'package:adyen_checkout/src/models/analytics_options.dart'; @@ -9,6 +11,34 @@ extension AnalyticsOptionsMapper on AnalyticsOptions { ); } +extension DropInConfigurationMapper on DropInConfiguration { + DropInConfigurationDTO toDTO() => DropInConfigurationDTO( + environment: environment, + clientKey: clientKey, + countryCode: countryCode.toUpperCase(), + amount: amount.toDTO(), + shopperLocale: shopperLocale ?? Platform.localeName, + cardsConfigurationDTO: cardsConfiguration?.toDTO(), + applePayConfigurationDTO: applePayConfiguration?.toDTO(), + googlePayConfigurationDTO: googlePayConfiguration?.toDTO(), + cashAppPayConfigurationDTO: cashAppPayConfiguration?.toDTO(), + analyticsOptionsDTO: analyticsOptions?.toDTO(), + isRemoveStoredPaymentMethodEnabled: _isRemoveStoredPaymentMethodEnabled( + storedPaymentMethodConfiguration), + showPreselectedStoredPaymentMethod: storedPaymentMethodConfiguration + ?.showPreselectedStoredPaymentMethod ?? + true, + skipListWhenSinglePaymentMethod: skipListWhenSinglePaymentMethod, + ); + + bool _isRemoveStoredPaymentMethodEnabled( + StoredPaymentMethodConfiguration? storedPaymentMethodConfiguration) => + storedPaymentMethodConfiguration?.deleteStoredPaymentMethodCallback != + null && + storedPaymentMethodConfiguration?.isRemoveStoredPaymentMethodEnabled == + true; +} + extension CardsConfigurationMapper on CardsConfiguration { CardsConfigurationDTO toDTO() => CardsConfigurationDTO( holderNameRequired: holderNameRequired, @@ -75,29 +105,3 @@ extension OrderResponseMapper on OrderResponseDTO { orderData: orderData, ); } - -extension CardsConfigurationDTOCopy on CardsConfigurationDTO { - CardsConfigurationDTO copyWith({ - bool? holderNameRequired, - AddressMode? addressMode, - bool? showStorePaymentField, - bool? showCvcForStoredCard, - bool? showCvc, - FieldVisibility? kcpFieldVisibility, - FieldVisibility? socialSecurityNumberFieldVisibility, - List? supportedCardTypes, - }) => - CardsConfigurationDTO( - holderNameRequired: holderNameRequired ?? this.holderNameRequired, - addressMode: addressMode ?? this.addressMode, - showStorePaymentField: - showStorePaymentField ?? this.showStorePaymentField, - showCvcForStoredCard: showCvcForStoredCard ?? this.showCvcForStoredCard, - showCvc: showCvc ?? this.showCvc, - kcpFieldVisibility: kcpFieldVisibility ?? this.kcpFieldVisibility, - socialSecurityNumberFieldVisibility: - socialSecurityNumberFieldVisibility ?? - this.socialSecurityNumberFieldVisibility, - supportedCardTypes: supportedCardTypes ?? this.supportedCardTypes, - ); -}