-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async init for embedded and initial selection #4068
Changes from 9 commits
3443723
7707959
0d645a6
2d359fd
df0e5b4
00fd4f5
fbb8df5
94429a3
bb0cf70
f7acdd6
cd37ab5
979f6ec
486fcb6
a8ca11d
7db1816
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,19 +55,59 @@ 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a temporary hack right? If so, let's document it |
||
|
||
// 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, | ||
integrationShape: .embedded) | ||
|
||
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() | ||
) | ||
|
||
let initialSelection: EmbeddedPaymentMethodsView.Selection? = { | ||
// Default to the customer's default or the first saved payment method, if any | ||
let customerDefault = CustomerPaymentOption.defaultPaymentMethod(for: configuration.customer?.id) | ||
switch customerDefault { | ||
case .applePay: | ||
return .applePay | ||
case .link: | ||
return .link | ||
case .stripeId, nil: | ||
return loadResult.savedPaymentMethods.first.map { .saved(paymentMethod: $0) } | ||
} | ||
}() | ||
|
||
let embeddedPaymentMethodsView = await EmbeddedPaymentMethodsView( | ||
initialSelection: initialSelection, | ||
paymentMethodTypes: paymentMethodTypes, | ||
savedPaymentMethod: loadResult.savedPaymentMethods.first, | ||
appearance: configuration.appearance, | ||
shouldShowApplePay: shouldShowApplePay, | ||
shouldShowLink: shouldShowLink, | ||
savedPaymentMethodAccessoryType: savedPaymentMethodAccessoryType | ||
) | ||
return .init(view: dummyView, configuration: configuration) | ||
return .init(view: embeddedPaymentMethodsView, configuration: configuration) | ||
} | ||
|
||
/// The result of an `update` call | ||
@frozen 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 | ||
|
@@ -180,3 +220,41 @@ extension EmbeddedPaymentElement { | |
public typealias BillingDetailsCollectionConfiguration = PaymentSheet.BillingDetailsCollectionConfiguration | ||
public typealias ExternalPaymentMethodConfiguration = PaymentSheet.ExternalPaymentMethodConfiguration | ||
} | ||
|
||
// TODO(porter) Create a protocol for the commonalities between PaymentSheet.Configuration <> EmbeddedPaymentElement.Configuration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you gonna follow up on this immediately or should we make a ticket There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah will do later, just created: https://jira.corp.stripe.com/browse/MOBILESDK-2533 |
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will we add Embedded-specific stuff like
hidesMandateText
orformSheetAction
if we go from playground settings -> PaymentSheet.Configuration -> EmbeddedConfiguration?Maybe we should make the "playground settings -> EmbeddedConfiguration" logic happen in the PlaygroundController or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think creating the EmbeddedConfiguration in the PlaygroundController sounds good, I was thinking we could just inject those two properties into this view controller but that doesn't scale well if we add more. Will update this in this PR.