Skip to content

Commit 3c63d8c

Browse files
authored
Merge pull request #8690 from woocommerce/issue/7757-print-receipt-not-showed
[Mobile Payments] Receipt Printing Controller is not shown
2 parents 1e6b5b2 + 3e0a813 commit 3c63d8c

File tree

6 files changed

+108
-86
lines changed

6 files changed

+108
-86
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
@@ -442,17 +442,18 @@ private extension CollectOrderPaymentUseCase {
442442
alertsPresenter.present(viewModel: paymentAlerts.success(printReceipt: { [order, configuration, weak self] in
443443
guard let self = self else { return }
444444

445-
// Inform about flow completion.
446-
onCompleted()
447-
448445
// Delegate print action
449-
ReceiptActionCoordinator.printReceipt(for: order,
450-
params: receiptParameters,
451-
countryCode: configuration.countryCode,
452-
cardReaderModel: self.connectedReader?.readerType.model,
453-
stores: self.stores,
454-
analytics: self.analytics)
455-
446+
Task { @MainActor in
447+
await 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+
454+
// Inform about flow completion.
455+
onCompleted()
456+
}
456457
}, emailReceipt: { [order, analytics, paymentOrchestrator, configuration, weak self] in
457458
guard let self = self else { return }
458459

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

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

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

WooCommerce/WooCommerceTests/ViewModels/CardPresentPayments/LegacyCollectOrderPaymentUseCaseTests.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,6 @@ final class LegacyCollectOrderPaymentUseCaseTests: XCTestCase {
110110
}
111111

112112
// MARK: Success alert actions
113-
114-
func test_printing_receipt_from_collectPayment_success_alert_tracks_receiptPrintTapped_event() throws {
115-
// Given
116-
let intent = PaymentIntent.fake().copy(charges: [.fake().copy(paymentMethod: .cardPresent(details: .fake()))])
117-
mockSuccessfulCardPresentPaymentActions(intent: intent)
118-
119-
// When
120-
waitFor { promise in
121-
self.useCase.collectPayment(onCollect: { _ in
122-
promise(())
123-
}, onCancel: {}, onCompleted: {})
124-
}
125-
alerts.printReceiptFromSuccessAlert?()
126-
127-
// Then
128-
let indexOfEvent = try XCTUnwrap(analyticsProvider.receivedEvents.firstIndex(where: { $0 == "receipt_print_tapped"}))
129-
let eventProperties = try XCTUnwrap(analyticsProvider.receivedProperties[indexOfEvent])
130-
XCTAssertEqual(eventProperties["card_reader_model"] as? String, Mocks.cardReaderModel)
131-
XCTAssertEqual(eventProperties["country"] as? String, "US")
132-
}
133-
134113
func test_emailing_receipt_from_collectPayment_success_alert_tracks_receiptEmailTapped_event() throws {
135114
// Given
136115
let intent = PaymentIntent.fake().copy(charges: [.fake().copy(paymentMethod: .cardPresent(details: .fake()))])

WooCommerce/WooCommerceTests/ViewRelated/CardPresentPayments/ReceiptActionCoordinatorTests.swift

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,39 @@ import Hardware
66
@testable import WooCommerce
77

88
final class ReceiptActionCoordinatorTests: XCTestCase {
9-
func test_printReceipt_logs_receiptPrintTapped_analyticEvent() throws {
9+
private var storesManager: MockStoresManager!
10+
11+
override func setUp() {
12+
super.setUp()
13+
14+
storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
15+
}
16+
17+
override func tearDown() {
18+
storesManager = nil
19+
}
20+
21+
func test_printReceipt_logs_receiptPrintTapped_analyticEvent() async throws {
1022
// Given
1123
let analytics = MockAnalyticsProvider()
1224
let order = MockOrders().makeOrder()
1325
let params = CardPresentReceiptParameters.makeParams()
1426

27+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
28+
switch action {
29+
case let .print(_, _, completion):
30+
completion(.success)
31+
default:
32+
break
33+
}
34+
}
35+
1536
// When
16-
ReceiptActionCoordinator.printReceipt(for: order,
37+
await ReceiptActionCoordinator.printReceipt(for: order,
1738
params: params,
1839
countryCode: "CA",
1940
cardReaderModel: "WISEPAD_3",
20-
stores: ServiceLocator.stores,
41+
stores: storesManager,
2142
analytics: WooAnalytics(analyticsProvider: analytics))
2243

2344
// Then
@@ -27,18 +48,26 @@ final class ReceiptActionCoordinatorTests: XCTestCase {
2748
XCTAssertEqual(eventProperties["country"] as? String, "CA")
2849
}
2950

30-
func test_printReceipt_sends_print_receiptAction() throws {
51+
func test_printReceipt_sends_print_receiptAction() async throws {
3152
// Given
3253
let order = MockOrders().makeOrder()
3354
let params = CardPresentReceiptParameters.makeParams()
3455

35-
let storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
3656
storesManager.reset()
3757

3858
assertEmpty(storesManager.receivedActions)
3959

60+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
61+
switch action {
62+
case let .print(_, _, completion):
63+
completion(.success)
64+
default:
65+
break
66+
}
67+
}
68+
4069
// When
41-
ReceiptActionCoordinator.printReceipt(for: order,
70+
await ReceiptActionCoordinator.printReceipt(for: order,
4271
params: params,
4372
countryCode: "CA",
4473
cardReaderModel: nil,
@@ -57,48 +86,53 @@ final class ReceiptActionCoordinatorTests: XCTestCase {
5786
}
5887
}
5988

60-
func test_printReceipt_success_logs_receiptPrintSuccess_analyticEvent() throws {
61-
try assertAnalyticLogged(.receiptPrintSuccess, for: .success)
89+
func test_printReceipt_success_logs_receiptPrintSuccess_analyticEvent() async throws {
90+
try await assertAnalyticLogged(.receiptPrintSuccess, for: .success)
6291
}
6392

64-
func test_printReceipt_cancel_logs_receiptPrintCanceled_analyticEvent() throws {
65-
try assertAnalyticLogged(.receiptPrintCanceled, for: .cancel)
93+
func test_printReceipt_cancel_logs_receiptPrintCanceled_analyticEvent() async throws {
94+
try await assertAnalyticLogged(.receiptPrintCanceled, for: .cancel)
6695
}
6796

68-
func test_printReceipt_fail_logs_receiptPrintFailed_analyticEvent() throws {
97+
func test_printReceipt_fail_logs_receiptPrintFailed_analyticEvent() async throws {
6998
let error = NSError(domain: "errordomain", code: 123, userInfo: nil)
70-
try assertAnalyticLogged(.receiptPrintFailed, for: .failure(error))
99+
try await assertAnalyticLogged(.receiptPrintFailed, for: .failure(error))
71100
}
72101
}
73102

74103
extension ReceiptActionCoordinatorTests {
75-
func assertAnalyticLogged(_ analytic: WooAnalyticsStat, for printingResult: PrintingResult) throws {
104+
func assertAnalyticLogged(_ analytic: WooAnalyticsStat, for printingResult: PrintingResult) async throws {
76105
// Given
77106
let order = MockOrders().makeOrder()
78107
let params = CardPresentReceiptParameters.makeParams()
108+
let countryCode = "CA"
109+
let cardReaderModel = "test_reader"
110+
79111

80-
let storesManager = MockStoresManager(sessionManager: .makeForTesting(authenticated: true))
81112
let analytics = MockAnalyticsProvider()
82113

114+
storesManager.whenReceivingAction(ofType: ReceiptAction.self) { action in
115+
switch action {
116+
case let .print(_, _, completion):
117+
completion(printingResult)
118+
default:
119+
break
120+
}
121+
}
122+
83123
// When
84-
ReceiptActionCoordinator.printReceipt(for: order,
124+
await ReceiptActionCoordinator.printReceipt(for: order,
85125
params: params,
86126
countryCode: "CA",
87-
cardReaderModel: nil,
127+
cardReaderModel: cardReaderModel,
88128
stores: storesManager,
89129
analytics: WooAnalytics(analyticsProvider: analytics))
90130

91-
//Then
92-
let action = try XCTUnwrap(storesManager.receivedActions.first as? ReceiptAction)
93-
switch action {
94-
case .print(order: _, parameters: _, let completion):
95-
completion(printingResult)
96-
97-
let receivedEvents = analytics.receivedEvents
98-
XCTAssert(receivedEvents.contains(analytic.rawValue))
99-
default:
100-
XCTFail("Print Receipt failed to dispatch .print action")
101-
}
131+
// Then
132+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == analytic.rawValue}))
133+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
134+
XCTAssertEqual(eventProperties["card_reader_model"] as? String, cardReaderModel)
135+
XCTAssertEqual(eventProperties["country"] as? String, countryCode)
102136
}
103137
}
104138

0 commit comments

Comments
 (0)