Skip to content

Commit 0bbee99

Browse files
committed
Trigger completion block after cancelling payment.
1 parent b860ee2 commit 0bbee99

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

WooCommerce/Classes/ViewRelated/Orders/Collect Payments/CollectOrderPaymentUseCase.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ private extension CollectOrderPaymentUseCase {
208208
alerts.readerIsReady(title: Localization.collectPaymentTitle(username: order.billingAddress?.firstName),
209209
amount: formattedAmount,
210210
onCancel: { [weak self] in
211-
self?.cancelPayment()
211+
self?.cancelPayment {
212+
onCompletion(.failure(CollectOrderPaymentUseCaseError.cancelled))
213+
}
212214
})
213215

214216
// Start collect payment process
@@ -219,7 +221,9 @@ private extension CollectOrderPaymentUseCase {
219221
onWaitingForInput: { [weak self] in
220222
// Request card input
221223
self?.alerts.tapOrInsertCard(onCancel: { [weak self] in
222-
self?.cancelPayment()
224+
self?.cancelPayment {
225+
onCompletion(.failure(CollectOrderPaymentUseCaseError.cancelled))
226+
}
223227
})
224228

225229
}, onProcessingMessage: { [weak self] in
@@ -293,12 +297,13 @@ private extension CollectOrderPaymentUseCase {
293297

294298
/// Cancels payment and record analytics.
295299
///
296-
func cancelPayment() {
300+
func cancelPayment(onCompleted: @escaping () -> ()) {
297301
paymentOrchestrator.cancelPayment { [weak self, analytics] _ in
298302
guard let self = self else { return }
299303
analytics.track(event: WooAnalyticsEvent.InPersonPayments.collectPaymentCanceled(forGatewayID: self.paymentGatewayAccount.gatewayID,
300304
countryCode: self.configuration.countryCode,
301305
cardReaderModel: self.connectedReader?.readerType.model ?? ""))
306+
onCompleted()
302307
}
303308
}
304309

@@ -391,6 +396,7 @@ private extension CollectOrderPaymentUseCase {
391396
struct UnknownEmailError: Error {}
392397
enum CollectOrderPaymentUseCaseError: Error {
393398
case cardReaderDisconnected
399+
case cancelled
394400
}
395401

396402
enum Localization {

WooCommerce/Classes/ViewRelated/Orders/Refund/RefundSubmissionUseCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private extension RefundSubmissionUseCase {
265265
paymentGatewayAccount: paymentGatewayAccount,
266266
onWaitingForInput: { [weak self] in
267267
// Requests card input.
268-
self?.alerts.tapOrInsertCard(onCancel: {
268+
self?.alerts.tapOrInsertCard(onCancel: { [weak self] in
269269
self?.cancelRefund()
270270
})
271271
}, onProcessingMessage: { [weak self] in

WooCommerce/WooCommerceTests/ViewModels/CardPresentPayments/CollectOrderPaymentUseCaseTests.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,26 @@ final class CollectOrderPaymentUseCaseTests: XCTestCase {
6060
XCTAssertEqual(eventProperties["plugin_slug"] as? String, Mocks.paymentGatewayAccount)
6161
}
6262

63-
func test_cancelling_readerIsReady_alert_tracks_collectPaymentCanceled_event() throws {
63+
func test_cancelling_readerIsReady_alert_triggers_onCompleted_and_tracks_collectPaymentCanceled_event_and_dispatches_cancel_action() throws {
64+
// Given
65+
assertEmpty(stores.receivedActions)
66+
6467
// When
6568
mockCardPresentPaymentActions()
66-
useCase.collectPayment(backButtonTitle: "", onCollect: { _ in }, onCompleted: {})
67-
alerts.cancelReaderIsReadyAlert?()
69+
let _: Void = waitFor { promise in
70+
self.useCase.collectPayment(backButtonTitle: "", onCollect: { _ in }, onCompleted: {
71+
promise(())
72+
})
73+
self.alerts.cancelReaderIsReadyAlert?()
74+
}
6875

6976
// Then
7077
let indexOfEvent = try XCTUnwrap(analyticsProvider.receivedEvents.firstIndex(where: { $0 == "card_present_collect_payment_canceled"}))
7178
let eventProperties = try XCTUnwrap(analyticsProvider.receivedProperties[indexOfEvent])
7279
XCTAssertEqual(eventProperties["card_reader_model"] as? String, Mocks.cardReaderModel)
7380
XCTAssertEqual(eventProperties["country"] as? String, "US")
7481
XCTAssertEqual(eventProperties["plugin_slug"] as? String, Mocks.paymentGatewayAccount)
75-
}
7682

77-
func test_cancelling_readerIsReady_alert_dispatches_cancel_action() throws {
78-
// Given
79-
assertEmpty(stores.receivedActions)
80-
81-
// When
82-
mockCardPresentPaymentActions()
83-
useCase.collectPayment(backButtonTitle: "", onCollect: { _ in }, onCompleted: {})
84-
alerts.cancelReaderIsReadyAlert?()
85-
86-
// Then
8783
let action = try XCTUnwrap(stores.receivedActions.last as? CardPresentPaymentAction)
8884
switch action {
8985
case .cancelPayment(onCompletion: _):

0 commit comments

Comments
 (0)