diff --git a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentMethodType.swift b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentMethodType.swift index a2e9639492e..798a3b6c231 100644 --- a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentMethodType.swift +++ b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentMethodType.swift @@ -191,6 +191,15 @@ extension PaymentSheet { configuration.isEligibleForBankTab } + func insertAtBankTabPosition(paymentMethodType: PaymentMethodType) { + let bankTabPosition = elementsSession.orderedPaymentMethodTypes.firstIndex(of: .link) + if let bankTabPosition, recommendedPaymentMethodTypes.indices.contains(bankTabPosition) { + recommendedPaymentMethodTypes.insert(paymentMethodType, at: bankTabPosition) + } else { + recommendedPaymentMethodTypes.append(paymentMethodType) + } + } + if eligibleForInstantDebits { let availabilityStatus = configurationSatisfiesRequirements( requirements: [.financialConnectionsSDK], @@ -198,7 +207,7 @@ extension PaymentSheet { intent: intent ) if availabilityStatus == .supported { - recommendedPaymentMethodTypes.append(.instantDebits) + insertAtBankTabPosition(paymentMethodType: .instantDebits) } // Else if here so we don't show both Instant Debits and Link Card Brand together. } else if eligibleForLinkCardBrand { @@ -208,7 +217,7 @@ extension PaymentSheet { intent: intent ) if availabilityStatus == .supported { - recommendedPaymentMethodTypes.append(.linkCardBrand) + insertAtBankTabPosition(paymentMethodType: .linkCardBrand) } } diff --git a/StripePaymentSheet/StripePaymentSheetTests/PaymentSheet/PaymentSheetPaymentMethodTypeTest.swift b/StripePaymentSheet/StripePaymentSheetTests/PaymentSheet/PaymentSheetPaymentMethodTypeTest.swift index 7ff429589a8..c32ed218c03 100644 --- a/StripePaymentSheet/StripePaymentSheetTests/PaymentSheet/PaymentSheetPaymentMethodTypeTest.swift +++ b/StripePaymentSheet/StripePaymentSheetTests/PaymentSheet/PaymentSheetPaymentMethodTypeTest.swift @@ -409,6 +409,36 @@ class PaymentSheetPaymentMethodTypeTest: XCTestCase { XCTAssertEqual(types, [.stripe(.card)]) } + func testPaymentMethodTypesInstantDebits() { + let intent = Intent._testPaymentIntent(paymentMethodTypes: [.link]) + let configuration = PaymentSheet.Configuration() + let types = PaymentSheet.PaymentMethodType.filteredPaymentMethodTypes( + from: intent, + elementsSession: ._testValue( + intent: intent, + linkMode: .linkCardBrand, + linkFundingSources: [.card, .bankAccount] + ), + configuration: configuration + ) + XCTAssertEqual(types, [.instantDebits]) + } + + func testPaymentMethodTypesInstantDebitsWithOrderedPaymentMethodTypes() { + let intent = Intent._testPaymentIntent(paymentMethodTypes: [.card, .cashApp, .amazonPay, .link, .klarna]) + var configuration = PaymentSheet.Configuration() + configuration.returnURL = "http://return-to-url" + let types = PaymentSheet.PaymentMethodType.filteredPaymentMethodTypes( + from: intent, + elementsSession: ._testValue( + intent: intent, + linkFundingSources: [.bankAccount] + ), + configuration: configuration + ) + XCTAssertEqual(types, [.stripe(.card), .stripe(.cashApp), .stripe(.amazonPay), .instantDebits, .stripe(.klarna)]) + } + func testPaymentMethodTypesLinkCardBrand() { let intent = Intent._testPaymentIntent(paymentMethodTypes: [.card]) let configuration = PaymentSheet.Configuration() @@ -424,6 +454,22 @@ class PaymentSheetPaymentMethodTypeTest: XCTestCase { XCTAssertEqual(types, [.stripe(.card), .linkCardBrand]) } + func testPaymentMethodTypesLinkCardBrandWithOrderedPaymentMethodTypes() { + let intent = Intent._testPaymentIntent(paymentMethodTypes: [.cashApp, .amazonPay, .card, .klarna]) + var configuration = PaymentSheet.Configuration() + configuration.returnURL = "http://return-to-url" + let types = PaymentSheet.PaymentMethodType.filteredPaymentMethodTypes( + from: intent, + elementsSession: ._testValue( + intent: intent, + linkMode: .linkCardBrand, + linkFundingSources: [.card, .bankAccount] + ), + configuration: configuration + ) + XCTAssertEqual(types, [.stripe(.cashApp), .stripe(.amazonPay), .stripe(.card), .stripe(.klarna), .linkCardBrand]) + } + func testPaymentMethodTypesLinkCardBrand_noDefaults() { let intent = Intent._testPaymentIntent(paymentMethodTypes: [.card]) var configuration = PaymentSheet.Configuration()