11package com .flutterwave .rave_android ;
22
3+ import android .app .ProgressDialog ;
34import android .content .Intent ;
45import android .os .Bundle ;
6+
7+ import androidx .annotation .Nullable ;
58import androidx .appcompat .app .AlertDialog ;
69import androidx .appcompat .app .AppCompatActivity ;
710import androidx .appcompat .widget .SwitchCompat ;
1518import android .widget .TextView ;
1619import android .widget .Toast ;
1720
18- import com .flutterwave .raveandroid .Meta ;
19- import com .flutterwave .raveandroid .RaveConstants ;
2021import com .flutterwave .raveandroid .RavePayActivity ;
21- import com .flutterwave .raveandroid .RavePayManager ;
22- import com .flutterwave .raveandroid .Utils ;
23- import com .flutterwave .raveandroid .responses .SubAccount ;
22+ import com .flutterwave .raveandroid .RaveUiManager ;
23+ import com .flutterwave .raveandroid .data .Utils ;
24+ import com .flutterwave .raveandroid .rave_core .models .SavedCard ;
25+ import com .flutterwave .raveandroid .rave_java_commons .Meta ;
26+ import com .flutterwave .raveandroid .rave_java_commons .RaveConstants ;
27+ import com .flutterwave .raveandroid .rave_java_commons .SubAccount ;
28+ import com .flutterwave .raveandroid .rave_presentation .FeeCheckListener ;
29+ import com .flutterwave .raveandroid .rave_presentation .RaveNonUIManager ;
30+ import com .flutterwave .raveandroid .rave_presentation .RavePayManager ;
31+ import com .flutterwave .raveandroid .rave_presentation .card .Card ;
32+ import com .flutterwave .raveandroid .rave_presentation .card .CardPaymentCallback ;
33+ import com .flutterwave .raveandroid .rave_presentation .card .CardPaymentManager ;
34+ import com .flutterwave .raveandroid .rave_presentation .card .SavedCardsListener ;
35+ import com .flutterwave .raveandroid .rave_presentation .data .AddressDetails ;
36+ import com .flutterwave .raveandroid .rave_remote .responses .SaveCardResponse ;
2437
2538import java .util .ArrayList ;
2639import java .util .List ;
2740
2841
29- public class MainActivity extends AppCompatActivity {
42+ public class MainActivity
43+ extends AppCompatActivity
44+ // Interfaces to implement for custom UI or no-UI usage
45+ implements
46+ FeeCheckListener , // Implement if you want to be able to check fees beforehand
47+ SavedCardsListener , // Implement if you want to be able to save cards and charge saved cards
48+ CardPaymentCallback {// Must be implemented to charge cards with custom UI or no-UI
3049
3150 EditText emailEt ;
3251 EditText amountEt ;
@@ -62,6 +81,7 @@ public class MainActivity extends AppCompatActivity {
6281 SwitchCompat isMpesaSwitch ;
6382 SwitchCompat accountAchSwitch ;
6483 SwitchCompat addSubAccountsSwitch ;
84+ SwitchCompat useRaveUISwitch ;
6585 SwitchCompat isPreAuthSwitch ;
6686 SwitchCompat allowSavedCardsSwitch ;
6787 SwitchCompat shouldDisplayFeeSwitch ;
@@ -72,6 +92,10 @@ public class MainActivity extends AppCompatActivity {
7292 LinearLayout expiryDetailsLayout ;
7393 TextView vendorListTXT ;
7494
95+ ProgressDialog progressDialog ;
96+ private CardPaymentManager cardPayManager ;
97+ private Card card ;
98+
7599 @ Override
76100 protected void onCreate (Bundle savedInstanceState ) {
77101 super .onCreate (savedInstanceState );
@@ -114,6 +138,7 @@ protected void onCreate(Bundle savedInstanceState) {
114138 barterSwitch = findViewById (R .id .barter_switch );
115139 isLiveSwitch = findViewById (R .id .isLiveSwitch );
116140 addSubAccountsSwitch = findViewById (R .id .addSubAccountsSwitch );
141+ useRaveUISwitch = findViewById (R .id .useRaveUISwitch );
117142 shouldShowStagingLabelSwitch = findViewById (R .id .shouldShowStagingLabelSwitch );
118143 addVendorBtn = findViewById (R .id .addVendorBtn );
119144 clearVendorBtn = findViewById (R .id .clearVendorsBtn );
@@ -269,61 +294,96 @@ private void validateEntries() {
269294 }
270295
271296 if (valid ) {
272- RavePayManager ravePayManager = new RavePayManager (this ).setAmount (Double .parseDouble (amount ))
273- .setCountry (country )
274- .setCurrency (currency )
275- .setEmail (email )
276- .setfName (fName )
277- .setlName (lName )
278- .setPhoneNumber (phoneNumber )
279- .setNarration (narration )
280- .setPublicKey (publicKey )
281- .setEncryptionKey (encryptionKey )
282- .setTxRef (txRef )
283- .acceptMpesaPayments (isMpesaSwitch .isChecked ())
284- .acceptAccountPayments (accountSwitch .isChecked ())
285- .acceptCardPayments (cardSwitch .isChecked ())
286- .allowSaveCardFeature (allowSavedCardsSwitch .isChecked ())
287- .acceptAchPayments (accountAchSwitch .isChecked ())
288- .acceptGHMobileMoneyPayments (ghMobileMoneySwitch .isChecked ())
289- .acceptUgMobileMoneyPayments (ugMobileMoneySwitch .isChecked ())
290- .acceptZmMobileMoneyPayments (zmMobileMoneySwitch .isChecked ())
291- .acceptRwfMobileMoneyPayments (rwfMobileMoneySwitch .isChecked ())
292- .acceptUkPayments (ukbankSwitch .isChecked ())
293- .acceptSaBankPayments (saBankSwitch .isChecked ())
294- .acceptFrancMobileMoneyPayments (francMobileMoneySwitch .isChecked ())
295- .acceptBankTransferPayments (bankTransferSwitch .isChecked ())
296- .acceptUssdPayments (ussdSwitch .isChecked ())
297- .acceptBarterPayments (barterSwitch .isChecked ())
298- .onStagingEnv (!isLiveSwitch .isChecked ())
299- .setSubAccounts (subAccounts )
300- .isPreAuth (isPreAuthSwitch .isChecked ())
301- .showStagingLabel (shouldShowStagingLabelSwitch .isChecked ())
302- // .setMeta(meta)
303- // .withTheme(R.style.TestNewTheme)
304- .shouldDisplayFee (shouldDisplayFeeSwitch .isChecked ());
305-
306-
307- // Customize pay with bank transfer options (optional)
308- if (isPermanentAccountSwitch .isChecked ())
309- ravePayManager .acceptBankTransferPayments (true , true );
310- else {
311- if (setExpirySwitch .isChecked ()) {
312- int duration = 0 , frequency = 0 ;
313- try {
314- duration = Integer .parseInt (durationEt .getText ().toString ());
315- frequency = Integer .parseInt (frequencyEt .getText ().toString ());
316- } catch (NumberFormatException e ) {
317- e .printStackTrace ();
297+ RavePayManager raveManager ;
298+
299+ boolean shouldUseRaveUi = useRaveUISwitch .isChecked ();
300+
301+ if (shouldUseRaveUi ) {
302+ raveManager = new RaveUiManager (this )
303+ .acceptMpesaPayments (isMpesaSwitch .isChecked ())
304+ .acceptAccountPayments (accountSwitch .isChecked ())
305+ .acceptCardPayments (cardSwitch .isChecked ())
306+ .allowSaveCardFeature (allowSavedCardsSwitch .isChecked ())
307+ .acceptAchPayments (accountAchSwitch .isChecked ())
308+ .acceptGHMobileMoneyPayments (ghMobileMoneySwitch .isChecked ())
309+ .acceptUgMobileMoneyPayments (ugMobileMoneySwitch .isChecked ())
310+ .acceptZmMobileMoneyPayments (zmMobileMoneySwitch .isChecked ())
311+ .acceptRwfMobileMoneyPayments (rwfMobileMoneySwitch .isChecked ())
312+ .acceptUkPayments (ukbankSwitch .isChecked ())
313+ .acceptSaBankPayments (saBankSwitch .isChecked ())
314+ .acceptFrancMobileMoneyPayments (francMobileMoneySwitch .isChecked ())
315+ .acceptBankTransferPayments (bankTransferSwitch .isChecked ())
316+ .acceptUssdPayments (ussdSwitch .isChecked ())
317+ .acceptBarterPayments (barterSwitch .isChecked ())
318+ // .withTheme(R.style.TestNewTheme)
319+ .showStagingLabel (shouldShowStagingLabelSwitch .isChecked ())
320+ .setAmount (Double .parseDouble (amount ))
321+ .setCountry (country )
322+ .setCurrency (currency )
323+ .setEmail (email )
324+ .setfName (fName )
325+ .setlName (lName )
326+ .setPhoneNumber (phoneNumber )
327+ .setNarration (narration )
328+ .setPublicKey (publicKey )
329+ .setEncryptionKey (encryptionKey )
330+ .setTxRef (txRef )
331+ .onStagingEnv (!isLiveSwitch .isChecked ())
332+ .setSubAccounts (subAccounts )
333+ .isPreAuth (isPreAuthSwitch .isChecked ())
334+ .setMeta (meta )
335+ .shouldDisplayFee (shouldDisplayFeeSwitch .isChecked ());
336+
337+ // Customize pay with bank transfer options (optional)
338+ if (isPermanentAccountSwitch .isChecked ())
339+ ((RaveUiManager ) raveManager ).acceptBankTransferPayments (true , true );
340+ else {
341+ if (setExpirySwitch .isChecked ()) {
342+ int duration = 0 , frequency = 0 ;
343+ try {
344+ duration = Integer .parseInt (durationEt .getText ().toString ());
345+ frequency = Integer .parseInt (frequencyEt .getText ().toString ());
346+ } catch (NumberFormatException e ) {
347+ e .printStackTrace ();
348+ }
349+ ((RaveUiManager ) raveManager ).acceptBankTransferPayments (true , duration , frequency );
318350 }
319- ravePayManager .acceptBankTransferPayments (true , duration , frequency );
320351 }
321- }
322-
323-
324- ravePayManager .initialize ();
325-
326352
353+ raveManager .initialize ();
354+
355+ } else {
356+ raveManager = new RaveNonUIManager ().setAmount (Double .parseDouble (amount ))
357+ .setCountry (country )
358+ .setCurrency (currency )
359+ .setEmail (email )
360+ .setfName (fName )
361+ .setlName (lName )
362+ .setPhoneNumber (phoneNumber )
363+ .setNarration (narration )
364+ .setPublicKey (publicKey )
365+ .setEncryptionKey (encryptionKey )
366+ .setTxRef (txRef )
367+ .onStagingEnv (!isLiveSwitch .isChecked ())
368+ .setSubAccounts (subAccounts )
369+ .setMeta (meta )
370+ .isPreAuth (isPreAuthSwitch .isChecked ())
371+ .initialize ();
372+
373+ cardPayManager = new CardPaymentManager (((RaveNonUIManager ) raveManager ), this , this );
374+ card = new Card (
375+ "5531886652142950" , // Test MasterCard PIN authentication
376+ // "4242424242424242", // Test VisaCard 3D-Secure Authentication
377+ // "4556052704172643", // Test VisaCard (Address Verification)
378+ "12" ,
379+ "30" ,
380+ "123"
381+ );
382+
383+ // cardPayManager.fetchSavedCards();
384+ // cardPayManager.fetchTransactionFee(card,this);
385+ cardPayManager .chargeCard (card );
386+ }
327387 }
328388 }
329389
@@ -412,4 +472,110 @@ public void onClick(View view) {
412472 alertDialog .show ();
413473 }
414474
475+ @ Override
476+ public void showProgressIndicator (boolean active ) {
477+ try {
478+ if (isFinishing ()) {
479+ return ;
480+ }
481+
482+ if (progressDialog == null ) {
483+ progressDialog = new ProgressDialog (this );
484+ progressDialog .setCanceledOnTouchOutside (false );
485+ progressDialog .setMessage ("Please wait..." );
486+ }
487+
488+ if (active && !progressDialog .isShowing ()) {
489+ progressDialog .show ();
490+ } else {
491+ progressDialog .dismiss ();
492+ }
493+ } catch (NullPointerException e ) {
494+ e .printStackTrace ();
495+ }
496+ }
497+
498+ @ Override
499+ public void collectCardPin () {
500+ Toast .makeText (this , "Submitting PIN" , Toast .LENGTH_SHORT ).show ();
501+ cardPayManager .submitPin ("3310" );
502+ }
503+
504+ @ Override
505+ public void collectOtp (String message ) {
506+ Toast .makeText (this , message , Toast .LENGTH_SHORT ).show ();
507+ Toast .makeText (this , "Submitting OTP" , Toast .LENGTH_SHORT ).show ();
508+ cardPayManager .submitOtp ("12345" );
509+ }
510+
511+ @ Override
512+ public void collectAddress () {
513+ Toast .makeText (this , "Submitting address details" , Toast .LENGTH_SHORT ).show ();
514+ cardPayManager .submitAddress (new AddressDetails (
515+ "8, Providence Street" ,
516+ "Lekki Phase 1" ,
517+ "Lagos" ,
518+ "102102" ,
519+ "NG" )
520+ );
521+ }
522+
523+ @ Override
524+ public void showAuthenticationWebPage (String authenticationUrl ) {
525+ Toast .makeText (this , "Called to load web page" , Toast .LENGTH_SHORT ).show ();
526+
527+ // Load webpage
528+ // cardPayManager.onWebpageAuthenticationComplete();
529+ }
530+
531+ @ Override
532+ public void onError (String errorMessage , @ Nullable String flwRef ) {
533+ Toast .makeText (this , errorMessage , Toast .LENGTH_LONG ).show ();
534+ }
535+
536+ @ Override
537+ public void onSuccessful (String flwRef ) {
538+ Toast .makeText (this , "Transaction Successful" , Toast .LENGTH_LONG ).show ();
539+ // cardPayManager.saveCard(); // Save card if needed
540+ }
541+
542+ @ Override
543+ public void onSavedCardsLookupSuccessful (List <SavedCard > cards , String phoneNumber ) {
544+ // Check that the list is not empty, show the user to select which they'd like to charge, then proceed to chargeSavedCard()
545+ if (cards .size () != 0 ) cardPayManager .chargeSavedCard (cards .get (0 ));
546+ else
547+ Toast .makeText (this , "No saved cards found for " + phoneNumber , Toast .LENGTH_SHORT ).show ();
548+ }
549+
550+ @ Override
551+ public void onSavedCardsLookupFailed (String message ) {
552+ Toast .makeText (this , message , Toast .LENGTH_SHORT ).show ();
553+ }
554+
555+ @ Override
556+ public void collectOtpForSaveCardCharge () {
557+ collectOtp ("Otp for saved card" );
558+ }
559+
560+ @ Override
561+ public void onCardSaveSuccessful (SaveCardResponse response , String phoneNumber ) {
562+
563+ }
564+
565+ @ Override
566+ public void onCardSaveFailed (String message ) {
567+
568+ }
569+
570+ @ Override
571+ public void onTransactionFeeFetched (String chargeAmount , String fee ) {
572+ // Display the fee to the customer
573+ Toast .makeText (this , "The transaction fee is " + fee , Toast .LENGTH_SHORT ).show ();
574+ // cardPayManager.chargeCard(card);
575+ }
576+
577+ @ Override
578+ public void onFetchFeeError (String errorMessage ) {
579+
580+ }
415581}
0 commit comments