From 2d359fd493b88a6ddf185d9213cea5638d93cc8d Mon Sep 17 00:00:00 2001 From: Nick Porter Date: Fri, 27 Sep 2024 11:16:04 -0600 Subject: [PATCH] Implement load --- .../Embedded/EmbeddedPaymentElement.swift | 78 +++++++++++++++++-- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift index 5487dc0797b..c7874f38df4 100644 --- a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift +++ b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Embedded/EmbeddedPaymentElement.swift @@ -52,19 +52,44 @@ public class EmbeddedPaymentElement { intentConfiguration: IntentConfiguration, configuration: Configuration ) async throws -> EmbeddedPaymentElement { - // TODO(https://jira.corp.stripe.com/browse/MOBILESDK-2525) - let dummyView = await EmbeddedPaymentMethodsView( - savedPaymentMethod: nil, - appearance: .default, - shouldShowApplePay: true, - shouldShowLink: true + let paymentSheetConfiguration = configuration.makePaymentSheetConfiguration() + + // TODO(porter) When we do analytics decide how to handle `isCustom` + let analyticsHelper = PaymentSheetAnalyticsHelper(isCustom: true, configuration: paymentSheetConfiguration) + AnalyticsHelper.shared.generateSessionID() + + let loadResult = try await PaymentSheetLoader.load(mode: .deferredIntent(intentConfiguration), + configuration: paymentSheetConfiguration, + analyticsHelper: analyticsHelper, isFlowController: true) + + let paymentMethodTypes = PaymentSheet.PaymentMethodType.filteredPaymentMethodTypes(from: .deferredIntent(intentConfig: intentConfiguration), + elementsSession: loadResult.elementsSession, + configuration: paymentSheetConfiguration, + logAvailability: true) + let shouldShowApplePay = PaymentSheet.isApplePayEnabled(elementsSession: loadResult.elementsSession, configuration: paymentSheetConfiguration) + let shouldShowLink = PaymentSheet.isLinkEnabled(elementsSession: loadResult.elementsSession, configuration: paymentSheetConfiguration) + let savedPaymentMethodAccessoryType = await RowButton.RightAccessoryButton.getAccessoryButtonType( + savedPaymentMethodsCount: loadResult.savedPaymentMethods.count, + isFirstCardCoBranded: loadResult.savedPaymentMethods.first?.isCoBrandedCard ?? false, + isCBCEligible: loadResult.elementsSession.isCardBrandChoiceEligible, + allowsRemovalOfLastSavedPaymentMethod: configuration.allowsRemovalOfLastSavedPaymentMethod, + allowsPaymentMethodRemoval: loadResult.elementsSession.allowsRemovalOfPaymentMethodsForPaymentSheet() ) - return .init(view: dummyView) + + let embeddedPaymentMethodsView = await EmbeddedPaymentMethodsView( + paymentMethodTypes: paymentMethodTypes, + savedPaymentMethod: loadResult.savedPaymentMethods.first, + appearance: configuration.appearance, + shouldShowApplePay: shouldShowApplePay, + shouldShowLink: shouldShowLink, + savedPaymentMethodAccessoryType: savedPaymentMethodAccessoryType + ) + return .init(view: embeddedPaymentMethodsView) } /// The result of an `update` call public enum UpdateResult { - /// The update succeded + /// The update succeeded case succeeded /// The update was canceled. This is only returned when a subsequent `update` call cancels previous ones. case canceled @@ -176,3 +201,40 @@ extension EmbeddedPaymentElement { public typealias BillingDetailsCollectionConfiguration = PaymentSheet.BillingDetailsCollectionConfiguration public typealias ExternalPaymentMethodConfiguration = PaymentSheet.ExternalPaymentMethodConfiguration } + +extension EmbeddedPaymentElement.Configuration { + func makePaymentSheetConfiguration() -> PaymentSheet.Configuration { + var paymentConfig = PaymentSheet.Configuration() + + paymentConfig.allowsDelayedPaymentMethods = allowsDelayedPaymentMethods + paymentConfig.allowsPaymentMethodsRequiringShippingAddress = allowsPaymentMethodsRequiringShippingAddress + paymentConfig.apiClient = apiClient + paymentConfig.applePay = applePay + paymentConfig.primaryButtonColor = primaryButtonColor + paymentConfig.primaryButtonLabel = primaryButtonLabel + paymentConfig.style = style + paymentConfig.customer = customer + paymentConfig.merchantDisplayName = merchantDisplayName + paymentConfig.returnURL = returnURL + paymentConfig.defaultBillingDetails = defaultBillingDetails + paymentConfig.savePaymentMethodOptInBehavior = savePaymentMethodOptInBehavior + paymentConfig.appearance = appearance + paymentConfig.shippingDetails = shippingDetails + paymentConfig.preferredNetworks = preferredNetworks + paymentConfig.userOverrideCountry = userOverrideCountry + paymentConfig.billingDetailsCollectionConfiguration = billingDetailsCollectionConfiguration + paymentConfig.removeSavedPaymentMethodMessage = removeSavedPaymentMethodMessage + paymentConfig.externalPaymentMethodConfiguration = externalPaymentMethodConfiguration + paymentConfig.paymentMethodOrder = paymentMethodOrder + paymentConfig.allowsRemovalOfLastSavedPaymentMethod = allowsRemovalOfLastSavedPaymentMethod + + /* Note: + There are 3 properties that differ today: + hidesMandateText + formSheetAction + paymentMethodLayout + */ + + return paymentConfig + } +}