Skip to content

Commit

Permalink
Create helper method for protecting stripe payment method id
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-at-kickstarter committed Jun 10, 2024
1 parent 7400f1d commit 0fa7ce2
Showing 1 changed file with 16 additions and 34 deletions.
50 changes: 16 additions & 34 deletions Library/ViewModels/PledgePaymentMethodsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 =
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

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

0 comments on commit 0fa7ce2

Please sign in to comment.