-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFrancMobileMoneyFragment.java
More file actions
356 lines (285 loc) · 11.8 KB
/
FrancMobileMoneyFragment.java
File metadata and controls
356 lines (285 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.flutterwave.raveandroid.francMobileMoney;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.flutterwave.raveandroid.R;
import com.flutterwave.raveandroid.RavePayActivity;
import com.flutterwave.raveandroid.RavePayInitializer;
import com.flutterwave.raveandroid.ViewObject;
import com.flutterwave.raveandroid.data.Utils;
import com.flutterwave.raveandroid.data.events.FeeDisplayResponseEvent;
import com.flutterwave.raveandroid.data.events.RequeryCancelledEvent;
import com.flutterwave.raveandroid.di.modules.FrancModule;
import com.flutterwave.raveandroid.rave_java_commons.Payload;
import com.flutterwave.raveandroid.rave_logger.events.StartTypingEvent;
import com.flutterwave.raveandroid.rave_presentation.data.events.ErrorEvent;
import com.flutterwave.raveutils.verification.RaveVerificationUtils;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.HashMap;
import javax.inject.Inject;
import static android.view.View.GONE;
import static com.flutterwave.raveandroid.rave_java_commons.RaveConstants.WEB_VERIFICATION_REQUEST_CODE;
import static com.flutterwave.raveandroid.rave_java_commons.RaveConstants.fieldAmount;
import static com.flutterwave.raveandroid.rave_java_commons.RaveConstants.fieldPhone;
import static com.flutterwave.raveandroid.rave_java_commons.RaveConstants.response;
/**
* A simple {@link Fragment} subclass.
*/
public class FrancMobileMoneyFragment extends Fragment implements FrancMobileMoneyUiContract.View, View.OnClickListener, View.OnFocusChangeListener {
@Inject
FrancMobileMoneyPresenter presenter;
private View v;
private Button payButton;
private TextInputLayout phoneTil;
private TextInputLayout amountTil;
private TextInputEditText phoneEt;
private TextInputEditText amountEt;
private ProgressDialog progressDialog;
private ProgressDialog pollingProgressDialog;
private int rave_phoneEtInt;
private RavePayInitializer ravePayInitializer;
private String flwRef;
private String note;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
injectComponents();
v = inflater.inflate(R.layout.rave_sdk_fragment_francophone, container, false);
initializeViews();
setListeners();
initializePresenter();
return v;
}
private void injectComponents() {
if (getActivity() != null) {
((RavePayActivity) getActivity()).getRaveUiComponent()
.plus(new FrancModule(this))
.inject(this);
}
}
private void initializePresenter() {
if (getActivity() != null) {
ravePayInitializer = ((RavePayActivity) getActivity()).getRavePayInitializer();
presenter.init(ravePayInitializer);
}
}
private void setListeners() {
payButton.setOnClickListener(this);
amountEt.setOnFocusChangeListener(this);
phoneEt.setOnFocusChangeListener(this);
}
private void initializeViews() {
payButton = v.findViewById(R.id.rave_payButton);
amountTil = v.findViewById(R.id.rave_amountTil);
amountEt = v.findViewById(R.id.rave_amountEt);
phoneTil = v.findViewById(R.id.rave_phoneTil);
phoneEt = v.findViewById(R.id.rave_phoneEt);
rave_phoneEtInt = amountEt.getId();
}
@Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.rave_payButton) {
if (getActivity() != null) {
Utils.hide_keyboard(getActivity());
clearErrors();
collectData();
}
}
}
private void clearErrors() {
amountTil.setErrorEnabled(false);
phoneTil.setErrorEnabled(false);
amountTil.setError(null);
phoneTil.setError(null);
}
private void collectData() {
HashMap<String, ViewObject> dataHashMap = new HashMap<>();
dataHashMap.put(fieldAmount, new ViewObject(amountTil.getId(), amountEt.getText().toString(), TextInputLayout.class));
dataHashMap.put(fieldPhone, new ViewObject(phoneTil.getId(), phoneEt.getText().toString(), TextInputLayout.class));
presenter.onDataCollected(dataHashMap);
}
@Override
public void showFieldError(int viewID, String message, Class<?> viewType) {
if (viewType == TextInputLayout.class) {
TextInputLayout view = v.findViewById(viewID);
view.setError(message);
} else if (viewType == EditText.class) {
EditText view = v.findViewById(viewID);
view.setError(message);
}
}
@Override
public void onAmountValidationSuccessful(String amountToPay) {
amountTil.setVisibility(GONE);
amountEt.setText(amountToPay);
}
@Override
public void onPhoneValidated(String phoneToSet, boolean isEditable) {
phoneEt.setText(phoneToSet);
phoneEt.setEnabled(isEditable);
}
@Override
public void showProgressIndicator(boolean active) {
if (getActivity() == null || getActivity().isFinishing()) {
return;
}
if (progressDialog == null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage(getResources().getString(R.string.wait));
}
if (active && !progressDialog.isShowing()) {
progressDialog.show();
} else if (active && progressDialog.isShowing()) {
//pass
} else {
progressDialog.dismiss();
}
}
/**
* Called when the auth model suggested is VBV. It opens a WebView
* that loads the authURL
*
* @param authenticationUrl URL to display in webview
* @param flwRef Reference of the payment transaction
*/
@Override
public void showWebPage(String authenticationUrl, String flwRef) {
this.flwRef = flwRef;
if (isAdded()){
new RaveVerificationUtils(this, ravePayInitializer.isStaging(), ravePayInitializer.getPublicKey(), ravePayInitializer.getTheme())
.showWebpageVerificationScreen(authenticationUrl,flwRef);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RavePayActivity.RESULT_SUCCESS) {
//just to be sure this v sent the receiving intent
if (requestCode == WEB_VERIFICATION_REQUEST_CODE) {
presenter.requeryTx(flwRef, ravePayInitializer.getPublicKey(), note);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onValidationSuccessful(HashMap<String, ViewObject> dataHashMap) {
presenter.processTransaction(dataHashMap, ravePayInitializer);
}
@Override
public void onTransactionFeeRetrieved(String charge_amount, final Payload payload, String fee) {
if (getActivity() != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.RaveDialogStyle);
builder.setMessage(getResources().getString(R.string.charge) + " " + charge_amount + " " + ravePayInitializer.getCurrency() + getResources().getString(R.string.askToContinue));
builder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
presenter.logEvent(new FeeDisplayResponseEvent(true).getEvent(), ravePayInitializer.getPublicKey());
presenter.chargeFranc(payload, ravePayInitializer.getEncryptionKey());
}
}).setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
presenter.logEvent(new FeeDisplayResponseEvent(false).getEvent(), ravePayInitializer.getPublicKey());
}
});
builder.show();
}
}
@Override
public void showFetchFeeFailed(String message) {
presenter.logEvent(new ErrorEvent(message).getEvent(), ravePayInitializer.getPublicKey());
showToast(message);
}
public void showToast(String message) {
if(getActivity() != null){
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPaymentSuccessful(String flwRef, String responseAsString) {
Intent intent = new Intent();
intent.putExtra(response, responseAsString);
if (getActivity() != null) {
((RavePayActivity) getActivity()).setRavePayResult(RavePayActivity.RESULT_SUCCESS, intent);
getActivity().finish();
}
}
@Override
public void onPaymentFailed(String message, String responseAsJSONString) {
Intent intent = new Intent();
intent.putExtra(response, responseAsJSONString);
if (getActivity() != null) {
((RavePayActivity) getActivity()).setRavePayResult(RavePayActivity.RESULT_ERROR, intent);
getActivity().finish();
}
}
@Override
public void onPaymentError(String message) {
presenter.logEvent(new ErrorEvent(message).getEvent(), ravePayInitializer.getPublicKey());
showToast(message);
}
@Override
public void showPollingIndicator(boolean active, String note) {
this.note = note;
if (getActivity() == null || getActivity().isFinishing()) {
return;
}
if (pollingProgressDialog == null) {
pollingProgressDialog = new ProgressDialog(getActivity());
pollingProgressDialog.setMessage(note == null || note.isEmpty()?getResources().getString(R.string.checkStatus):note);
}
if (active && !pollingProgressDialog.isShowing()) {
pollingProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
presenter.logEvent(new RequeryCancelledEvent().getEvent(), ravePayInitializer.getPublicKey());
pollingProgressDialog.dismiss();
}
});
pollingProgressDialog.show();
} else if (active && pollingProgressDialog.isShowing()) {
//pass
} else {
pollingProgressDialog.dismiss();
}
}
@Override
public void onResume() {
super.onResume();
presenter.onAttachView(this);
}
@Override
public void onDetach() {
super.onDetach();
if (presenter != null) {
presenter.onDetachView();
}
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
int i = view.getId();
String fieldName = "";
if (i == R.id.rave_amountEt) {
fieldName = "Amount";
} else if (i == R.id.rave_phoneEt) {
fieldName = "Phone Number";
}
if (hasFocus) {
presenter.logEvent(new StartTypingEvent(fieldName).getEvent(), ravePayInitializer.getPublicKey());
}
}
}