Skip to content

Commit 375b2e5

Browse files
authored
Merge pull request #7063 from woocommerce/issue/7004-refactor-CPP-eligibility-logic-into-Yosemite
[Mobile Payments] Refactor cpp eligibility logic into Yosemite
2 parents 780c71c + 902cfd1 commit 375b2e5

File tree

18 files changed

+266
-312
lines changed

18 files changed

+266
-312
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ final class OrderDetailsDataSource: NSObject {
4646

4747
/// Whether the order is eligible for card present payment.
4848
///
49-
var isEligibleForCardPresentPayment: Bool {
50-
return cardPresentPaymentsConfiguration.isSupportedCountry &&
51-
order.isEligibleForCardPresentPayment(cardPresentPaymentsConfiguration: cardPresentPaymentsConfiguration,
52-
products: resultsControllers.products)
53-
}
49+
var isEligibleForCardPresentPayment: Bool = false
5450

5551
var isEligibleForRefund: Bool {
5652
guard !isRefundedStatus,
@@ -567,7 +563,7 @@ private extension OrderDetailsDataSource {
567563
private func configureCustomerPaid(cell: TwoColumnHeadlineFootnoteTableViewCell) {
568564
let paymentViewModel = OrderPaymentDetailsViewModel(order: order)
569565
cell.leftText = Titles.paidByCustomer
570-
cell.rightText = paymentViewModel.paymentTotal
566+
cell.rightText = order.paymentTotal
571567
cell.updateFootnoteText(paymentViewModel.paymentSummary)
572568
}
573569

@@ -603,10 +599,8 @@ private extension OrderDetailsDataSource {
603599
}
604600

605601
private func configureNetAmount(cell: TwoColumnHeadlineFootnoteTableViewCell) {
606-
let paymentViewModel = OrderPaymentDetailsViewModel(order: order)
607-
608602
cell.leftText = Titles.netAmount
609-
cell.rightText = paymentViewModel.netAmount
603+
cell.rightText = order.netAmount
610604
cell.hideFootnote()
611605
}
612606

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ extension OrderDetailsViewModel {
233233
}
234234

235235
group.enter()
236-
refreshCardPresentPaymentEligibility()
236+
checkCardPresentPaymentEligibility() {
237+
onReloadSections?()
238+
group.leave()
239+
}
240+
241+
group.enter()
242+
loadPaymentGatewayAccounts()
237243
group.leave()
238244

239245
group.enter()
@@ -601,7 +607,33 @@ extension OrderDetailsViewModel {
601607
stores.dispatch(action)
602608
}
603609

604-
func refreshCardPresentPaymentEligibility() {
610+
func checkCardPresentPaymentEligibility(onCompletion: @escaping (() -> Void)) {
611+
let configuration = configurationLoader.configuration
612+
613+
guard configuration.isSupportedCountry else {
614+
dataSource.isEligibleForCardPresentPayment = false
615+
onCompletion()
616+
return
617+
}
618+
619+
let action = OrderCardPresentPaymentEligibilityAction
620+
.orderIsEligibleForCardPresentPayment(orderID: order.orderID,
621+
siteID: order.siteID,
622+
cardPresentPaymentsConfiguration: configurationLoader.configuration) { [weak self] result in
623+
switch result {
624+
case .success(let eligible):
625+
self?.dataSource.isEligibleForCardPresentPayment = eligible
626+
case .failure(_):
627+
self?.dataSource.isEligibleForCardPresentPayment = false
628+
}
629+
630+
onCompletion()
631+
}
632+
633+
stores.dispatch(action)
634+
}
635+
636+
func loadPaymentGatewayAccounts() {
605637
/// No need for a completion here. The VC will be notified of changes to the stored paymentGatewayAccounts
606638
/// by the viewModel (after passing up through the dataSource and originating in the resultsControllers)
607639
///

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ final class OrderPaymentDetailsViewModel {
5858
}
5959

6060
var totalValue: String {
61-
return currencyFormatter.formatAmount(order.total, with: order.currency) ?? String()
61+
order.totalValue
6262
}
6363

6464
var paymentTotal: String {
65-
if order.datePaid == nil {
66-
return currencyFormatter.formatAmount("0.00", with: order.currency) ?? String()
67-
}
68-
69-
return totalValue
65+
order.paymentTotal
7066
}
7167

7268
private var feesTotal: Decimal {
@@ -162,26 +158,10 @@ final class OrderPaymentDetailsViewModel {
162158
return currencyFormatter.formatAmount(condensedRefund.normalizedTotalAsNegative, with: order.currency)
163159
}
164160

165-
/// Format the net amount with the correct currency
166-
///
167-
var netAmount: String? {
168-
guard let netDecimal = calculateNetAmount() else {
169-
return nil
170-
}
171-
172-
return currencyFormatter.formatAmount(netDecimal, with: order.currency)
173-
}
174-
175161
var couponLines: [OrderCouponLine] {
176162
return order.coupons
177163
}
178164

179-
/// Signals whether the net amount for the order matches the total order amount
180-
///
181-
var hasBeenPartiallyCharged: Bool {
182-
return totalValue != netAmount
183-
}
184-
185165
init(order: Order, refund: Refund? = nil, currencySettings: CurrencySettings = ServiceLocator.currencySettings) {
186166
self.order = order
187167
self.refund = refund
@@ -204,19 +184,6 @@ final class OrderPaymentDetailsViewModel {
204184

205185
return NSLocalizedString("Discount", comment: "Discount label for payment view") + " (" + output + ")"
206186
}
207-
208-
/// Calculate the net amount after refunds
209-
///
210-
private func calculateNetAmount() -> NSDecimalNumber? {
211-
guard let orderTotal = currencyFormatter.convertToDecimal(order.total) else {
212-
return .zero
213-
}
214-
215-
let totalRefundedUseCase = TotalRefundedCalculationUseCase(order: order, currencyFormatter: currencyFormatter)
216-
let refundTotal = totalRefundedUseCase.totalRefunded()
217-
218-
return orderTotal.adding(refundTotal)
219-
}
220187
}
221188

222189
private extension OrderPaymentDetailsViewModel {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ private extension OrderDetailsViewController {
600600
// Refresh date & view once payment has been collected.
601601
if result.isSuccess {
602602
self.viewModel.syncOrderAfterPaymentCollection {
603-
self.viewModel.refreshCardPresentPaymentEligibility()
603+
self.viewModel.checkCardPresentPaymentEligibility {
604+
self.reloadTableViewSectionsAndData()
605+
}
604606
}
605607
}
606608
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ final class SimplePaymentsMethodsViewModel: ObservableObject {
9393
return controller
9494
}()
9595

96-
/// Product ResultsController.
97-
///
98-
private lazy var productResultsController: ResultsController<StorageProduct> = {
99-
let predicate = NSPredicate(format: "siteID == %lld", siteID)
100-
let descriptor = NSSortDescriptor(key: "name", ascending: true)
101-
102-
return ResultsController<StorageProduct>(storageManager: storage, matching: predicate, sortedBy: [descriptor])
103-
}()
104-
10596
/// Retains the use-case so it can perform all of its async tasks.
10697
///
10798
private var collectPaymentsUseCase: CollectOrderPaymentProtocol?
@@ -270,20 +261,29 @@ private extension SimplePaymentsMethodsViewModel {
270261
///
271262
func bindStoreCPPState() {
272263
ordersResultController.onDidChangeContent = updateCardPaymentVisibility
273-
productResultsController.onDidChangeContent = updateCardPaymentVisibility
274264
try? ordersResultController.performFetch()
275-
try? productResultsController.performFetch()
276265
}
277266

278267
func updateCardPaymentVisibility() {
279-
guard let order = ordersResultController.fetchedObjects.first else {
268+
guard cardPresentPaymentsConfiguration.isSupportedCountry else {
280269
showPayWithCardRow = false
270+
281271
return
282272
}
283273

284-
showPayWithCardRow = cardPresentPaymentsConfiguration.isSupportedCountry && order.isEligibleForCardPresentPayment(
285-
cardPresentPaymentsConfiguration: cardPresentPaymentsConfiguration,
286-
products: productResultsController.fetchedObjects)
274+
let action = OrderCardPresentPaymentEligibilityAction
275+
.orderIsEligibleForCardPresentPayment(orderID: orderID,
276+
siteID: siteID,
277+
cardPresentPaymentsConfiguration: cardPresentPaymentsConfiguration) { [weak self] result in
278+
switch result {
279+
case .success(let eligible):
280+
self?.showPayWithCardRow = eligible
281+
case .failure(_):
282+
self?.showPayWithCardRow = false
283+
}
284+
}
285+
286+
stores.dispatch(action)
287287
}
288288

289289
func updateOrderAsynchronously() {

WooCommerce/Classes/Yosemite/AuthenticatedState.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AuthenticatedState: StoresManagerState {
4141
MediaStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
4242
NotificationStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
4343
NotificationCountStore(dispatcher: dispatcher, storageManager: storageManager, fileStorage: PListFileStorage()),
44+
OrderCardPresentPaymentEligibilityStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
4445
OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
4546
OrderStore(dispatcher: dispatcher, storageManager: storageManager, network: network),
4647
OrderStatusStore(dispatcher: dispatcher, storageManager: storageManager, network: network),

0 commit comments

Comments
 (0)