Skip to content

Commit 6542fcb

Browse files
committed
Pass cancellation request through orchestrator, restore pass there too
1 parent c554def commit 6542fcb

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

WooCommerce/Classes/ViewModels/CardPresentPayments/CardPresentModalTapCard.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ final class CardPresentModalTapCard: CardPresentPaymentsModalViewModel {
1010
/// Charge amount
1111
private let amount: String
1212

13+
/// Cancellation callback
14+
private let onCancel: () -> Void
15+
1316
let textMode: PaymentsModalTextMode = .fullInfo
1417
let actionsMode: PaymentsModalActionsMode = .secondaryOnlyAction
1518

@@ -33,22 +36,20 @@ final class CardPresentModalTapCard: CardPresentPaymentsModalViewModel {
3336

3437
let bottomSubtitle: String? = Localization.tapInsertOrSwipe
3538

36-
init(name: String, amount: String) {
39+
init(name: String, amount: String, onCancel: @escaping () -> Void) {
3740
self.name = name
3841
self.amount = amount
42+
self.onCancel = onCancel
3943
}
4044

4145
func didTapPrimaryButton(in viewController: UIViewController?) {
4246
//
4347
}
4448

4549
func didTapSecondaryButton(in viewController: UIViewController?) {
46-
ServiceLocator.analytics.track(.collectPaymentCanceled)
47-
let action = CardPresentPaymentAction.cancelPayment(onCompletion: nil)
48-
49-
ServiceLocator.stores.dispatch(action)
50-
51-
viewController?.dismiss(animated: true, completion: nil)
50+
viewController?.dismiss(animated: true, completion: { [weak self] in
51+
self?.onCancel()
52+
})
5253
}
5354

5455
func didTapAuxiliaryButton(in viewController: UIViewController?) {

WooCommerce/Classes/ViewModels/CardPresentPayments/PaymentCaptureOrchestrator.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ final class PaymentCaptureOrchestrator {
7777
}
7878

7979
func cancelPayment(onCompletion: @escaping (Result<Void, Error>) -> Void) {
80-
let action = CardPresentPaymentAction.cancelPayment(onCompletion: onCompletion)
80+
let action = CardPresentPaymentAction.cancelPayment() { [weak self] result in
81+
self?.allowPassPresentation()
82+
onCompletion(result)
83+
}
8184
ServiceLocator.stores.dispatch(action)
8285
}
8386

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsPaymentAlerts.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ final class OrderDetailsPaymentAlerts {
4848
presentViewModel(viewModel: viewModel)
4949
}
5050

51-
func tapOrInsertCard() {
52-
let viewModel = tapOrInsert()
51+
func tapOrInsertCard(onCancel: @escaping () -> Void) {
52+
let viewModel = tapOrInsert(onCancel: onCancel)
5353
presentViewModel(viewModel: viewModel)
5454
}
5555

@@ -89,8 +89,8 @@ private extension OrderDetailsPaymentAlerts {
8989
CardPresentModalReaderIsReady(name: name, amount: amount)
9090
}
9191

92-
func tapOrInsert() -> CardPresentPaymentsModalViewModel {
93-
CardPresentModalTapCard(name: name, amount: amount)
92+
func tapOrInsert(onCancel: @escaping () -> Void) -> CardPresentPaymentsModalViewModel {
93+
CardPresentModalTapCard(name: name, amount: amount, onCancel: onCancel)
9494
}
9595

9696
func remove() -> CardPresentPaymentsModalViewModel {

WooCommerce/Classes/ViewRelated/Orders/Order Details/OrderDetailsViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,11 @@ private extension OrderDetailsViewController {
728728

729729
ServiceLocator.analytics.track(.collectPaymentTapped)
730730
viewModel.collectPayment { [weak self] readerEventMessage in
731-
self?.paymentAlerts.tapOrInsertCard()
731+
self?.paymentAlerts.tapOrInsertCard(onCancel: {
732+
self?.viewModel.cancelPayment(onCompletion: { _ in
733+
ServiceLocator.analytics.track(.collectPaymentCanceled)
734+
})
735+
})
732736
} onClearMessage: { [weak self] in
733737
self?.paymentAlerts.removeCard()
734738
} onProcessingMessage: { [weak self] in

WooCommerce/WooCommerceTests/ViewModels/CardPresentPayments/CardPresentModalTapCardTests.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ import TestKit
55

66
final class CardPresentModalTapCardTests: XCTestCase {
77
private var viewModel: CardPresentModalTapCard!
8+
private var closures: Closures!
89

910
override func setUp() {
1011
super.setUp()
11-
viewModel = CardPresentModalTapCard(name: Expectations.name, amount: Expectations.amount)
12+
closures = Closures()
13+
viewModel = CardPresentModalTapCard(
14+
name: Expectations.name,
15+
amount: Expectations.amount,
16+
onCancel: closures.onCancel()
17+
)
1218
}
1319

1420
override func tearDown() {
1521
viewModel = nil
22+
closures = nil
1623
super.tearDown()
1724
}
1825

@@ -48,33 +55,26 @@ final class CardPresentModalTapCardTests: XCTestCase {
4855
XCTAssertNotNil(viewModel.bottomSubtitle)
4956
}
5057

51-
func test_secondary_button_dispatched_cancel_action() throws {
52-
let storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
53-
storesManager.reset()
54-
55-
ServiceLocator.setStores(storesManager)
56-
57-
assertEmpty(storesManager.receivedActions)
58-
59-
viewModel.didTapSecondaryButton(in: nil)
60-
61-
XCTAssertEqual(storesManager.receivedActions.count, 1)
62-
63-
let action = try XCTUnwrap(storesManager.receivedActions.first as? CardPresentPaymentAction)
64-
switch action {
65-
case .cancelPayment(onCompletion: _):
66-
XCTAssertTrue(true)
67-
default:
68-
XCTFail("Primary button failed to dispatch .cancelPayment action")
69-
}
58+
func test_secondary_button_calls_closure() throws {
59+
viewModel.didTapSecondaryButton(in: UIViewController())
60+
XCTAssertTrue(closures.didTapCancel)
7061
}
7162
}
7263

73-
7464
private extension CardPresentModalTapCardTests {
7565
enum Expectations {
7666
static var name = "name"
7767
static var amount = "amount"
7868
static var image = UIImage.cardPresentImage
7969
}
8070
}
71+
72+
private final class Closures {
73+
var didTapCancel = false
74+
75+
func onCancel() -> () -> Void {
76+
return { [weak self] in
77+
self?.didTapCancel = true
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)