@@ -7,6 +7,10 @@ import Combine
77import enum Networking. DotcomError
88
99final 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
0 commit comments