Skip to content

Commit d6ab1f2

Browse files
authored
Merge pull request #5597 from woocommerce/issue/5581-collect-payment-completion
2 parents a322b6d + b2c4583 commit d6ab1f2

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Amount/SimplePaymentsAmount.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ final class SimplePaymentsAmountHostingController: UIHostingController<SimplePay
1818
return presenter
1919
}()
2020

21-
init(viewModel: SimplePaymentsAmountViewModel, presentNoticePublisher: AnyPublisher<SimplePaymentsNotice, Never>) {
21+
/// Presents notices at the system level, currently uses the main tab-bar as source view controller.
22+
///
23+
private let systemNoticePresenter: NoticePresenter
24+
25+
init(viewModel: SimplePaymentsAmountViewModel,
26+
presentNoticePublisher: AnyPublisher<SimplePaymentsNotice, Never>,
27+
systemNoticePresenter: NoticePresenter = ServiceLocator.noticePresenter) {
28+
self.systemNoticePresenter = systemNoticePresenter
2229
super.init(rootView: SimplePaymentsAmount(viewModel: viewModel))
2330

2431
// Needed because a `SwiftUI` cannot be dismissed when being presented by a UIHostingController
@@ -32,6 +39,8 @@ final class SimplePaymentsAmountHostingController: UIHostingController<SimplePay
3239
.sink { [weak self] notice in
3340

3441
switch notice {
42+
case .completed:
43+
self?.systemNoticePresenter.enqueue(notice: .init(title: SimplePaymentsAmount.Localization.completed, feedbackType: .success))
3544
case .error(let description):
3645
self?.modalNoticePresenter.enqueue(notice: .init(title: description, feedbackType: .error))
3746
}
@@ -154,6 +163,7 @@ private extension SimplePaymentsAmount {
154163
static let title = NSLocalizedString("Take Payment", comment: "Title for the simple payments screen")
155164
static let instructions = NSLocalizedString("Enter Amount", comment: "Short instructions label in the simple payments screen")
156165
static let cancelTitle = NSLocalizedString("Cancel", comment: "Title for the button to cancel the simple payments screen")
166+
static let completed = NSLocalizedString("🎉 Order completed", comment: "Title for the button to cancel the simple payments screen")
157167

158168
static func buttonTitle() -> String {
159169
if ServiceLocator.featureFlagService.isFeatureFlagEnabled(.simplePaymentsPrototype) {
@@ -175,5 +185,6 @@ private extension SimplePaymentsAmount {
175185
/// Representation of possible notices that can be displayed
176186
///
177187
enum SimplePaymentsNotice: Equatable {
188+
case completed
178189
case error(String)
179190
}

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Method/SimplePaymentsMethodsViewModel.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ final class SimplePaymentsMethodsViewModel: ObservableObject {
105105

106106
if error == nil {
107107
onSuccess()
108+
self.presentNoticeSubject.send(.completed)
108109
} else {
109110
self.presentNoticeSubject.send(.error(Localization.markAsPaidError))
110111
}
@@ -137,13 +138,14 @@ final class SimplePaymentsMethodsViewModel: ObservableObject {
137138
paymentGatewayAccount: paymentGateway,
138139
rootViewController: rootViewController)
139140
collectPaymentsUseCase?.collectPayment(onCollect: { _ in
140-
print("On collect does nothing for now...")
141+
/* No op. */
141142
}, onCompleted: { [weak self] in
142-
// TODO: Show success notice
143-
144143
// Inform success to consumer
145144
onSuccess()
146145

146+
// Sent notice request
147+
self?.presentNoticeSubject.send(.completed)
148+
147149
// Make sure we free all the resources
148150
self?.collectPaymentsUseCase = nil
149151
})

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsAmountViewModelTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ final class SimplePaymentsAmountViewModelTests: XCTestCase {
300300
switch intent {
301301
case .error:
302302
promise(true)
303+
case .completed:
304+
promise(false)
303305
}
304306
}
305307
.store(in: &self.subscriptions)

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsMethodsViewModelTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ final class SimplePaymentsMethodsViewModelTests: XCTestCase {
8888
XCTAssertTrue(onSuccessInvoked)
8989
}
9090

91+
func test_view_model_attempts_completed_notice_presentation_when_marking_an_order_as_paid() {
92+
// Given
93+
let stores = MockStoresManager(sessionManager: .testingInstance)
94+
let noticeSubject = PassthroughSubject<SimplePaymentsNotice, Never>()
95+
let viewModel = SimplePaymentsMethodsViewModel(formattedTotal: "$12.00", presentNoticeSubject: noticeSubject, stores: stores)
96+
stores.whenReceivingAction(ofType: OrderAction.self) { action in
97+
switch action {
98+
case let .updateOrderStatus(_, _, _, onCompletion):
99+
onCompletion(nil)
100+
default:
101+
XCTFail("Unexpected action: \(action)")
102+
}
103+
}
104+
105+
// When
106+
let receivedCompleted: Bool = waitFor { promise in
107+
noticeSubject.sink { intent in
108+
switch intent {
109+
case .error:
110+
promise(false)
111+
case .completed:
112+
promise(true)
113+
}
114+
}
115+
.store(in: &self.subscriptions)
116+
viewModel.markOrderAsPaid(onSuccess: {})
117+
}
118+
119+
// Then
120+
XCTAssertTrue(receivedCompleted)
121+
}
122+
91123
func test_view_model_attempts_error_notice_presentation_when_failing_to_mark_order_as_paid() {
92124
// Given
93125
let stores = MockStoresManager(sessionManager: .testingInstance)
@@ -108,6 +140,8 @@ final class SimplePaymentsMethodsViewModelTests: XCTestCase {
108140
switch intent {
109141
case .error:
110142
promise(true)
143+
case .completed:
144+
promise(false)
111145
}
112146
}
113147
.store(in: &self.subscriptions)

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsSummaryViewModelTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ final class SimplePaymentsSummaryViewModelTests: XCTestCase {
133133
switch intent {
134134
case .error:
135135
promise(true)
136+
case .completed:
137+
promise(false)
136138
}
137139
}
138140
.store(in: &self.subscriptions)

0 commit comments

Comments
 (0)