Skip to content
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

read from customersession setting instead of config #4517

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a149588
read from customersession setting instead of config
joyceqin-stripe Jan 30, 2025
4660d4f
changed naming to better match others
joyceqin-stripe Jan 30, 2025
2057cb2
merged with master
joyceqin-stripe Jan 30, 2025
cf5fd54
elementssession test
joyceqin-stripe Jan 30, 2025
b6cfbe9
remove canUpdateCardBrand (removed in master)
joyceqin-stripe Jan 30, 2025
cb9d01d
fix build
joyceqin-stripe Jan 30, 2025
907a363
fix test
joyceqin-stripe Jan 30, 2025
08087f6
merge with main
joyceqin-stripe Jan 30, 2025
9aa6307
fixing build
joyceqin-stripe Jan 31, 2025
23358dd
replace payment_method_set_as_default with payment_method_sync_defaul…
joyceqin-stripe Feb 4, 2025
782ffec
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 4, 2025
d43d73e
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 4, 2025
68ae80e
remove @testable import from STPTestingAPIClient
joyceqin-stripe Feb 4, 2025
56b7ff8
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 4, 2025
9c268e7
read from elementsSession in makeViewModels instead of passing it in
joyceqin-stripe Feb 5, 2025
ac0fdf1
Merge branch 'joyceqin/MOBILESDK-2666' of github.com:stripe/stripe-io…
joyceqin-stripe Feb 5, 2025
3aef64e
mock network for tests with updated ci test backend
joyceqin-stripe Feb 5, 2025
ea32e91
revert timeout
joyceqin-stripe Feb 5, 2025
2fc6c05
do not show apple pay in customersheet when adding first saved pm
joyceqin-stripe Feb 6, 2025
4f2f131
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 10, 2025
1480972
cleanup
joyceqin-stripe Feb 10, 2025
f88219a
Merge branch 'joyceqin/MOBILESDK-2666' of github.com:stripe/stripe-io…
joyceqin-stripe Feb 10, 2025
69d0483
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 10, 2025
f9bd545
Merge branch 'master' into joyceqin/MOBILESDK-2666
joyceqin-stripe Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ struct CustomerSessionPlaygroundView: View {
if viewModel.paymentMethodSave == .disabled {
SettingPickerView(setting: $viewModel.allowRedisplayOverride)
}
else {
SettingPickerView(setting: $viewModel.paymentMethodSetAsDefault)
}
SettingPickerView(setting: $viewModel.paymentMethodRemove)
SettingPickerView(setting: $viewModel.paymentMethodRemoveLast)
SettingPickerView(setting: paymentMethodRedisplayBinding)
if viewModel.paymentMethodRedisplay == .enabled {
SettingPickerView(setting: $viewModel.paymentMethodAllowRedisplayFilters)
}
SettingPickerView(setting: $viewModel.allowsSetAsDefaultPM)
}
}.padding()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct CustomerSheetTestPlayground: View {
SettingPickerView(setting: $playgroundController.settings.paymentMethodRemove)
SettingPickerView(setting: $playgroundController.settings.paymentMethodRemoveLast)
SettingPickerView(setting: $playgroundController.settings.paymentMethodAllowRedisplayFilters)
SettingPickerView(setting: $playgroundController.settings.allowsSetAsDefaultPM)
SettingPickerView(setting: $playgroundController.settings.paymentMethodSyncDefault)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ class CustomerSheetTestPlaygroundController: ObservableObject {
case .allowVisa:
configuration.cardBrandAcceptance = .allowed(brands: [.visa])
}
configuration.allowsSetAsDefaultPM = settings.allowsSetAsDefaultPM == .on
return configuration
}

Expand Down Expand Up @@ -328,6 +327,7 @@ class CustomerSheetBackend {
"merchant_country_code": settings.merchantCountryCode.rawValue,
"customer_session_payment_method_remove": settings.paymentMethodRemove.rawValue,
"customer_session_payment_method_remove_last": settings.paymentMethodRemoveLast.rawValue,
"customer_session_payment_method_sync_default": settings.paymentMethodSyncDefault.rawValue,
] as [String: Any]

if let allowRedisplayValue = settings.paymentMethodAllowRedisplayFilters.arrayValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,20 @@ public struct CustomerSheetTestPlaygroundSettings: Codable, Equatable {
}
}

enum PaymentMethodSyncDefault: String, PickerEnum {
static let enumName: String = "PaymentMethodSyncDefault"

case enabled
case disabled
}

enum CardBrandAcceptance: String, PickerEnum {
static let enumName: String = "cardBrandAcceptance"
case all
case blockAmEx
case allowVisa
}

enum AllowsSetAsDefaultPM: String, PickerEnum {
static let enumName: String = "allowsSetAsDefaultPM"
case on
case off
}

var customerMode: CustomerMode
var customerId: String?
var customerKeyType: CustomerKeyType
Expand All @@ -174,8 +175,8 @@ public struct CustomerSheetTestPlaygroundSettings: Codable, Equatable {
var paymentMethodRemove: PaymentMethodRemove
var paymentMethodRemoveLast: PaymentMethodRemoveLast
var paymentMethodAllowRedisplayFilters: PaymentMethodAllowRedisplayFilters
var paymentMethodSyncDefault: PaymentMethodSyncDefault
var cardBrandAcceptance: CardBrandAcceptance
var allowsSetAsDefaultPM: AllowsSetAsDefaultPM

static func defaultValues() -> CustomerSheetTestPlaygroundSettings {
return CustomerSheetTestPlaygroundSettings(customerMode: .new,
Expand All @@ -197,8 +198,8 @@ public struct CustomerSheetTestPlaygroundSettings: Codable, Equatable {
paymentMethodRemove: .enabled,
paymentMethodRemoveLast: .enabled,
paymentMethodAllowRedisplayFilters: .always,
cardBrandAcceptance: .all,
allowsSetAsDefaultPM: .off)
paymentMethodSyncDefault: .disabled,
cardBrandAcceptance: .all)
}

var base64Data: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
}
}


enum PaymentMethodSetAsDefault: String, PickerEnum {
static let enumName: String = "PaymentMethodSetAsDefault"
case enabled
case disabled
}

enum DefaultBillingAddress: String, PickerEnum {
static var enumName: String { "Default billing address" }

Expand Down Expand Up @@ -446,12 +453,6 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
case allowVisa
}

enum AllowsSetAsDefaultPM: String, PickerEnum {
static let enumName: String = "allowsSetAsDefaultPM"
case on
case off
}

enum ConfigurationStyle: String, PickerEnum {
static let enumName: String = "Style"
case automatic
Expand Down Expand Up @@ -482,6 +483,7 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
var paymentMethodRemoveLast: PaymentMethodRemoveLast
var paymentMethodRedisplay: PaymentMethodRedisplay
var paymentMethodAllowRedisplayFilters: PaymentMethodAllowRedisplayFilters
var paymentMethodSetAsDefault: PaymentMethodSetAsDefault
var defaultBillingAddress: DefaultBillingAddress
var customEmail: String?
var linkPassthroughMode: LinkPassthroughMode
Expand All @@ -506,7 +508,6 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
var formSheetAction: FormSheetAction
var embeddedViewDisplaysMandateText: DisplaysMandateTextEnabled
var cardBrandAcceptance: CardBrandAcceptance
var allowsSetAsDefaultPM: AllowsSetAsDefaultPM

static func defaultValues() -> PaymentSheetTestPlaygroundSettings {
return PaymentSheetTestPlaygroundSettings(
Expand All @@ -531,6 +532,7 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
paymentMethodRemoveLast: .enabled,
paymentMethodRedisplay: .enabled,
paymentMethodAllowRedisplayFilters: .always,
paymentMethodSetAsDefault: .disabled,
defaultBillingAddress: .off,
customEmail: nil,
linkPassthroughMode: .passthrough,
Expand All @@ -553,8 +555,7 @@ struct PaymentSheetTestPlaygroundSettings: Codable, Equatable {
collectAddress: .automatic,
formSheetAction: .continue,
embeddedViewDisplaysMandateText: .on,
cardBrandAcceptance: .all,
allowsSetAsDefaultPM: .off)
cardBrandAcceptance: .all)
}

static let nsUserDefaultsKey = "PaymentSheetTestPlaygroundSettings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ class PlaygroundController: ObservableObject {
case .allowVisa:
configuration.cardBrandAcceptance = .allowed(brands: [.visa])
}
configuration.allowsSetAsDefaultPM = settings.allowsSetAsDefaultPM == .on

switch settings.style {
case .automatic:
Expand Down Expand Up @@ -281,7 +280,6 @@ class PlaygroundController: ObservableObject {
case .allowVisa:
configuration.cardBrandAcceptance = .allowed(brands: [.visa])
}
configuration.allowsSetAsDefaultPM = settings.allowsSetAsDefaultPM == .on
return configuration
}

Expand Down Expand Up @@ -639,6 +637,7 @@ extension PlaygroundController {
"customer_session_payment_method_remove": settings.paymentMethodRemove.rawValue,
"customer_session_payment_method_remove_last": settings.paymentMethodRemoveLast.rawValue,
"customer_session_payment_method_redisplay": settings.paymentMethodRedisplay.rawValue,
"customer_session_payment_method_set_as_default": settings.paymentMethodSetAsDefault.rawValue,
// "set_shipping_address": true // Uncomment to make server vend PI with shipping address populated
] as [String: Any]
if settingsToLoad.apmsEnabled == .off, let supportedPaymentMethods = settingsToLoad.supportedPaymentMethods, !supportedPaymentMethods.isEmpty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct CustomerSession: Equatable, Hashable {
return nil
}
let paymentMethodRemoveLast = mobilePaymentElementFeaturesDict["payment_method_remove_last"] as? String ?? "enabled"
let paymentMethodSetAsDefault = mobilePaymentElementFeaturesDict["payment_method_set_as_default"] as? String ?? "disabled"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised we don't have any tests for this class? If so can we file a run ticket to get some tests created for this class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a ticket: RUN_MOBILESDK-3836

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have tests. They are in STPElementsSessionTest.swift

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, it's a child of ElementsSession. Kinda hard to tell it's being tested might still be good to have standalone tests.


var allowRedisplayOverrideValue: STPPaymentMethodAllowRedisplay?
if let allowRedisplayOverride = mobilePaymentElementFeaturesDict["payment_method_save_allow_redisplay_override"] as? String {
Expand All @@ -52,7 +53,8 @@ struct CustomerSession: Equatable, Hashable {
features: MobilePaymentElementComponentFeature(paymentMethodSave: paymentMethodSave == "enabled",
paymentMethodRemove: paymentMethodRemove == "enabled",
paymentMethodRemoveLast: paymentMethodRemoveLast == "enabled",
paymentMethodSaveAllowRedisplayOverride: allowRedisplayOverrideValue))
paymentMethodSaveAllowRedisplayOverride: allowRedisplayOverrideValue,
paymentMethodSetAsDefault: paymentMethodSetAsDefault == "enabled"))
} else {
mobilePaymentElementComponent = MobilePaymentElementComponent(enabled: false, features: nil)
}
Expand All @@ -64,10 +66,11 @@ struct CustomerSession: Equatable, Hashable {
return nil
}
let paymentMethodRemoveLast = customerSheetFeaturesDict["payment_method_remove_last"] as? String ?? "enabled"

let paymentMethodSyncDefault = customerSheetFeaturesDict["payment_method_sync_default"] as? String ?? "disabled"
customerSheetComponent = CustomerSheetComponent(enabled: true,
features: CustomerSheetComponentFeature(paymentMethodRemove: paymentMethodRemove == "enabled",
paymentMethodRemoveLast: paymentMethodRemoveLast == "enabled"))
paymentMethodRemoveLast: paymentMethodRemoveLast == "enabled",
paymentMethodSyncDefault: paymentMethodSyncDefault == "enabled"))
} else {
customerSheetComponent = CustomerSheetComponent(enabled: false, features: nil)
}
Expand All @@ -94,6 +97,7 @@ struct MobilePaymentElementComponentFeature: Equatable, Hashable {
let paymentMethodRemove: Bool
let paymentMethodRemoveLast: Bool
let paymentMethodSaveAllowRedisplayOverride: STPPaymentMethodAllowRedisplay?
let paymentMethodSetAsDefault: Bool
}

struct CustomerSheetComponent: Equatable, Hashable {
Expand All @@ -104,4 +108,5 @@ struct CustomerSheetComponent: Equatable, Hashable {
struct CustomerSheetComponentFeature: Equatable, Hashable {
let paymentMethodRemove: Bool
let paymentMethodRemoveLast: Bool
let paymentMethodSyncDefault: Bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ extension STPElementsSession {
return customer?.customerSession.mobilePaymentElementComponent.features?.paymentMethodRemoveLast ?? true
}

var paymentMethodSetAsDefaultForPaymentSheet: Bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be in this PR, but we should probably rename xForPaymentSheet to xForMPE

return customer?.customerSession.mobilePaymentElementComponent.features?.paymentMethodSetAsDefault ?? false
}

func allowsRemovalOfPaymentMethodsForCustomerSheet() -> Bool {
var allowsRemovalOfPaymentMethods = false
if let customerSession = customer?.customerSession {
Expand All @@ -227,6 +231,10 @@ extension STPElementsSession {
return customer?.customerSession.customerSheetComponent.features?.paymentMethodRemoveLast ?? true
}

var paymentMethodSyncDefaultForCustomerSheet: Bool {
return customer?.customerSession.customerSheetComponent.features?.paymentMethodSyncDefault ?? false
}

var isLinkCardBrand: Bool {
linkSettings?.linkMode == .linkCardBrand
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ extension CustomerSessionAdapter {
return stripePaymentMethodId
}

func fetchSelectedPaymentOption(for customerId: String, customer: ElementsCustomer? = nil) -> CustomerPaymentOption? {
func fetchSelectedPaymentOption(for customerId: String, elementsSession: STPElementsSession? = nil) -> CustomerPaymentOption? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly surprised we don't have tests for this, can you mention this class in the JIRA ticket you create.

// if opted in to the "set as default" feature, try to get default payment method from elements session
if configuration.allowsSetAsDefaultPM {
guard let customer = customer,
if let elementsSession = elementsSession, elementsSession.paymentMethodSyncDefaultForCustomerSheet {
guard let customer = elementsSession.customer,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() else { return nil }
return CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class CustomerSavedPaymentMethodsCollectionViewController: UIViewController {
let showApplePay: Bool
let allowsRemovalOfLastSavedPaymentMethod: Bool
let paymentMethodRemove: Bool
let paymentMethodSyncDefault: Bool
let isTestMode: Bool
let allowsSetAsDefaultPM: Bool
}

/// Whether or not you can edit save payment methods by removing or updating them.
Expand Down Expand Up @@ -243,7 +243,7 @@ class CustomerSavedPaymentMethodsCollectionViewController: UIViewController {
delegate: CustomerSavedPaymentMethodsCollectionViewControllerDelegate? = nil
) {
// when opted into the set as default feature, only show payment methods that can be set as default (card, US bank account)
if configuration.allowsSetAsDefaultPM {
if configuration.paymentMethodSyncDefault {
self.savedPaymentMethods = savedPaymentMethods.filter{ savedPaymentMethod in CustomerSheet.supportedDefaultPaymentMethods.contains{paymentMethodType in
savedPaymentMethod.type == paymentMethodType}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
let configuration: CustomerSheet.Configuration
let customerSheetDataSource: CustomerSheetDataSource
let paymentMethodRemove: Bool
let paymentMethodSyncDefault: Bool
let allowsRemovalOfLastSavedPaymentMethod: Bool
let cbcEligible: Bool

Expand Down Expand Up @@ -64,11 +65,11 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
private var cachedClientSecret: String?

var showApplePay: Bool {
return isApplePayEnabled && !configuration.allowsSetAsDefaultPM
return isApplePayEnabled && !paymentMethodSyncDefault
}

var paymentMethodTypes: [PaymentSheet.PaymentMethodType] {
let supportedPaymentMethods = configuration.allowsSetAsDefaultPM ? CustomerSheet.supportedDefaultPaymentMethods : CustomerSheet.supportedPaymentMethods
let supportedPaymentMethods = paymentMethodSyncDefault ? CustomerSheet.supportedDefaultPaymentMethods : CustomerSheet.supportedPaymentMethods
let paymentMethodTypes = merchantSupportedPaymentMethodTypes.customerSheetSupportedPaymentMethodTypesForAdd(canCreateSetupIntents: canCreateSetupIntents, supportedPaymentMethods: supportedPaymentMethods)
return paymentMethodTypes.toPaymentSheetPaymentMethodTypes()
}
Expand Down Expand Up @@ -108,8 +109,8 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
showApplePay: showApplePay,
allowsRemovalOfLastSavedPaymentMethod: allowsRemovalOfLastSavedPaymentMethod,
paymentMethodRemove: paymentMethodRemove,
isTestMode: configuration.apiClient.isTestmode,
allowsSetAsDefaultPM: configuration.allowsSetAsDefaultPM
paymentMethodSyncDefault: paymentMethodSyncDefault,
isTestMode: configuration.apiClient.isTestmode
),
appearance: configuration.appearance,
cbcEligible: cbcEligible,
Expand Down Expand Up @@ -151,6 +152,7 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
customerSheetDataSource: CustomerSheetDataSource,
isApplePayEnabled: Bool,
paymentMethodRemove: Bool,
paymentMethodSyncDefault: Bool,
allowsRemovalOfLastSavedPaymentMethod: Bool,
cbcEligible: Bool,
csCompletion: CustomerSheet.CustomerSheetCompletion?,
Expand All @@ -163,12 +165,13 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
self.customerSheetDataSource = customerSheetDataSource
self.isApplePayEnabled = isApplePayEnabled
self.paymentMethodRemove = paymentMethodRemove
self.paymentMethodSyncDefault = paymentMethodSyncDefault
self.allowsRemovalOfLastSavedPaymentMethod = allowsRemovalOfLastSavedPaymentMethod
self.cbcEligible = cbcEligible
self.csCompletion = csCompletion
self.delegate = delegate

if Self.shouldShowPaymentMethodCarousel(savedPaymentMethods: savedPaymentMethods, showApplePay: isApplePayEnabled && !configuration.allowsSetAsDefaultPM) {
if Self.shouldShowPaymentMethodCarousel(savedPaymentMethods: savedPaymentMethods, showApplePay: isApplePayEnabled && !paymentMethodSyncDefault) {
self.mode = .selectingSaved
} else {
switch customerSheetDataSource.dataSource {
Expand Down Expand Up @@ -659,11 +662,11 @@ class CustomerSavedPaymentMethodsViewController: UIViewController {
mostRecentlyAddedPaymentMethod: mostRecentlyAddedPaymentMethod,
savedPaymentMethodsConfiguration: self.configuration,
configuration: .init(
showApplePay: isApplePayEnabled,
showApplePay: showApplePay,
allowsRemovalOfLastSavedPaymentMethod: allowsRemovalOfLastSavedPaymentMethod,
paymentMethodRemove: paymentMethodRemove,
isTestMode: configuration.apiClient.isTestmode,
allowsSetAsDefaultPM: configuration.allowsSetAsDefaultPM
paymentMethodSyncDefault: paymentMethodSyncDefault,
isTestMode: configuration.apiClient.isTestmode
),
appearance: configuration.appearance,
cbcEligible: cbcEligible,
Expand Down
Loading
Loading