Skip to content

Commit

Permalink
isCashPaymentInProgress flag to intercept card events
Browse files Browse the repository at this point in the history
Each time we tap the card on the reader, the .card payment state would override the current .cash payent state, changing the views. With this flag we filter card events when a cash transaction is in progress, so we can ignore them.

Allowing card payments while cash hasn’t been “marked as complete” yet, and disallowing doubles charges on the success screen.
  • Loading branch information
iamgabrielma committed Jan 8, 2025
1 parent ffbf8ae commit 26acf3e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions WooCommerce/Classes/POS/Models/PointOfSaleAggregateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PointOfSaleAggregateModel: ObservableObject, PointOfSaleAggregateModelProt
private let orderController: PointOfSaleOrderControllerProtocol
private let analytics: Analytics

private var isCashPaymentInProgress: Bool = false
private var startPaymentOnCardReaderConnection: AnyCancellable?
private var cardReaderDisconnection: AnyCancellable?

Expand Down Expand Up @@ -129,6 +130,7 @@ extension PointOfSaleAggregateModel {
}

func startNewCart() {
isCashPaymentInProgress = false
removeAllItemsFromCart()
orderController.clearOrder()
setStateForEditing()
Expand Down Expand Up @@ -200,10 +202,12 @@ extension PointOfSaleAggregateModel {
}

func startCashPayment() {
isCashPaymentInProgress = true
paymentState = .cash(.collectingCash)
}

func cancelCashPayment() {
isCashPaymentInProgress = false
paymentState = .card(.idle)
}

Expand Down Expand Up @@ -304,10 +308,17 @@ private extension PointOfSaleAggregateModel {
.assign(to: &$cardPresentPaymentInlineMessage)

cardPresentPaymentService.paymentEventPublisher
.compactMap { [weak self] paymentEvent in
guard let self else { return .none }
return PointOfSalePaymentState(from: paymentEvent,
using: presentationStyleDeterminerDependencies)
.compactMap { [weak self] paymentEvent -> PointOfSalePaymentState? in
guard let self else { return nil }

let newPaymentState = PointOfSalePaymentState(from: paymentEvent,
using: presentationStyleDeterminerDependencies)

if self.isCashPaymentInProgress, case .card = newPaymentState {
cardPresentPaymentService.cancelPayment()
return .cash(.paymentSuccess)
}
return newPaymentState
}
.assign(to: &$paymentState)

Expand Down

0 comments on commit 26acf3e

Please sign in to comment.