Skip to content

Commit 253d6b9

Browse files
committed
AmountValidator Update
1 parent 5e10c56 commit 253d6b9

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

raveandroid/src/main/java/com/flutterwave/raveandroid/mpesa/MpesaFragment.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.flutterwave.raveandroid.R;
2121
import com.flutterwave.raveandroid.RavePayActivity;
2222
import com.flutterwave.raveandroid.RavePayInitializer;
23+
import com.flutterwave.raveandroid.Utils;
2324
import com.flutterwave.raveandroid.ViewObject;
2425

2526
import java.util.HashMap;
@@ -90,8 +91,11 @@ private void initializeViews() {
9091
public void onClick(View view) {
9192
int i = view.getId();
9293
if (i == R.id.rave_payButton) {
93-
clearErrors();
94-
collectData();
94+
if (getActivity() != null) {
95+
Utils.hide_keyboard(getActivity());
96+
clearErrors();
97+
collectData();
98+
}
9599
}
96100
}
97101

raveandroid/src/main/java/com/flutterwave/raveandroid/mpesa/MpesaPresenter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
168168
String phone = dataHashMap.get(fieldPhone).getData();
169169
Class phoneViewType = dataHashMap.get(fieldPhone).getViewType();
170170

171-
boolean isAmountValidated = amountValidator.isAmountValid(amount);
171+
boolean isAmountValid = amountValidator.isAmountValid(amount);
172172
boolean isPhoneValid = phoneValidator.isPhoneValid(phone);
173173

174-
if (!isAmountValidated) {
174+
if (!isAmountValid) {
175175
valid = false;
176176
mView.showFieldError(amountID, validAmountPrompt, amountViewType);
177177
}
@@ -195,7 +195,7 @@ public void processTransaction(HashMap<String, ViewObject> dataHashMap, RavePayI
195195
ravePayInitializer.setAmount(Double.parseDouble(dataHashMap.get(fieldAmount).getData()));
196196

197197
PayloadBuilder builder = new PayloadBuilder();
198-
builder.setAmount(ravePayInitializer.getAmount() + "")
198+
builder.setAmount(String.valueOf(ravePayInitializer.getAmount()))
199199
.setCountry(ravePayInitializer.getCountry())
200200
.setCurrency(ravePayInitializer.getCurrency())
201201
.setEmail(ravePayInitializer.getEmail())

raveandroid/src/main/java/com/flutterwave/raveandroid/validators/AmountValidator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.flutterwave.raveandroid.validators;
22

3-
import java.util.regex.Pattern;
4-
53
public class AmountValidator {
64

75
public boolean isAmountValid(Double amount) {
@@ -10,9 +8,17 @@ public boolean isAmountValid(Double amount) {
108

119
public boolean isAmountValid(String amount) {
1210

13-
if (amount != null && !amount.isEmpty()) {
14-
return Pattern.matches("^[0-9]*$", amount);
15-
} else {
11+
try {
12+
if (amount != null) {
13+
if (!amount.isEmpty()) {
14+
return Double.parseDouble(amount) > 0;
15+
} else {
16+
return false;
17+
}
18+
} else {
19+
return false;
20+
}
21+
} catch (NumberFormatException e) {
1622
return false;
1723
}
1824

raveandroid/src/test/java/com/flutterwave/raveandroid/validators/AmountValidatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void setUp() throws Exception {
2020

2121
@Test
2222
public void isAmountValid_isCorrectAmountPassed_returnsTrue() {
23-
Double amount = 1.0;
23+
Double amount = 123.0;
2424
boolean isAmountValid = SUT.isAmountValid(amount);
2525
assertThat(true, is(isAmountValid));
2626
}

0 commit comments

Comments
 (0)