From 63e5d5e8bb2459dd48f7772a39d58ef21eaaf250 Mon Sep 17 00:00:00 2001 From: Mubarak Sadoon Date: Thu, 13 Oct 2022 14:48:03 -0400 Subject: [PATCH] [PAY-2021] Error Backing Payment Card Being Unselected (#1748) * removed conditional logic to unselect newly added payment sheet card if it is an errored backing. * corrected existing tests for errored backing logic * added corrections to test for last payment method added on errored backing. * removed comment on test * clean up --- .../PledgePaymentMethodsViewModel.swift | 25 +++---------------- .../PledgePaymentMethodsViewModelTests.swift | 9 +++---- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/Library/ViewModels/PledgePaymentMethodsViewModel.swift b/Library/ViewModels/PledgePaymentMethodsViewModel.swift index 809f40dc86..74b0d567c1 100644 --- a/Library/ViewModels/PledgePaymentMethodsViewModel.swift +++ b/Library/ViewModels/PledgePaymentMethodsViewModel.swift @@ -434,7 +434,7 @@ public final class PledgePaymentMethodsViewModel: PledgePaymentMethodsViewModelT private func pledgePaymentSheetMethodCellDataAndSelectedCardSetupIntent( with paymentMethodData: PledgePaymentMethodsAndSelectionData, - project: Project + project _: Project ) -> PledgePaymentMethodsAndSelectionData { // We know we have a new payment sheet card, so de-select all existing non-payment sheet cards. let preexistingCardDataUnselected: [PledgePaymentMethodCellData] = { @@ -491,28 +491,11 @@ private func pledgePaymentSheetMethodCellDataAndSelectedCardSetupIntent( return updatePaymentMethodData }() - // If there is no backing, simply select the first payment sheet card in the list - guard let backing = project.personalization.backing else { - return updatedDataWithSelection - } + return updatedDataWithSelection - /* - In keeping with existing logic inside `pledgePaymentMethodCellDataAndSelectedCard` , if newly added payment intent card is on an errored backing, then do not select any new/existing cards. + /** + Unlike the existing logic inside `pledgePaymentMethodCellDataAndSelectedCard`, we don't know the card id after its' been added, because new payment sheet cards only contain an image and a redacted card number. If we did we can ensure that an errored backing does not highlight the new card if it matches the payment source id on the backing. Right now this flow only handles the latest cards from the payment sheet, so we can simply select the first one added. If this code path is taken the method above is not called again for the same instance of this view controller, the flag is lazy initialized and doesn't change while this page is displayed. */ - if backing.status == .errored { - let updatePaymentMethodData = paymentMethodData - |> \.paymentMethodsCellData .~ preexistingCardDataUnselected - |> \.paymentSheetPaymentMethodsCellData .~ preexistingPaymentSheetCardDataUnselected - |> \.selectedCard .~ nil - |> \.selectedSetupIntent .~ nil - - return updatePaymentMethodData - } else { - /* - If we're working with a backing, select the first payment sheet card added - */ - return updatedDataWithSelection - } } private func pledgePaymentMethodCellDataAndSelectedCard( diff --git a/Library/ViewModels/PledgePaymentMethodsViewModelTests.swift b/Library/ViewModels/PledgePaymentMethodsViewModelTests.swift index 05824994c8..9a476acfaa 100644 --- a/Library/ViewModels/PledgePaymentMethodsViewModelTests.swift +++ b/Library/ViewModels/PledgePaymentMethodsViewModelTests.swift @@ -437,7 +437,7 @@ final class PledgePaymentMethodsViewModelTests: TestCase { image: UIImage(), redactedCardNumber: "••••1234", setupIntent: "seti_1LVlHO4VvJ2PtfhK43R6p7FI_secret_MEDiGbxfYVnHGsQy8v8TbZJTQhlNKLZ", - isSelected: false, + isSelected: true, isEnabled: true ) @@ -471,13 +471,12 @@ final class PledgePaymentMethodsViewModelTests: TestCase { ) self.reloadPaymentMethodsSelectedSetupIntent .assertValues( - [nil, nil, nil], - "Newly added payment sheet card not selected because of errored backing." + [nil, nil, expectedPaymentSheetPaymentMethodCard.setupIntent], + "Newly added payment sheet card still selected even on errored backing." ) self.reloadPaymentMethodsSelectedCard .assertValues( - [nil, nil, nil], - "No card to select after payment sheet card added because of errored backing." + [nil, nil, nil] ) self.reloadPaymentMethodsShouldReload.assertValues([true, true, true]) self.reloadPaymentMethodsIsLoading.assertValues([true, false, false])