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 the default_payment_method field from elements session and use that as the default PM #4313

Merged
merged 45 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1403094
reading default_payment_method from back end?
joyceqin-stripe Nov 27, 2024
71c5516
revert accidental space changes;
joyceqin-stripe Nov 27, 2024
e3ecc2c
customersheet default from back end
joyceqin-stripe Nov 27, 2024
fb86faa
removed unused value
joyceqin-stripe Dec 2, 2024
b283446
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 2, 2024
9ed5ed2
remove unused property
joyceqin-stripe Dec 2, 2024
e689449
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 2, 2024
3c36c97
fix build issue
joyceqin-stripe Dec 2, 2024
c749e6e
Merge branch 'joyceqin-MOBILESDK-2799' of github.com:stripe/stripe-io…
joyceqin-stripe Dec 2, 2024
aac6ae1
fix test
joyceqin-stripe Dec 2, 2024
b25eb4b
merged with master
joyceqin-stripe Dec 4, 2024
5936f96
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 4, 2024
0fca0dc
check default pm against savedPaymentMethods instead of customer.paym…
joyceqin-stripe Dec 5, 2024
c226e09
removed alternateUpdatePaymentMethodNavigation, consolidated reading …
joyceqin-stripe Dec 5, 2024
4d146b6
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 5, 2024
f858536
remove alternateUpdatePaymentMethodNavigation flag and updated tests
joyceqin-stripe Dec 6, 2024
8d1c90b
update snapshot tests
joyceqin-stripe Dec 6, 2024
c70c6a1
update row button chevron condition
joyceqin-stripe Dec 6, 2024
d999b9e
remove removeonly mode, update tests
joyceqin-stripe Dec 6, 2024
ac5189a
fixing tests
joyceqin-stripe Dec 6, 2024
ffcf6e4
remove wait for chevron existence in tests for last card
joyceqin-stripe Dec 6, 2024
403fdcb
localizable string
joyceqin-stripe Dec 6, 2024
c8fbfef
edit a test
joyceqin-stripe Dec 6, 2024
2f515cd
merged with remove alternateupdatepaymentmethodnavigation flag
joyceqin-stripe Dec 6, 2024
0a92c87
remove removeOnly snapshots
joyceqin-stripe Dec 6, 2024
a89890c
Merge branch 'master' into joyceqin-remove-alternateupdatepaymentmeth…
joyceqin-stripe Dec 6, 2024
bd51b31
changelog
joyceqin-stripe Dec 6, 2024
3c69384
Merge branch 'master' into joyceqin-remove-alternateupdatepaymentmeth…
joyceqin-stripe Dec 6, 2024
f239d20
update changelog and tests
joyceqin-stripe Dec 6, 2024
5fad2d3
Merge branch 'joyceqin-remove-alternateupdatepaymentmethodnavigation-…
joyceqin-stripe Dec 6, 2024
4da3034
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 6, 2024
05407cc
Merge branch 'joyceqin-remove-alternateupdatepaymentmethodnavigation-…
joyceqin-stripe Dec 6, 2024
eaae456
Merge branch 'master' into joyceqin-remove-alternateupdatepaymentmeth…
joyceqin-stripe Dec 6, 2024
7022a11
sleep for 1 second to allow label animation to finish testRemovalOfSa…
joyceqin-stripe Dec 6, 2024
f5fbd58
Merge branch 'joyceqin-remove-alternateupdatepaymentmethodnavigation-…
joyceqin-stripe Dec 6, 2024
cb12138
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 6, 2024
79f2a94
Revert space change
joyceqin-stripe Dec 6, 2024
b592b55
fallback when opted in to set as default not local default but first …
joyceqin-stripe Dec 9, 2024
74c9db8
added tests
joyceqin-stripe Dec 9, 2024
dbc2beb
Merge branch 'joyceqin-MOBILESDK-2799' of github.com:stripe/stripe-io…
joyceqin-stripe Dec 9, 2024
40574a6
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 9, 2024
2e8b49e
no saved pms customersessionadapter test
joyceqin-stripe Dec 9, 2024
a2a37e6
removed explicit nil assignments when already implied
joyceqin-stripe Dec 9, 2024
0b6781f
Merge branch 'joyceqin-MOBILESDK-2799' of github.com:stripe/stripe-io…
joyceqin-stripe Dec 9, 2024
7d3f1c4
Merge branch 'master' into joyceqin-MOBILESDK-2799
joyceqin-stripe Dec 9, 2024
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 @@ -36,14 +36,17 @@ struct ElementsCustomer: Equatable, Hashable {
}

// Optional
// to test default payment methods reading from back end, hard-code a valid default payment method
// later, when API calls to get and update default payment method are available, that will no longer be needed
let defaultPaymentMethod = response["default_payment_method"] as? String
return ElementsCustomer(paymentMethods: paymentMethods, defaultPaymentMethod: defaultPaymentMethod, customerSession: customerSession)
}

static func getDefaultPaymentMethod(from customer: ElementsCustomer?) -> STPPaymentMethod? {
guard let customer = customer else { return nil }
return customer.paymentMethods.first { $0.stripeId == customer.defaultPaymentMethod }
func getDefaultOrFirstPaymentMethod() -> STPPaymentMethod? {
// if customer has a default payment method from the elements session, return the default payment method
let defaultSavedPaymentMethod = paymentMethods.first { $0.stripeId == defaultPaymentMethod }
if let defaultSavedPaymentMethod = defaultSavedPaymentMethod {
return defaultSavedPaymentMethod
}
// otherwise, return the first payment method from the customer's list of saved payment methods
return paymentMethods.first
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ extension CustomerSessionAdapter {
}

func fetchSelectedPaymentOption(for customerId: String, customer: ElementsCustomer? = nil) -> CustomerPaymentOption? {
guard configuration.allowsSetAsDefaultPM,
let customer = customer,
let defaultPaymentMethod = customer.defaultPaymentMethod else {
return CustomerPaymentOption.defaultPaymentMethod(for: customerId)
// 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,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() else { return nil }
return CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}

return CustomerPaymentOption.stripeId(defaultPaymentMethod)
return CustomerPaymentOption.defaultPaymentMethod(for: customerId)
}

func detachPaymentMethod(paymentMethodId: String) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ extension CustomerSheet {

var selectedPaymentOption: CustomerPaymentOption?

// get default payment method from elements session
if configuration.allowsSetAsDefaultPM,
let defaultPaymentMethod = ElementsCustomer.getDefaultPaymentMethod(from: elementsSession.customer) {
// if opted in to the "set as default" feature, try to get default payment method from elements session
if configuration.allowsSetAsDefaultPM {
guard let customer = elementsSession.customer,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() else { return nil }
selectedPaymentOption = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ extension EmbeddedPaymentElement {
isFlatCheckmarkStyle: configuration.appearance.embeddedPaymentElement.row.style == .flatWithCheckmark
)
let initialSelection: EmbeddedPaymentMethodsView.Selection? = {
// get default payment method from elements session
if configuration.allowsSetAsDefaultPM,
let defaultPaymentMethod = ElementsCustomer.getDefaultPaymentMethod(from: loadResult.elementsSession.customer) {
return .saved(paymentMethod: defaultPaymentMethod)
}

// Select the previous payment option
switch previousPaymentOption {
case .applePay:
Expand All @@ -60,7 +54,19 @@ extension EmbeddedPaymentElement {
}

// If there's no previous customer input, default to the customer's default or the first saved payment method, if any
let customerDefault = CustomerPaymentOption.defaultPaymentMethod(for: configuration.customer?.id)
var customerDefault: CustomerPaymentOption?
// if opted in to the "set as default" feature, try to get default payment method from elements session
if configuration.allowsSetAsDefaultPM {
if let defaultPaymentMethod = loadResult.elementsSession.customer?.getDefaultOrFirstPaymentMethod() {
customerDefault = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
else {
customerDefault = nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should be able to remove this, customerDefault is implied nil from it's definition. Same with other similar code paths in the PR.

}
else {
customerDefault = CustomerPaymentOption.defaultPaymentMethod(for: configuration.customer?.id)
}
switch customerDefault {
case .applePay:
return .applePay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ final class PaymentSheetLoader {
// Move default PM to front
if let customerID = configuration.customer?.id {
var defaultPaymentMethodOption: CustomerPaymentOption?
// get default payment method from elements session
if configuration.allowsSetAsDefaultPM,
let defaultPaymentMethod = ElementsCustomer.getDefaultPaymentMethod(from: elementsSession.customer) {
// if opted in to the "set as default" feature, try to get default payment method from elements session
if configuration.allowsSetAsDefaultPM {
guard let customer = elementsSession.customer,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() else { return [] }
defaultPaymentMethodOption = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,15 @@ class SavedPaymentOptionsViewController: UIViewController {
static func makeViewModels(savedPaymentMethods: [STPPaymentMethod], customerID: String?, showApplePay: Bool, showLink: Bool, allowsSetAsDefaultPM: Bool, customer: ElementsCustomer?) -> (defaultSelectedIndex: Int, viewModels: [Selection]) {
// Get the default
var defaultPaymentMethodOption: CustomerPaymentOption?
// get default payment method from elements session
if allowsSetAsDefaultPM,
let defaultPaymentMethod = ElementsCustomer.getDefaultPaymentMethod(from: customer) {
defaultPaymentMethodOption = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
// if opted in to the "set as default" feature, try to get default payment method from elements session
if allowsSetAsDefaultPM {
if let customer = customer,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() {
defaultPaymentMethodOption = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
else {
defaultPaymentMethodOption = nil
}
}
else {
defaultPaymentMethodOption = CustomerPaymentOption.defaultPaymentMethod(for: customerID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,6 @@ class PaymentSheetVerticalViewController: UIViewController, FlowControllerViewCo
if let selection {
return selection
}
// get default payment method from elements session
if configuration.allowsSetAsDefaultPM,
let defaultPaymentMethod = ElementsCustomer.getDefaultPaymentMethod(from: elementsSession.customer) {
return .saved(paymentMethod: defaultPaymentMethod)
}

switch previousPaymentOption {
case .applePay:
Expand Down Expand Up @@ -323,7 +318,20 @@ class PaymentSheetVerticalViewController: UIViewController, FlowControllerViewCo
}
}
// Default to the customer's default or the first saved payment method, if any
let customerDefault = CustomerPaymentOption.defaultPaymentMethod(for: configuration.customer?.id)
var customerDefault: CustomerPaymentOption?
// if opted in to the "set as default" feature, try to get default payment method from elements session
if configuration.allowsSetAsDefaultPM {
if let customer = elementsSession.customer,
let defaultPaymentMethod = customer.getDefaultOrFirstPaymentMethod() {
customerDefault = CustomerPaymentOption.stripeId(defaultPaymentMethod.stripeId)
}
else {
customerDefault = nil
}
}
else {
customerDefault = CustomerPaymentOption.defaultPaymentMethod(for: configuration.customer?.id)
}
switch customerDefault {
case .applePay:
return isFlowController ? .applePay : nil // Only default to Apple Pay in flow controller mode
Expand Down