Skip to content

Commit d533657

Browse files
committed
Prevent user to create an order with zero amount
1 parent 0851c3a commit d533657

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Amount/SimplePaymentsAmountViewModel.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
3535
///
3636
var onOrderCreated: (Order) -> Void = { _ in }
3737

38-
/// Returns true when amount has less than two characters.
39-
/// Less than two, because `$` should be the first character.
38+
/// Returns true when amount is a valid number bigger than .zero.
4039
///
4140
var shouldDisableDoneButton: Bool {
42-
amount.count < 2
41+
let decimalAmount = (currencyFormatter.convertToDecimal(from: amount) ?? .zero) as Decimal
42+
return decimalAmount <= .zero
4343
}
4444

4545
/// Defines if the view actions should be disabled.
@@ -85,6 +85,10 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
8585
///
8686
private let storeCurrencySettings: CurrencySettings
8787

88+
/// Currency formatter for the provided amount
89+
///
90+
private let currencyFormatter: CurrencyFormatter
91+
8892
/// Current store currency symbol
8993
///
9094
private let storeCurrencySymbol: String
@@ -110,6 +114,7 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
110114
self.presentNoticeSubject = presentNoticeSubject
111115
self.storeCurrencySettings = storeCurrencySettings
112116
self.storeCurrencySymbol = storeCurrencySettings.symbol(from: storeCurrencySettings.currencyCode)
117+
self.currencyFormatter = CurrencyFormatter(currencySettings: storeCurrencySettings)
113118
self.analytics = analytics
114119
self.isDevelopmentPrototype = isDevelopmentPrototype
115120
}

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsAmountViewModelTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ final class SimplePaymentsAmountViewModelTests: XCTestCase {
9191
XCTAssertTrue(viewModel.shouldDisableDoneButton)
9292
}
9393

94+
func test_view_model_disables_next_button_when_amount_is_not_greater_than_zero() {
95+
// Given
96+
let viewModel = SimplePaymentsAmountViewModel(siteID: sampleSiteID, storeCurrencySettings: usStoreSettings)
97+
98+
// When
99+
viewModel.amount = "$0"
100+
101+
// Then
102+
XCTAssertTrue(viewModel.shouldDisableDoneButton)
103+
}
104+
94105
func test_view_model_enables_next_button_when_amount_has_more_than_one_character() {
95106
// Given
96107
let viewModel = SimplePaymentsAmountViewModel(siteID: sampleSiteID, storeCurrencySettings: usStoreSettings)

0 commit comments

Comments
 (0)