@@ -48,6 +48,10 @@ final class RefundConfirmationViewModel {
4848 )
4949 ]
5050
51+ /// Retains the use-case so it can perform all of its async tasks.
52+ ///
53+ private var submissionUseCase : RefundSubmissionProtocol ?
54+
5155 private let analytics : Analytics
5256
5357 init ( details: Details ,
@@ -62,37 +66,62 @@ final class RefundConfirmationViewModel {
6266
6367 /// Submit the refund.
6468 ///
65- func submit( onCompletion: @escaping ( Result < Void , Error > ) -> Void ) {
66- // Create refund object
69+ /// - Parameters:
70+ /// - rootViewController: view controller used to present in-person refund alerts if needed.
71+ /// - showInProgressUI: called when in-progress UI should be shown during refund submission. In-person refund submission does not show in-progress UI.
72+ /// - onCompletion: called when the refund submission completes.
73+ func submit( rootViewController: UIViewController ,
74+ showInProgressUI: @escaping ( ( ) -> Void ) ,
75+ onCompletion: @escaping ( Result < Void , Error > ) -> Void ) {
76+ // TODO: 6601 - remove Interac workaround when the API support is shipped.
77+ let isInterac : Bool = {
78+ switch details. charge? . paymentMethodDetails {
79+ case . some( . interacPresent) :
80+ return true
81+ default :
82+ return false
83+ }
84+ } ( )
85+ let automaticallyRefundsPayment = isInterac && ServiceLocator . featureFlagService. isFeatureFlagEnabled ( . canadaInPersonPayments) ?
86+ false : gatewaySupportsAutomaticRefunds ( )
87+
88+ // Creates refund object.
6789 let shippingLine = details. refundsShipping ? details. order. shippingLines. first : nil
6890 let fees = details. refundsFees ? details. order. fees : [ ]
6991 let useCase = RefundCreationUseCase ( amount: details. amount,
7092 reason: reasonForRefundCellViewModel. value,
71- automaticallyRefundsPayment: gatewaySupportsAutomaticRefunds ( ) ,
93+ automaticallyRefundsPayment: automaticallyRefundsPayment ,
7294 items: details. items,
7395 shippingLine: shippingLine,
7496 fees: fees,
7597 currencyFormatter: currencyFormatter)
7698 let refund = useCase. createRefund ( )
7799
78- // Submit it
79- let action = RefundAction . createRefund ( siteID: details. order. siteID, orderID: details. order. orderID, refund: refund) { [ weak self] _, error in
100+ // Submits refund.
101+ let submissionUseCase = RefundSubmissionUseCase ( siteID: details. order. siteID,
102+ details: . init( order: details. order,
103+ charge: details. charge,
104+ amount: details. amount) ,
105+ rootViewController: rootViewController,
106+ currencyFormatter: currencyFormatter,
107+ stores: actionProcessor,
108+ analytics: analytics)
109+ self . submissionUseCase = submissionUseCase
110+ submissionUseCase. submitRefund ( refund,
111+ showInProgressUI: showInProgressUI,
112+ onCompletion: { [ weak self] result in
80113 guard let self = self else { return }
81- if let error = error {
82- DDLogError ( " Error creating refund: \( refund) \n With Error: \( error) " )
83- self . trackCreateRefundRequestFailed ( error: error)
84- return onCompletion ( . failure( error) )
85- }
86114
87- // We don't care if the "update order" fails. We return .success() as the refund creation already succeeded.
88- self . updateOrder { _ in
89- onCompletion ( . success( ( ) ) )
115+ switch result {
116+ case . success:
117+ // We don't care if the "update order" fails. We return .success() as the refund creation already succeeded.
118+ self . updateOrder { _ in
119+ onCompletion ( . success( ( ) ) )
120+ }
121+ default :
122+ onCompletion ( result)
90123 }
91- self . trackCreateRefundRequestSuccess ( )
92- }
93-
94- actionProcessor. dispatch ( action)
95- trackCreateRefundRequest ( )
124+ } )
96125 }
97126
98127 /// Updates the order associated with the refund to reflect the latest refund status.
@@ -192,28 +221,6 @@ extension RefundConfirmationViewModel {
192221 func trackSummaryButtonTapped( ) {
193222 analytics. track ( event: WooAnalyticsEvent . IssueRefund. summaryButtonTapped ( orderID: details. order. orderID) )
194223 }
195-
196- /// Tracks when the create refund request is made.
197- ///
198- private func trackCreateRefundRequest( ) {
199- analytics. track ( event: WooAnalyticsEvent . IssueRefund. createRefund ( orderID: details. order. orderID,
200- fullyRefunded: details. amount == details. order. total,
201- method: . items,
202- gateway: details. order. paymentMethodID,
203- amount: details. amount) )
204- }
205-
206- /// Tracks when the create refund request succeeds.
207- ///
208- private func trackCreateRefundRequestSuccess( ) {
209- analytics. track ( event: WooAnalyticsEvent . IssueRefund. createRefundSuccess ( orderID: details. order. orderID) )
210- }
211-
212- /// Tracks when the create refund request fails.
213- ///
214- private func trackCreateRefundRequestFailed( error: Error ) {
215- analytics. track ( event: WooAnalyticsEvent . IssueRefund. createRefundFailed ( orderID: details. order. orderID, error: error) )
216- }
217224}
218225
219226// MARK: - Section and Row Types
0 commit comments