Skip to content

Commit

Permalink
Implement load
Browse files Browse the repository at this point in the history
  • Loading branch information
porter-stripe committed Sep 27, 2024
1 parent 0d645a6 commit 2d359fd
Showing 1 changed file with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 2d359fd

Please sign in to comment.