Skip to content

Commit 70767fb

Browse files
committed
Refactor view and presenter
1 parent 7b11e70 commit 70767fb

File tree

7 files changed

+190
-90
lines changed

7 files changed

+190
-90
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ Set the public key, encryption key and other required parameters. The `RavePayMa
6161
.acceptMpesaPayments(boolean)
6262
.acceptAchPayments(boolean)
6363
.acceptGHMobileMoneyPayments(boolean)
64-
.acceptUgMobileMoneyPayments(boolean)
64+
.acceptUgMobileMoneyPayments(boolean)
65+
.acceptBankTransferPayments(boolean)
6566
.onStagingEnv(boolean)
6667
.setMeta(List<Meta>)
6768
.withTheme(styleId)
@@ -79,7 +80,7 @@ Set the public key, encryption key and other required parameters. The `RavePayMa
7980
| setfName(fName) | This is the first name of the card holder or the customer | `String` | Required
8081
| setlName(lName) | This is the last name of the card holder or the customer | `String` | Required
8182
| setEmail(email) | This is the email address of the customer | `String` | Required
82-
| setNarration(narration) | This is a custom description added by the merchant | `String` | Not Required
83+
| setNarration(narration) | This is a custom description added by the merchant. For `Bank Transfer` payments, this becomes the account name of the account to be paid into. See more details [here](https://developer.flutterwave.com/v2.0/reference#pay-with-bank-transfer-nigeria). | `String` | Not Required
8384
| setPublicKey(publicKey) | Merchant's public key. Get your merchant keys here for [ staging](https://flutterwavedevelopers.readme.io/blog/how-to-get-your-staging-keys-from-the-rave-sandbox-environment) and [live](https://flutterwavedevelopers.readme.io/blog/how-to-get-your-live-keys-from-the-rave-dashboard)| `String` | Required
8485
| setEncryptionKey(encryptionKey) | Merchant's encryption key. Get your merchant keys here for [ staging](https://flutterwavedevelopers.readme.io/blog/how-to-get-your-staging-keys-from-the-rave-sandbox-environment) and [live](https://flutterwavedevelopers.readme.io/blog/how-to-get-your-live-keys-from-the-rave-dashboard) | `String` | Required
8586
| setTxRef(txRef) | This is the unique reference, unique to the particular transaction being carried out. It is generated by the merchant for every transaction | `String` | Required
@@ -89,10 +90,10 @@ Set the public key, encryption key and other required parameters. The `RavePayMa
8990
| acceptGHMobileMoneyPayments(boolean) | Set to `true` if you want to accept Ghana mobile money payments, else set to `false` . For this option to work, you should set your country to `GH` and your currency to `GHS`| `boolean` | Not Required |
9091
| acceptUgMobileMoneyPayments(boolean) | Set to `true` if you want to accept Uganda mobile money payments, else set to `false` . For this option to work, you should set your country to `UG` and your currency to `UGX`| `boolean` | Not Required |
9192
| acceptAchPayments(boolean) | Set to `true` if you want to accept US ACH charges from your customers, else set to `false` . For this option to work, you should set your country to `US` and your currency to `USD`. You also have to set `acceptAccountPayments(true)`| `boolean` | Not Required |
92-
| acceptBankTransferPayments(boolean) | Set to `true` if you want to accept payments via bank transfer from your customers, else set to `false` . This option is currently only available for Nigerian Naira.| `boolean` | Not Required |
93+
| acceptBankTransferPayments(boolean) | Set to `true` if you want to accept payments via bank transfer from your customers, else set to `false` . This option is currently only available for Nigerian Naira. See more details in the [API documentation](https://developer.flutterwave.com/v2.0/reference#pay-with-bank-transfer-nigeria).| `boolean` | Not Required |
9394
| onStagingEnv(boolean) | Set to `true` if you want your transactions to run in the staging environment otherwise set to `false`. Defaults to false | `boolean` | Not Required
9495
| setMeta(`List<Meta>`) | Pass in any other custom data you wish to pass. It takes in a `List` of `Meta` objects | List<Meta> | Not Required
95-
| setSubAccounts(`List<SubAccount>`) | Pass in a `List` of `SubAccount`,if you want to split transaction fee with other people. Subaccounts are your vendors' accounts that you want to settle per transaction. To initialize a `SubAccout` class, do `SubAccount(String subAccountId,String transactionSplitRatio)` or `SubAccount(String subAccountId,String transactionSplitRatio,String transactionChargeType, String transactionCharge)` to also charge the subaccount a fee. [Learn more about split payments and subaccounts](https://developer.flutterwave.com/docs/split-payment).| `List<SubAccount>`| Not Required
96+
| setSubAccounts(`List<SubAccount>`) | Pass in a `List` of `SubAccount`,if you want to split transaction fee with other people. Subaccounts are your vendors' accounts that you want to settle per transaction. To initialize a `SubAccount` class, do `SubAccount(String subAccountId,String transactionSplitRatio)` or `SubAccount(String subAccountId,String transactionSplitRatio,String transactionChargeType, String transactionCharge)` to also charge the subaccount a fee. [Learn more about split payments and subaccounts](https://developer.flutterwave.com/docs/split-payment).| `List<SubAccount>`| Not Required
9697
| setIsPreAuth(boolean) | Set to `true` to preauthorise the transaction amount. [Learn more about preauthourization](https://developer.flutterwave.com/v2.0/reference#introduction-1). | `int` | Not Required
9798
| withTheme(styleId) | Sets the theme of the UI. | `int` | Not Required
9899
| setPaymentPlan(payment_plan) | If you want to do recurrent payment, this is the payment plan ID to use for the recurring payment, you can see how to create payment plans [here](https://flutterwavedevelopers.readme.io/v2.0/reference#create-payment-plan) and [here](https://flutterwavedevelopers.readme.io/docs/recurring-billing). This is only available for card payments | `String` | Not Required

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ else if (ravePayInitializer.getCountry().equalsIgnoreCase("ng") && ravePayInitia
120120

121121
if (ravePayInitializer.isWithBankTransfer()) {
122122
if (ravePayInitializer.getCountry().equalsIgnoreCase("ng") && ravePayInitializer.getCurrency().equalsIgnoreCase("ngn")){
123-
raveFragments.add(new RaveFragment(new BankTransferFragment(), "Pay with Bank Transfer"));
123+
raveFragments.add(new RaveFragment(new BankTransferFragment(), "Bank Transfer"));
124124
}
125125
}
126126

raveandroid/src/main/java/com/flutterwave/raveandroid/banktransfer/BankTransferContract.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.flutterwave.raveandroid.banktransfer;
22

33
import com.flutterwave.raveandroid.Payload;
4+
import com.flutterwave.raveandroid.RavePayInitializer;
5+
import com.flutterwave.raveandroid.ViewObject;
46
import com.flutterwave.raveandroid.responses.ChargeResponse;
57

8+
import java.util.HashMap;
9+
610

711
public interface BankTransferContract {
812

@@ -18,12 +22,18 @@ interface View {
1822
void onPaymentFailed(String message, String responseAsJSONString);
1923
void onTransferDetailsReceived(ChargeResponse response);
2024
void onPollingTimeout(String flwRef, String txRef, String responseAsJSONString);
25+
void onAmountValidationSuccessful(String valueOf);
26+
void showFieldError(int viewID, String message, Class<?> viewType);
27+
void onValidationSuccessful(HashMap<String, ViewObject> dataHashMap);
2128
}
2229

2330
interface UserActionsListener {
2431
void fetchFee(Payload payload);
2532
void requeryTx(String flwRef, String txRef, String publicKey);
2633
void payWithBankTransfer(Payload body, String encryptionKey);
2734
void setRequeryCountdownTime(long currentTimeMillis);
35+
void init(RavePayInitializer ravePayInitializer);
36+
void processTransaction(HashMap<String, ViewObject> dataHashMap, RavePayInitializer ravePayInitializer);
37+
void onDataCollected(HashMap<String, ViewObject> dataHashMap);
2838
}
2939
}

raveandroid/src/main/java/com/flutterwave/raveandroid/banktransfer/BankTransferFragment.java

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@
1717
import android.view.View;
1818
import android.view.ViewGroup;
1919
import android.widget.Button;
20+
import android.widget.EditText;
2021
import android.widget.TextView;
2122
import android.widget.Toast;
2223

2324
import com.flutterwave.raveandroid.Payload;
2425
import com.flutterwave.raveandroid.PayloadBuilder;
2526
import com.flutterwave.raveandroid.R;
27+
import com.flutterwave.raveandroid.RaveConstants;
2628
import com.flutterwave.raveandroid.RavePayActivity;
2729
import com.flutterwave.raveandroid.RavePayInitializer;
2830
import com.flutterwave.raveandroid.Utils;
31+
import com.flutterwave.raveandroid.ViewObject;
2932
import com.flutterwave.raveandroid.responses.ChargeResponse;
3033

34+
import java.util.HashMap;
35+
3136
import static android.view.View.GONE;
3237

3338
/**
3439
* A simple {@link Fragment} subclass.
3540
*/
36-
public class BankTransferFragment extends Fragment implements BankTransferContract.View {
41+
public class BankTransferFragment extends Fragment implements BankTransferContract.View, View.OnClickListener {
3742

3843
View v;
3944
TextInputEditText amountEt;
@@ -45,6 +50,7 @@ public class BankTransferFragment extends Fragment implements BankTransferContra
4550
TextView transferInstructionTv;
4651
TextView transferStatusTv;
4752
Button verifyPaymentButton;
53+
Button payButton;
4854
ConstraintLayout initiateChargeLayout;
4955
ConstraintLayout transferDetailsLayout;
5056
RavePayInitializer ravePayInitializer;
@@ -61,11 +67,58 @@ public BankTransferFragment() {
6167
@Override
6268
public View onCreateView(LayoutInflater inflater, ViewGroup container,
6369
Bundle savedInstanceState) {
70+
presenter = new BankTransferPresenter(getActivity(), this);
6471

6572
// Inflate the layout for this fragment
6673
v = inflater.inflate(R.layout.fragment_bank_transfer, container, false);
6774

68-
presenter = new BankTransferPresenter(getActivity(), this);
75+
76+
initializeViews();
77+
78+
setListeners();
79+
80+
ravePayInitializer = ((RavePayActivity) getActivity()).getRavePayInitializer();
81+
82+
presenter.init(ravePayInitializer);
83+
84+
double amountToPay = ravePayInitializer.getAmount();
85+
86+
if (amountToPay > 0) {
87+
amountTil.setVisibility(GONE);
88+
amountEt.setText(String.valueOf(amountToPay));
89+
}
90+
91+
return v;
92+
}
93+
94+
private void setListeners() {
95+
payButton.setOnClickListener(this);
96+
verifyPaymentButton.setOnClickListener(this);
97+
98+
}
99+
100+
@Override
101+
public void onClick(View view) {
102+
int i = view.getId();
103+
if (i == payButton.getId()) {
104+
clearErrors();
105+
Utils.hide_keyboard(getActivity());
106+
collectData();
107+
}
108+
109+
if(i == verifyPaymentButton.getId()){
110+
verifyPayment();
111+
}
112+
}
113+
114+
private void collectData() {
115+
HashMap<String, ViewObject> dataHashMap = new HashMap<>();
116+
117+
dataHashMap.put(RaveConstants.fieldAmount, new ViewObject(amountTil.getId(), amountEt.getText().toString(), TextInputLayout.class));
118+
presenter.onDataCollected(dataHashMap);
119+
}
120+
121+
private void initializeViews() {
69122
amountEt = (TextInputEditText) v.findViewById(R.id.rave_amountTV);
70123
amountTil = (TextInputLayout) v.findViewById(R.id.rave_amountTil);
71124
initiateChargeLayout = (ConstraintLayout) v.findViewById(R.id.rave_initiate_payment_layout);
@@ -76,36 +129,31 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
76129
beneficiaryNameTv = (TextView) v.findViewById(R.id.rave_beneficiary_name_tv);
77130
bankNameTv = (TextView) v.findViewById(R.id.rave_bank_name_tv);
78131
accountNumberTv = (TextView) v.findViewById(R.id.rave_account_number_tv);
79-
80-
Button payButton = (Button) v.findViewById(R.id.rave_payButton);
132+
payButton = (Button) v.findViewById(R.id.rave_payButton);
81133
verifyPaymentButton = (Button) v.findViewById(R.id.rave_verify_payment_button);
134+
}
82135

83-
ravePayInitializer = ((RavePayActivity) getActivity()).getRavePayInitializer();
84-
85-
payButton.setOnClickListener(new View.OnClickListener() {
86-
@Override
87-
public void onClick(View v) {
88-
validate();
89-
}
90-
91-
});
92-
verifyPaymentButton.setOnClickListener(new View.OnClickListener() {
93-
@Override
94-
public void onClick(View v) {
95-
verifyPayment();
96-
}
97-
});
136+
@Override
137+
public void onAmountValidationSuccessful(String amountToPay) {
138+
amountTil.setVisibility(GONE);
139+
amountEt.setText(amountToPay);
140+
}
98141

99-
double amountToPay = ravePayInitializer.getAmount();
100142

101-
if (amountToPay > 0) {
102-
amountTil.setVisibility(GONE);
103-
amountEt.setText(String.valueOf(amountToPay));
143+
@Override
144+
public void showFieldError(int viewID, String message, Class<?> viewType) {
145+
146+
if (viewType == TextInputLayout.class) {
147+
TextInputLayout view = v.findViewById(viewID);
148+
view.setError(message);
149+
} else if (viewType == EditText.class) {
150+
EditText view = v.findViewById(viewID);
151+
view.setError(message);
104152
}
105153

106-
return v;
107154
}
108155

156+
109157
private void verifyPayment() {
110158
canShowPollingIndicator = true;
111159
showPollingIndicator(true);
@@ -127,8 +175,9 @@ public void onPollingRoundComplete(String flwRef, String txRef, String publicKey
127175

128176
@Override
129177
public void showPollingIndicator(boolean active) {
130-
if (getActivity().isFinishing()) {
131-
return;
178+
if (getActivity() != null) {
179+
if (getActivity().isFinishing())
180+
return;
132181
}
133182

134183
if (canShowPollingIndicator) {
@@ -204,60 +253,6 @@ private void showTransferDetails(ChargeResponse response) {
204253

205254
}
206255

207-
private void validate() {
208-
clearErrors();
209-
Utils.hide_keyboard(getActivity());
210-
211-
boolean valid = true;
212-
213-
String amount = amountEt.getText().toString();
214-
215-
try {
216-
double amnt = Double.parseDouble(amount);
217-
218-
if (amnt <= 0) {
219-
valid = false;
220-
amountTil.setError("Enter a valid amount");
221-
}
222-
} catch (Exception e) {
223-
e.printStackTrace();
224-
valid = false;
225-
amountTil.setError("Enter a valid amount");
226-
}
227-
228-
if (valid) {
229-
230-
ravePayInitializer.setAmount(Double.parseDouble(amount));
231-
232-
String txRef = ravePayInitializer.getTxRef();
233-
Log.d("txRef", txRef);
234-
PayloadBuilder builder = new PayloadBuilder();
235-
builder.setAmount(ravePayInitializer.getAmount() + "")
236-
.setCountry(ravePayInitializer.getCountry())
237-
.setCurrency(ravePayInitializer.getCurrency())
238-
.setEmail(ravePayInitializer.getEmail())
239-
.setFirstname(ravePayInitializer.getfName())
240-
.setLastname(ravePayInitializer.getlName())
241-
.setIP(Utils.getDeviceImei(getActivity()))
242-
.setTxRef(ravePayInitializer.getTxRef())
243-
.setMeta(ravePayInitializer.getMeta())
244-
.setSubAccount(ravePayInitializer.getSubAccount())
245-
.setPBFPubKey(ravePayInitializer.getPublicKey())
246-
.setIsPreAuth(ravePayInitializer.getIsPreAuth())
247-
.setDevice_fingerprint(Utils.getDeviceImei(getActivity()));
248-
249-
250-
Payload body = builder.createBankTransferPayload();
251-
252-
if (ravePayInitializer.getIsDisplayFee()) {
253-
presenter.fetchFee(body);
254-
} else {
255-
presenter.payWithBankTransfer(body, ravePayInitializer.getEncryptionKey());
256-
}
257-
}
258-
259-
}
260-
261256

262257
@Override
263258
public void showProgressIndicator(boolean active) {
@@ -384,5 +379,12 @@ public void showFetchFeeFailed(String s) {
384379
showToast(s);
385380
}
386381

382+
@Override
383+
public void onValidationSuccessful(HashMap<String, ViewObject> dataHashMap) {
384+
385+
ravePayInitializer.setAmount(Double.parseDouble(dataHashMap.get(RaveConstants.fieldAmount).getData()));
386+
presenter.processTransaction(dataHashMap, ravePayInitializer);
387+
388+
}
387389

388390
}

0 commit comments

Comments
 (0)