Skip to content

Commit d9744ff

Browse files
committed
refactor of codes and validation
1 parent 55de6c2 commit d9744ff

17 files changed

+537
-469
lines changed

raveandroid/src/main/java/com/flutterwave/raveandroid/RaveConstants.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ public class RaveConstants {
1717
public static String VBV = "VBVSECURECODE";
1818
public static String GTB_OTP = "GTB_OTP";
1919
public static String ACCESS_OTP = "ACCESS_OTP";
20+
public static String NG = "NG";
21+
public static String NGN = "NGN";
22+
public static String UGX = "UGX";
2023
public static String NOAUTH = "NOAUTH";
2124
public static String PIN = "PIN";
2225
public static String AVS_VBVSECURECODE = "AVS_VBVSECURECODE";
26+
public static String enterOTP = "Enter your one time password (OTP)";
2327
public static String NOAUTH_INTERNATIONAL = "NOAUTH_INTERNATIONAL";
2428
public static String RAVEPAY = "ravepay";
2529
public static String RAVE_PARAMS = "raveparams";
@@ -44,6 +48,8 @@ public class RaveConstants {
4448

4549
public static String success = "success";
4650
public static String noResponse = "No response data was returned";
51+
public static String accounNumberPrompt = "Enter a valid account number";
52+
public static String defaultAccounNumber = "0000000000";
4753

4854
public static String response = "response";
4955
public static String mtn = "mtn";
@@ -74,7 +80,6 @@ public class RaveConstants {
7480
public static String unknownAuthmsg = "Unknown Auth Model";
7581
public static String unknownResCodemsg = "Unknown charge response code";
7682
public static String no_authurl_was_returnedmsg = "No authUrl was returned";
77-
public static String no_response_data_was_returnedmsg = "No response data was returned";
7883
public static String wait = "Please wait...";
7984
public static String cancelPayment = "CANCEL PAYMENT";
8085
}

raveandroid/src/main/java/com/flutterwave/raveandroid/Utils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,13 @@ public static PublicKey getKey(String key) {
198198
}
199199

200200
public static String getEncryptedData(String unEncryptedString, String encryptionKey) {
201-
try {
202-
return encrypt(unEncryptedString, encryptionKey);
203-
} catch (Exception e) {
204-
e.printStackTrace();
201+
202+
if (unEncryptedString !=null && encryptionKey != null) {
203+
try {
204+
return encrypt(unEncryptedString, encryptionKey);
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
}
205208
}
206209
return null;
207210
}

raveandroid/src/main/java/com/flutterwave/raveandroid/account/AccountFragment.java

Lines changed: 122 additions & 102 deletions
Large diffs are not rendered by default.

raveandroid/src/main/java/com/flutterwave/raveandroid/account/AccountPresenter.java

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.HashMap;
3030
import java.util.List;
3131

32+
import static com.flutterwave.raveandroid.RaveConstants.*;
33+
3234
/**
3335
* Created by hamzafetuga on 20/07/2017.
3436
*/
@@ -136,15 +138,15 @@ public void onSuccess(ChargeResponse response, String responseAsJSONString) {
136138
String status = response.getStatus();
137139
String message = response.getMessage();
138140

139-
if (status.equalsIgnoreCase(RaveConstants.success)) {
141+
if (status.equalsIgnoreCase(success)) {
140142
mView.onValidateSuccessful(flwRef, responseAsJSONString);
141143
}
142144
else {
143145
mView.onValidateError(status, responseAsJSONString);
144146
}
145147
}
146148
else {
147-
mView.onPaymentError(RaveConstants.invalidCharge);
149+
mView.onPaymentError(invalidCharge);
148150
}
149151
}
150152

@@ -179,15 +181,15 @@ public void onSuccess(FeeCheckResponse response) {
179181
}
180182
catch (Exception e) {
181183
e.printStackTrace();
182-
mView.showFetchFeeFailed("An error occurred while retrieving transaction fee");
184+
mView.showFetchFeeFailed(transactionError);
183185
}
184186
}
185187

186188
@Override
187189
public void onError(String message) {
188190
mView.showProgressIndicator(false);
189-
Log.e(RaveConstants.RAVEPAY, message);
190-
mView.showFetchFeeFailed("An error occurred while retrieving transaction fee");
191+
Log.e(RAVEPAY, message);
192+
mView.showFetchFeeFailed(transactionError);
191193
}
192194
});
193195
}
@@ -234,56 +236,59 @@ public void onDataCollected(HashMap<String, ViewObject> dataHashMap) {
234236

235237
boolean valid = true;
236238

237-
int amountID = dataHashMap.get(RaveConstants.fieldAmount).getViewId();
238-
String amount = dataHashMap.get(RaveConstants.fieldAmount).getData();
239-
Class amountViewType = dataHashMap.get(RaveConstants.fieldAmount).getViewType();
239+
int amountID = dataHashMap.get(fieldAmount).getViewId();
240+
String amount = dataHashMap.get(fieldAmount).getData();
241+
Class amountViewType = dataHashMap.get(fieldAmount).getViewType();
240242

241-
int emailID = dataHashMap.get(RaveConstants.fieldEmail).getViewId();
242-
String email = dataHashMap.get(RaveConstants.fieldEmail).getData();
243-
Class emailViewType = dataHashMap.get(RaveConstants.fieldEmail).getViewType();
243+
int emailID = dataHashMap.get(fieldEmail).getViewId();
244+
String email = dataHashMap.get(fieldEmail).getData();
245+
Class emailViewType = dataHashMap.get(fieldEmail).getViewType();
244246

245-
int accountID = dataHashMap.get(RaveConstants.fieldAccount).getViewId();
246-
String account = dataHashMap.get(RaveConstants.fieldAccount).getData();
247-
Class accountViewType = dataHashMap.get(RaveConstants.fieldAccount).getViewType();
247+
int accountID = dataHashMap.get(fieldAccount).getViewId();
248+
String account = dataHashMap.get(fieldAccount).getData();
249+
Class accountViewType = dataHashMap.get(fieldAccount).getViewType();
248250

249-
int phoneID = dataHashMap.get(RaveConstants.fieldPhone).getViewId();
250-
String phone = dataHashMap.get(RaveConstants.fieldPhone).getData();
251-
Class phoneViewType = dataHashMap.get(RaveConstants.fieldPhone).getViewType();
251+
int phoneID = dataHashMap.get(fieldPhone).getViewId();
252+
String phone = dataHashMap.get(fieldPhone).getData();
253+
Class phoneViewType = dataHashMap.get(fieldPhone).getViewType();
252254

255+
boolean isAmountValid = amountValidator.isAmountValid(Double.valueOf(amount));
256+
boolean isPhoneValid = phoneValidator.isPhoneValid(phone);
257+
boolean isEmailValid = emailValidator.isEmailValid(email);
253258

254-
if (!amountValidator.isAmountValid(Double.valueOf(amount))) {
259+
if (!isAmountValid) {
255260
valid = false;
256-
mView.showFieldError(amountID, RaveConstants.validAmountPrompt, amountViewType);
261+
mView.showFieldError(amountID, validAmountPrompt, amountViewType);
257262
}
258263

259-
if (!phoneValidator.isPhoneValid(phone)) {
264+
if (!isPhoneValid) {
260265
valid = false;
261-
mView.showFieldError(phoneID, RaveConstants.validPhonePrompt, phoneViewType);
266+
mView.showFieldError(phoneID, validPhonePrompt, phoneViewType);
262267
}
263268

264-
if (!emailValidator.isEmailValid(email)) {
269+
if (!isEmailValid) {
265270
valid = false;
266-
mView.showFieldError(emailID, RaveConstants.validEmailPrompt, emailViewType);
271+
mView.showFieldError(emailID, validEmailPrompt, emailViewType);
267272
}
268273

269274
if (account.isEmpty()) {
270275
valid = false;
271-
mView.showFieldError(accountID, "Enter a valid account number", phoneViewType);
276+
mView.showFieldError(accountID, accounNumberPrompt, phoneViewType);
272277
} else {
273-
account = "0000000000";
278+
account = defaultAccounNumber;
274279
}
275280

276281
try {
277282
double amnt = Double.parseDouble(amount);
278283

279284
if (amnt <= 0) {
280285
valid = false;
281-
mView.showToast(RaveConstants.validAmountPrompt);
286+
mView.showToast(validAmountPrompt);
282287
}
283288
} catch (Exception e) {
284289
e.printStackTrace();
285290
valid = false;
286-
mView.showToast(RaveConstants.validAmountPrompt);
291+
mView.showToast(validAmountPrompt);
287292
}
288293

289294
if (valid){
@@ -299,32 +304,32 @@ public void processTransaction(HashMap<String, ViewObject> dataHashMap, RavePayI
299304

300305
if (ravePayInitializer!=null) {
301306
PayloadBuilder builder = new PayloadBuilder();
302-
builder.setAmount(ravePayInitializer.getAmount() + "")
303-
.setEmail(dataHashMap.get(RaveConstants.fieldEmail).getData())
304-
.setCountry("NG").setCurrency("NGN")
307+
builder.setAmount(String.valueOf(ravePayInitializer.getAmount()))
308+
.setEmail(dataHashMap.get(fieldEmail).getData())
309+
.setCountry(NG).setCurrency(NGN)
305310
.setPBFPubKey(ravePayInitializer.getPublicKey())
306311
.setDevice_fingerprint(Utils.getDeviceImei(context))
307312
.setIP(Utils.getDeviceImei(context)).setTxRef(ravePayInitializer.getTxRef())
308-
.setAccountbank(dataHashMap.get(RaveConstants.fieldBankCode).getData())
313+
.setAccountbank(dataHashMap.get(fieldBankCode).getData())
309314
.setMeta(ravePayInitializer.getMeta())
310315
.setSubAccount(ravePayInitializer.getSubAccount())
311-
.setAccountnumber(dataHashMap.get(RaveConstants.fieldAccount).getData())
312-
.setBVN(RaveConstants.fieldBVN)
316+
.setAccountnumber(dataHashMap.get(fieldAccount).getData())
317+
.setBVN(fieldBVN)
313318
.setIsPreAuth(ravePayInitializer.getIsPreAuth());
314319

315320
Payload body = builder.createBankPayload();
316-
body.setPasscode(RaveConstants.date_of_birth);
317-
body.setPhonenumber(RaveConstants.fieldPhone);
321+
body.setPasscode(date_of_birth);
322+
body.setPhonenumber(fieldPhone);
318323

319-
if ((dataHashMap.get(RaveConstants.fieldBankCode).getData().equalsIgnoreCase("058") ||
320-
dataHashMap.get(RaveConstants.fieldBankCode).getData().equalsIgnoreCase("011"))
321-
&& (Double.parseDouble(dataHashMap.get(RaveConstants.fieldAmount).getData()) <= 100)) {
324+
if ((dataHashMap.get(fieldBankCode).getData().equalsIgnoreCase("058") ||
325+
dataHashMap.get(fieldBankCode).getData().equalsIgnoreCase("011"))
326+
&& (Double.parseDouble(dataHashMap.get(fieldAmount).getData()) <= 100)) {
322327
mView.showGTBankAmountIssue();
323328
} else {
324329
if (ravePayInitializer.getIsDisplayFee()) {
325-
fetchFee(body, Boolean.valueOf(dataHashMap.get(RaveConstants.isInternetBanking).getData()));
330+
fetchFee(body, Boolean.valueOf(dataHashMap.get(isInternetBanking).getData()));
326331
} else {
327-
chargeAccount(body, ravePayInitializer.getEncryptionKey(), Boolean.valueOf(dataHashMap.get(RaveConstants.isInternetBanking).getData()));
332+
chargeAccount(body, ravePayInitializer.getEncryptionKey(), Boolean.valueOf(dataHashMap.get(isInternetBanking).getData()));
328333
}
329334
}
330335
}
@@ -345,8 +350,10 @@ public void onDetachView() {
345350
public void init(RavePayInitializer ravePayInitializer) {
346351

347352
if (ravePayInitializer!=null) {
353+
348354
boolean isEmailValid = emailValidator.isEmailValid(ravePayInitializer.getEmail());
349355
boolean isAmountValid = amountValidator.isAmountValid(ravePayInitializer.getAmount());
356+
350357
if (isEmailValid) {
351358
mView.onEmailValidated(ravePayInitializer.getEmail(), View.GONE);
352359
} else {

raveandroid/src/main/java/com/flutterwave/raveandroid/ach/AchFragment.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
*/
3030
public class AchFragment extends Fragment implements AchContract.View, View.OnClickListener {
3131

32+
private View v;
33+
private Button payButton;
34+
private AchPresenter presenter;
35+
private TextInputLayout amountTil;
36+
private TextInputEditText amountEt;
37+
private TextView payInstructionsTv;
3238
private ProgressDialog progressDialog;
33-
AchPresenter presenter;
34-
View v;
3539
private RavePayInitializer ravePayInitializer;
36-
Button payButton;
37-
TextInputLayout amountTil;
38-
TextInputEditText amountEt;
39-
TextView payInstructionsTv;
40+
4041
public static final int FOR_ACH = 892;
4142

4243
@Override
@@ -48,7 +49,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
4849
v = inflater.inflate(R.layout.fragment_ach, container, false);
4950

5051
initializeViews();
51-
ravePayInitializer = ((RavePayActivity) getActivity()).getRavePayInitializer();
52+
53+
initializeRavePay();
5254

5355
presenter.onStartAchPayment(ravePayInitializer);
5456

@@ -57,6 +59,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5759
return v;
5860
}
5961

62+
private void initializeRavePay() {
63+
if (getActivity() != null ){
64+
ravePayInitializer = ((RavePayActivity) getActivity()).getRavePayInitializer();
65+
}
66+
}
67+
6068
private void setListeners() {
6169
payButton.setOnClickListener(this);
6270
}
@@ -215,7 +223,6 @@ public void onPaymentSuccessful(String status, String flwRef, String responseAsJ
215223

216224
Intent intent = new Intent();
217225
intent.putExtra("response", responseAsJSONString);
218-
219226
if (getActivity() != null) {
220227
getActivity().setResult(RavePayActivity.RESULT_SUCCESS, intent);
221228
getActivity().finish();

raveandroid/src/main/java/com/flutterwave/raveandroid/ach/AchPresenter.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@
1515
import com.flutterwave.raveandroid.data.SharedPrefsRequestImpl;
1616
import com.flutterwave.raveandroid.responses.ChargeResponse;
1717
import com.flutterwave.raveandroid.responses.RequeryResponse;
18+
import com.flutterwave.raveandroid.validators.AmountValidator;
1819

1920

2021
public class AchPresenter implements AchContract.UserActionsListener {
2122

2223
private Context context;
2324
private AchContract.View mView;
2425
private SharedPrefsRequestImpl sharedMgr;
26+
private AmountValidator amountValidator;
2527

2628
public AchPresenter(Context context, AchContract.View mView) {
2729
this.context = context;
2830
this.mView = mView;
2931
sharedMgr = new SharedPrefsRequestImpl(context);
32+
amountValidator = new AmountValidator();
3033
}
3134

3235
@Override
3336
public void onStartAchPayment(RavePayInitializer ravePayInitializer) {
3437

35-
if (ravePayInitializer.getAmount() > 0 || ravePayInitializer.toString().isEmpty()) {
38+
boolean isAmountValid = amountValidator.isAmountValid(ravePayInitializer.getAmount());
39+
if (isAmountValid){
3640
mView.showAmountField(false);
3741
mView.showRedirectMessage(true);
3842
}
@@ -48,25 +52,14 @@ public void onPayButtonClicked(RavePayInitializer ravePayInitializer, String amo
4852

4953
mView.showAmountError(null);
5054

51-
if (ravePayInitializer.getAmount() > 0) {
55+
boolean isAmountValid = amountValidator.isAmountValid(ravePayInitializer.getAmount());
56+
57+
if (isAmountValid){
58+
ravePayInitializer.setAmount(ravePayInitializer.getAmount());
5259
initiatePayment(ravePayInitializer);
5360
}
5461
else {
55-
try {
56-
double amnt = Double.parseDouble(amount);
57-
58-
if (amnt <= 0) {
59-
mView.showAmountError(context.getResources().getString(R.string.validAmountPrompt));
60-
}
61-
else {
62-
ravePayInitializer.setAmount(amnt);
63-
initiatePayment(ravePayInitializer);
64-
}
65-
}
66-
catch (Exception e) {
67-
e.printStackTrace();
68-
mView.showAmountError(context.getResources().getString(R.string.validAmountPrompt));
69-
}
62+
mView.showAmountError(context.getResources().getString(R.string.validAmountPrompt));
7063
}
7164

7265
}
@@ -110,6 +103,7 @@ public void chargeAccount(Payload payload, String encryptionKey, final boolean i
110103
mView.showProgressIndicator(true);
111104

112105
new NetworkRequestImpl().chargeCard(body, new Callbacks.OnChargeRequestComplete() {
106+
113107
@Override
114108
public void onSuccess(ChargeResponse response, String responseAsJSONString) {
115109

@@ -137,7 +131,7 @@ public void onSuccess(ChargeResponse response, String responseAsJSONString) {
137131

138132
}
139133
else {
140-
mView.onPaymentError(RaveConstants.no_response_data_was_returnedmsg);
134+
mView.onPaymentError(RaveConstants.noResponse);
141135
}
142136

143137
}

0 commit comments

Comments
 (0)