Skip to content

Commit bd3450e

Browse files
committed
Wait for the print action to finish before dismissing the card payment flow.
1 parent 2a0a022 commit bd3450e

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

WooCommerce/Classes/ViewModels/CardPresentPayments/ReceiptActionCoordinator.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@ struct ReceiptActionCoordinator {
77
countryCode: String,
88
cardReaderModel: String?,
99
stores: StoresManager,
10-
analytics: Analytics) {
10+
analytics: Analytics) async {
1111
analytics.track(event: .InPersonPayments.receiptPrintTapped(countryCode: countryCode, cardReaderModel: cardReaderModel))
1212

13-
let action = ReceiptAction.print(order: order, parameters: params) { (result) in
14-
switch result {
15-
case .success:
16-
analytics.track(event: .InPersonPayments.receiptPrintSuccess(countryCode: countryCode, cardReaderModel: cardReaderModel))
17-
case .cancel:
18-
analytics.track(event: .InPersonPayments.receiptPrintCanceled(countryCode: countryCode, cardReaderModel: cardReaderModel))
19-
case .failure(let error):
20-
analytics.track(event: .InPersonPayments.receiptPrintFailed(error: error, countryCode: countryCode, cardReaderModel: cardReaderModel))
21-
DDLogError("⛔️ Failed to print receipt: \(error.localizedDescription)")
13+
await withCheckedContinuation { continuation in
14+
let action = ReceiptAction.print(order: order, parameters: params) { (result) in
15+
switch result {
16+
case .success:
17+
analytics.track(event: .InPersonPayments.receiptPrintSuccess(countryCode: countryCode, cardReaderModel: cardReaderModel))
18+
case .cancel:
19+
analytics.track(event: .InPersonPayments.receiptPrintCanceled(countryCode: countryCode, cardReaderModel: cardReaderModel))
20+
case .failure(let error):
21+
analytics.track(event: .InPersonPayments.receiptPrintFailed(error: error, countryCode: countryCode, cardReaderModel: cardReaderModel))
22+
DDLogError("⛔️ Failed to print receipt: \(error.localizedDescription)")
23+
}
24+
25+
continuation.resume()
2226
}
23-
}
2427

25-
stores.dispatch(action)
28+
Task { @MainActor in
29+
stores.dispatch(action)
30+
}
31+
}
2632
}
2733
}

WooCommerce/Classes/ViewModels/CardPresentPayments/ReceiptViewModel.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ final class ReceiptViewModel {
4848

4949
/// Prints the receipt
5050
func printReceipt() {
51-
ReceiptActionCoordinator.printReceipt(for: order,
52-
params: receipt,
53-
countryCode: countryCode,
54-
cardReaderModel: nil,
55-
stores: stores,
56-
analytics: ServiceLocator.analytics)
51+
Task { @MainActor in
52+
await ReceiptActionCoordinator.printReceipt(for: order,
53+
params: receipt,
54+
countryCode: countryCode,
55+
cardReaderModel: nil,
56+
stores: stores,
57+
analytics: ServiceLocator.analytics)
58+
}
5759
}
5860

5961
/// Returns a boolean that indicates whether email is supported for the app and device so that email UI is only displayed when it is supported.

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,18 @@ private extension CollectOrderPaymentUseCase {
440440
alertsPresenter.present(viewModel: paymentAlerts.success(printReceipt: { [order, configuration, weak self] in
441441
guard let self = self else { return }
442442

443-
// Inform about flow completion.
444-
onCompleted()
445-
446443
// Delegate print action
447-
ReceiptActionCoordinator.printReceipt(for: order,
448-
params: receiptParameters,
449-
countryCode: configuration.countryCode,
450-
cardReaderModel: self.connectedReader?.readerType.model,
451-
stores: self.stores,
452-
analytics: self.analytics)
453-
444+
Task { @MainActor in
445+
await ReceiptActionCoordinator.printReceipt(for: order,
446+
params: receiptParameters,
447+
countryCode: configuration.countryCode,
448+
cardReaderModel: self.connectedReader?.readerType.model,
449+
stores: self.stores,
450+
analytics: self.analytics)
451+
452+
// Inform about flow completion.
453+
onCompleted()
454+
}
454455
}, emailReceipt: { [order, analytics, paymentOrchestrator, configuration, weak self] in
455456
guard let self = self else { return }
456457

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,17 @@ private extension LegacyCollectOrderPaymentUseCase {
399399
alerts.success(printReceipt: { [order, configuration, weak self] in
400400
guard let self = self else { return }
401401

402-
// Inform about flow completion.
403-
onCompleted()
404-
405402
// Delegate print action
406-
ReceiptActionCoordinator.printReceipt(for: order,
407-
params: receiptParameters,
408-
countryCode: configuration.countryCode,
409-
cardReaderModel: self.connectedReader?.readerType.model,
410-
stores: self.stores,
411-
analytics: self.analytics)
412-
403+
Task { @MainActor in
404+
await ReceiptActionCoordinator.printReceipt(for: order,
405+
params: receiptParameters,
406+
countryCode: configuration.countryCode,
407+
cardReaderModel: self.connectedReader?.readerType.model,
408+
stores: self.stores,
409+
analytics: self.analytics)
410+
// Inform about flow completion.
411+
onCompleted()
412+
}
413413
}, emailReceipt: { [order, analytics, paymentOrchestrator, configuration, weak self] in
414414
guard let self = self else { return }
415415

0 commit comments

Comments
 (0)