From 0fa7ce286841bb7be9fceb0c1d749828369966ad Mon Sep 17 00:00:00 2001 From: Amy Date: Mon, 10 Jun 2024 13:36:06 -0400 Subject: [PATCH] Create helper method for protecting stripe payment method id --- .../PledgePaymentMethodsViewModel.swift | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/Library/ViewModels/PledgePaymentMethodsViewModel.swift b/Library/ViewModels/PledgePaymentMethodsViewModel.swift index 979989328c..8d8c0817c8 100644 --- a/Library/ViewModels/PledgePaymentMethodsViewModel.swift +++ b/Library/ViewModels/PledgePaymentMethodsViewModel.swift @@ -168,13 +168,8 @@ public final class PledgePaymentMethodsViewModel: PledgePaymentMethodsViewModelT .map { cellData, selectedCard -> PledgePaymentMethodsAndSelectionData in var selectedPaymentMethod: PaymentSourceSelected? - if let selectedCardId = selectedCard?.id { - if let stripeCardId = selectedCard?.stripeCardId { - selectedPaymentMethod = .savedCreditCard(selectedCardId, stripeCardId) - } else { - assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") - selectedPaymentMethod = .savedCreditCard(selectedCardId, "") - } + if let card = selectedCard { + selectedPaymentMethod = createSelectedPaymentMethod(from: card) } return PledgePaymentMethodsAndSelectionData( @@ -195,12 +190,7 @@ public final class PledgePaymentMethodsViewModel: PledgePaymentMethodsViewModelT updatedCardData.newPaymentMethods = newCards if let selectedCard = newSelectedCard { - if let stripeCardId = selectedCard.stripeCardId { - updatedCardData.selectedPaymentMethod = .savedCreditCard(selectedCard.id, stripeCardId) - } else { - assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") - updatedCardData.selectedPaymentMethod = .savedCreditCard(selectedCard.id, "") - } + updatedCardData.selectedPaymentMethod = createSelectedPaymentMethod(from: selectedCard) } let updatedPaymentMethodSelectionData = @@ -249,13 +239,7 @@ public final class PledgePaymentMethodsViewModel: PledgePaymentMethodsViewModelT var selectionUpdatedData = updatedData selectionUpdatedData.existingPaymentMethods = cellData(data.existingPaymentMethods, selecting: nil) selectionUpdatedData.newPaymentMethods = cellData(data.newPaymentMethods, selecting: card) - - if let stripeCardId = card.stripeCardId { - selectionUpdatedData.selectedPaymentMethod = .savedCreditCard(card.id, stripeCardId) - } else { - assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") - selectionUpdatedData.selectedPaymentMethod = .savedCreditCard(card.id, "") - } + selectionUpdatedData.selectedPaymentMethod = createSelectedPaymentMethod(from: card) return selectionUpdatedData } else if indexPath.row < paymentSheetPaymentMethodCount + paymentMethodCount { @@ -265,12 +249,7 @@ public final class PledgePaymentMethodsViewModel: PledgePaymentMethodsViewModelT var selectionUpdatedData = updatedData selectionUpdatedData.existingPaymentMethods = cellData(data.existingPaymentMethods, selecting: card) selectionUpdatedData.newPaymentMethods = cellData(data.newPaymentMethods, selecting: nil) - if let stripeCardId = card.stripeCardId { - selectionUpdatedData.selectedPaymentMethod = .savedCreditCard(card.id, stripeCardId) - } else { - assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") - selectionUpdatedData.selectedPaymentMethod = .savedCreditCard(card.id, "") - } + selectionUpdatedData.selectedPaymentMethod = createSelectedPaymentMethod(from: card) return selectionUpdatedData } @@ -555,14 +534,8 @@ private func pledgePaymentSheetMethodCellDataAndSelectedCardSetupIntent( updatePaymentMethodData.newPaymentMethods = data if newestPaymentSheetPaymentMethod.isEnabled { - if let stripeCardId = newestPaymentSheetPaymentMethod.card.stripeCardId { - updatePaymentMethodData - .selectedPaymentMethod = .savedCreditCard(newestPaymentSheetPaymentMethod.card.id, stripeCardId) - } else { - assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") - updatePaymentMethodData - .selectedPaymentMethod = .savedCreditCard(newestPaymentSheetPaymentMethod.card.id, "") - } + updatePaymentMethodData + .selectedPaymentMethod = createSelectedPaymentMethod(from: newestPaymentSheetPaymentMethod.card) } return updatePaymentMethodData @@ -660,3 +633,12 @@ private func cards( ) -> [UserCreditCards.CreditCard] { return cards.sorted { card1, _ in card1.id == backing?.paymentSource?.id } } + +private func createSelectedPaymentMethod(from card: UserCreditCards.CreditCard) -> PaymentSourceSelected { + guard let stripeCardId = card.stripeCardId, !stripeCardId.isEmpty else { + assert(false, "Expected stripeCardId to be set. Late pledges may fail if this value is missing.") + return .savedCreditCard(card.id, nil) + } + + return .savedCreditCard(card.id, stripeCardId) +}