Skip to content

Commit

Permalink
[PAY-2021] Error Backing Payment Card Being Unselected (#1748)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
msadoon committed Oct 13, 2022
1 parent d382b78 commit 63e5d5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
25 changes: 4 additions & 21 deletions Library/ViewModels/PledgePaymentMethodsViewModel.swift
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 4 additions & 5 deletions Library/ViewModels/PledgePaymentMethodsViewModelTests.swift
Expand Up @@ -437,7 +437,7 @@ final class PledgePaymentMethodsViewModelTests: TestCase {
image: UIImage(),
redactedCardNumber: "••••1234",
setupIntent: "seti_1LVlHO4VvJ2PtfhK43R6p7FI_secret_MEDiGbxfYVnHGsQy8v8TbZJTQhlNKLZ",
isSelected: false,
isSelected: true,
isEnabled: true
)

Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 63e5d5e

Please sign in to comment.