Skip to content

Commit 46b2ac8

Browse files
committed
Integrate collectPaymentsUseCase on OrderDetails flow
1 parent 848f959 commit 46b2ac8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import Combine
77
import enum Networking.DotcomError
88

99
final class OrderDetailsViewModel {
10+
/// Retains the use-case so it can perform all of its async tasks.
11+
///
12+
private var collectPaymentsUseCase: CollectOrderPaymentUseCase?
13+
1014
private let paymentOrchestrator = PaymentCaptureOrchestrator()
1115
private let stores: StoresManager
1216

@@ -533,6 +537,33 @@ extension OrderDetailsViewModel {
533537
.eraseToAnyPublisher()
534538
}
535539

540+
/// Collects payments for the current order.
541+
/// Tries to connect to a reader if necesary.
542+
/// Handles receipt sharing.
543+
///
544+
func collectPayment(rootViewController: UIViewController, backButtonTitle: String, onCollect: @escaping (Result<Void, Error>) -> Void) {
545+
guard let paymentGateway = cardPresentPaymentGatewayAccounts.first else {
546+
return DDLogError("⛔️ Payment Gateway not found, can't collect payment.")
547+
}
548+
549+
let formattedTotal: String = {
550+
let currencyFormatter = CurrencyFormatter(currencySettings: ServiceLocator.currencySettings)
551+
let currencyCode = ServiceLocator.currencySettings.currencyCode
552+
let unit = ServiceLocator.currencySettings.symbol(from: currencyCode)
553+
return currencyFormatter.formatAmount(order.total, with: unit) ?? ""
554+
}()
555+
556+
collectPaymentsUseCase = CollectOrderPaymentUseCase(siteID: order.siteID,
557+
order: order,
558+
formattedAmount: formattedTotal,
559+
paymentGatewayAccount: paymentGateway,
560+
rootViewController: rootViewController)
561+
collectPaymentsUseCase?.collectPayment(backButtonTitle: backButtonTitle, onCollect: onCollect, onCompleted: { [weak self] in
562+
// Make sure we free all the resources
563+
self?.collectPaymentsUseCase = nil
564+
})
565+
}
566+
536567
/// We are passing the ReceiptParameters as part of the completon block
537568
/// We do so at this point for testing purposes.
538569
/// When we implement persistance, the receipt metadata would be persisted

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,17 @@ private extension OrderDetailsViewController {
711711
}
712712

713713
@objc private func collectPayment(at: IndexPath) {
714+
viewModel.collectPayment(rootViewController: self, backButtonTitle: Localization.Payments.backToOrder) { [weak self] result in
715+
guard let self = self else { return }
716+
// Refresh date & view once payment has been collected.
717+
if result.isSuccess {
718+
self.syncOrderAfterPaymentCollection {
719+
self.refreshCardPresentPaymentEligibility()
720+
}
721+
}
722+
}
723+
return
724+
714725
cardReaderAvailableSubscription = viewModel.cardReaderAvailable()
715726
.sink(
716727
receiveCompletion: { [weak self] result in

0 commit comments

Comments
 (0)