@@ -25,6 +25,10 @@ final class SimplePaymentsSummaryViewModel: ObservableObject {
2525 ///
2626 @Published var enableTaxes : Bool = false
2727
28+ /// Defines if a loading indicator should be shown.
29+ ///
30+ @Published private( set) var showLoadingIndicator = false
31+
2832 /// Total to charge. With or without taxes.
2933 ///
3034 var total : String {
@@ -45,6 +49,22 @@ final class SimplePaymentsSummaryViewModel: ObservableObject {
4549 ///
4650 private let currencyFormatter : CurrencyFormatter
4751
52+ /// Store ID
53+ ///
54+ private let siteID : Int64
55+
56+ /// Order ID to update.
57+ ///
58+ private let orderID : Int64
59+
60+ /// Fee ID to update.
61+ ///
62+ private let feeID : Int64
63+
64+ /// Stores Manager.
65+ ///
66+ private let stores : StoresManager
67+
4868 /// ViewModel for the edit order note view.
4969 ///
5070 lazy private( set) var noteViewModel = SimplePaymentsNoteViewModel ( )
@@ -53,8 +73,16 @@ final class SimplePaymentsSummaryViewModel: ObservableObject {
5373 totalWithTaxes: String ,
5474 taxAmount: String ,
5575 noteContent: String ? = nil ,
56- currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ) {
76+ siteID: Int64 = 0 ,
77+ orderID: Int64 = 0 ,
78+ feeID: Int64 = 0 ,
79+ currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ,
80+ stores: StoresManager = ServiceLocator . stores) {
81+ self . siteID = siteID
82+ self . orderID = orderID
83+ self . feeID = feeID
5784 self . currencyFormatter = currencyFormatter
85+ self . stores = stores
5886 self . providedAmount = currencyFormatter. formatAmount ( providedAmount) ?? providedAmount
5987 self . totalWithTaxes = currencyFormatter. formatAmount ( totalWithTaxes) ?? totalWithTaxes
6088 self . taxAmount = currencyFormatter. formatAmount ( taxAmount) ?? taxAmount
@@ -81,16 +109,49 @@ final class SimplePaymentsSummaryViewModel: ObservableObject {
81109
82110 convenience init ( order: Order ,
83111 providedAmount: String ,
84- currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ) {
112+ currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ,
113+ stores: StoresManager = ServiceLocator . stores) {
85114 self . init ( providedAmount: providedAmount,
86115 totalWithTaxes: order. total,
87116 taxAmount: order. totalTax,
88- currencyFormatter: currencyFormatter)
117+ siteID: order. siteID,
118+ orderID: order. orderID,
119+ feeID: order. fees. first? . feeID ?? 0 ,
120+ currencyFormatter: currencyFormatter,
121+ stores: stores)
89122 }
90123
91124 /// Sends a signal to reload the view. Needed when coming back from the `EditNote` view.
92125 ///
93126 func reloadContent( ) {
94127 objectWillChange. send ( )
95128 }
129+
130+ /// Updates the order remotely with the information entered by the merchant.
131+ ///
132+ func updateOrder( ) {
133+ showLoadingIndicator = true
134+ let action = OrderAction . updateSimplePaymentsOrder ( siteID: siteID,
135+ orderID: orderID,
136+ feeID: feeID,
137+ amount: providedAmount,
138+ taxable: enableTaxes,
139+ orderNote: noteContent,
140+ email: email) { [ weak self] result in
141+ guard let self = self else { return }
142+ self . showLoadingIndicator = false
143+
144+ switch result {
145+ case . success:
146+ // TODO: Navigate to Payment Method
147+ // TODO: Analytics
148+ break
149+ case . failure:
150+ // TODO: Present notice
151+ // TODO: Analytics
152+ break
153+ }
154+ }
155+ stores. dispatch ( action)
156+ }
96157}
0 commit comments