6666import static com .flutterwave .raveandroid .RaveConstants .fieldCvv ;
6767import static com .flutterwave .raveandroid .RaveConstants .fieldEmail ;
6868import static com .flutterwave .raveandroid .RaveConstants .fieldcardNoStripped ;
69- import static com .flutterwave .raveandroid .RaveConstants .invalidChargeCode ;
7069import static com .flutterwave .raveandroid .RaveConstants .noResponse ;
7170import static com .flutterwave .raveandroid .RaveConstants .success ;
7271import static com .flutterwave .raveandroid .RaveConstants .tokenExpired ;
7372import static com .flutterwave .raveandroid .RaveConstants .tokenNotFound ;
7473import static com .flutterwave .raveandroid .RaveConstants .transactionError ;
7574import static com .flutterwave .raveandroid .RaveConstants .unknownAuthmsg ;
76- import static com .flutterwave .raveandroid .RaveConstants .unknownResCodemsg ;
7775import static com .flutterwave .raveandroid .RaveConstants .validAmountPrompt ;
7876import static com .flutterwave .raveandroid .RaveConstants .validCreditCardPrompt ;
7977import static com .flutterwave .raveandroid .RaveConstants .validCvvPrompt ;
@@ -134,7 +132,7 @@ public CardPresenter(Context context, CardContract.View mView) {
134132 this .context = context ;
135133 }
136134
137- public CardPresenter (Context context , CardContract .View mView , AppComponent appComponent ){
135+ public CardPresenter (Context context , CardContract .View mView , AppComponent appComponent ) {
138136 this .context = context ;
139137 this .mView = mView ;
140138 this .eventLogger = appComponent .eventLogger ();
@@ -152,6 +150,13 @@ public CardPresenter(Context context, CardContract.View mView, AppComponent appC
152150 this .gson = appComponent .gson ();
153151 }
154152
153+ /**
154+ * Makes a generic call to the charge endpoint with the payload provided. Handles both conditions
155+ * for initial charge request and when the suggested auth has been added.
156+ *
157+ * @param payload {@link Payload} object to be sent.
158+ * @param encryptionKey Rave encryption key gotten from dashboard
159+ */
155160 @ Override
156161 public void chargeCard (final Payload payload , final String encryptionKey ) {
157162
@@ -189,33 +194,44 @@ public void onSuccess(ChargeResponse response, String responseAsJSONString) {
189194 mView .onPaymentError (unknownAuthmsg );
190195 }
191196 } else {
192- String authModelUsed = response .getData ().getAuthModelUsed ();
193-
194- if (authModelUsed != null ) {
195-
196- if (authModelUsed .equalsIgnoreCase (VBV )) {
197- String authUrlCrude = response .getData ().getAuthurl ();
198- String flwRef = response .getData ().getFlwRef ();
199-
200- mView .onVBVAuthModelUsed (authUrlCrude , flwRef );
201- } else if (authModelUsed .equalsIgnoreCase (GTB_OTP )
202- || authModelUsed .equalsIgnoreCase (ACCESS_OTP )
203- || authModelUsed .toLowerCase ().contains ("otp" )) {
204- String flwRef = response .getData ().getFlwRef ();
205- String chargeResponseMessage = response .getData ().getChargeResponseMessage ();
206- chargeResponseMessage = chargeResponseMessage == null ? enterOTP : chargeResponseMessage ;
207- mView .showOTPLayout (flwRef , chargeResponseMessage );
208-
209- } else if (authModelUsed .equalsIgnoreCase (NOAUTH )) {
210- String flwRef = response .getData ().getFlwRef ();
211- mView .onNoAuthUsed (flwRef , payload .getPBFPubKey ());
197+ // Check if transaction is already successful
198+ if (response .getData ().getChargeResponseCode () != null && response .getData ().getChargeResponseCode ().equalsIgnoreCase ("00" )) {
199+ mView .onChargeCardSuccessful (response );
200+
201+ } else {
202+
203+ String authModelUsed = response .getData ().getAuthModelUsed ();
204+
205+ if (authModelUsed != null ) {
206+
207+ if (authModelUsed .equalsIgnoreCase (VBV ) || authModelUsed .equalsIgnoreCase (AVS_VBVSECURECODE )) {
208+ String authUrlCrude = response .getData ().getAuthurl ();
209+ String flwRef = response .getData ().getFlwRef ();
210+
211+ mView .onVBVAuthModelUsed (authUrlCrude , flwRef );
212+ } else if (authModelUsed .equalsIgnoreCase (GTB_OTP )
213+ || authModelUsed .equalsIgnoreCase (ACCESS_OTP )
214+ || authModelUsed .toLowerCase ().contains ("otp" )
215+ || authModelUsed .equalsIgnoreCase (PIN )) {
216+ String flwRef = response .getData ().getFlwRef ();
217+ String chargeResponseMessage = response .getData ().getChargeResponseMessage ();
218+ chargeResponseMessage = (chargeResponseMessage == null || chargeResponseMessage .length () == 0 ) ? enterOTP : chargeResponseMessage ;
219+ mView .showOTPLayout (flwRef , chargeResponseMessage );
220+
221+ } else if (authModelUsed .equalsIgnoreCase (NOAUTH )) {
222+ String flwRef = response .getData ().getFlwRef ();
223+ mView .onNoAuthUsed (flwRef , payload .getPBFPubKey ());
224+ } else {
225+ mView .onPaymentError (unknownAuthmsg );
226+ }
227+ } else {
228+ mView .onPaymentError (unknownAuthmsg );
212229 }
213230 }
214231 }
215232 } else {
216233 mView .onPaymentError (noResponse );
217234 }
218-
219235 }
220236
221237 @ Override
@@ -230,42 +246,7 @@ public void onError(String message, String responseAsJSONString) {
230246 public void chargeSavedCard (Payload payload , String encryptionKey ) {
231247 if (payload .getOtp () == null || payload .getOtp () == "" ) {
232248 sendRaveOTP (payload );
233- } else {
234- // Charge saved card
235- String cardRequestBodyAsString = Utils .convertChargeRequestPayloadToJson (payload );
236- String encryptedCardRequestBody = Utils .getEncryptedData (cardRequestBodyAsString , encryptionKey );
237-
238- final ChargeRequestBody body = new ChargeRequestBody ();
239- body .setAlg ("3DES-24" );
240- body .setPBFPubKey (payload .getPBFPubKey ());
241- body .setClient (encryptedCardRequestBody );
242-
243- mView .showProgressIndicator (true );
244-
245- networkRequest .charge (body , new Callbacks .OnChargeRequestComplete () {
246- @ Override
247- public void onSuccess (ChargeResponse response , String responseAsJSONString ) {
248-
249- mView .showProgressIndicator (false );
250-
251- if (response .getData () != null ) {
252- Log .d ("Saved card charge" , responseAsJSONString );
253- mView .onChargeCardSuccessful (response );
254-
255- } else {
256- mView .onPaymentError ("No response data was returned" );
257- }
258-
259- }
260-
261- @ Override
262- public void onError (String message , String responseAsJSONString ) {
263-
264- mView .showProgressIndicator (false );
265- mView .onPaymentError (message );
266- }
267- });
268- }
249+ } else chargeCard (payload , encryptionKey );
269250 }
270251
271252 @ Override
@@ -305,61 +286,9 @@ public void chargeCardWithAVSModel(Payload payload, String address, String city,
305286 payload .setBillingcountry (country );
306287 payload .setBillingstate (state );
307288
308- String cardRequestBodyAsString = Utils .convertChargeRequestPayloadToJson (payload );
309- String encryptedCardRequestBody = payloadEncryptor .getEncryptedData (cardRequestBodyAsString , encryptionKey ).trim ().replaceAll ("\\ n" , "" );
310-
311- ChargeRequestBody body = new ChargeRequestBody ();
312- body .setAlg ("3DES-24" );
313- body .setPBFPubKey (payload .getPBFPubKey ());
314- body .setClient (encryptedCardRequestBody );
315-
316- mView .showProgressIndicator (true );
317-
318-
319289 logEvent (new ChargeAttemptEvent ("AVS Card" ).getEvent (), payload .getPBFPubKey ());
320290
321-
322- networkRequest .charge (body , new Callbacks .OnChargeRequestComplete () {
323-
324- @ Override
325- public void onSuccess (ChargeResponse response , String responseAsJSONString ) {
326-
327- mView .showProgressIndicator (false );
328-
329- if (response .getData () != null && response .getData ().getChargeResponseCode () != null ) {
330- String chargeResponseCode = response .getData ().getChargeResponseCode ();
331-
332- if (chargeResponseCode .equalsIgnoreCase ("00" )) {
333- mView .onChargeCardSuccessful (response );
334- } else if (chargeResponseCode .equalsIgnoreCase ("02" )) {
335- String authModelUsed = response .getData ().getAuthModelUsed ();
336-
337- if (authModelUsed .equalsIgnoreCase (PIN )) {
338- String flwRef = response .getData ().getFlwRef ();
339- String chargeResponseMessage = response .getData ().getChargeResponseMessage ();
340- chargeResponseMessage = (chargeResponseMessage == null || chargeResponseMessage .length () == 0 ) ? enterOTP : chargeResponseMessage ;
341- mView .showOTPLayout (flwRef , chargeResponseMessage );
342- } else if (authModelUsed .equalsIgnoreCase (VBV )) {
343- String flwRef = response .getData ().getFlwRef ();
344- mView .onAVSVBVSecureCodeModelUsed (response .getData ().getAuthurl (), flwRef );
345- } else {
346- mView .onPaymentError (unknownAuthmsg );
347- }
348- } else {
349- mView .onPaymentError (unknownResCodemsg );
350- }
351- } else {
352- mView .onPaymentError (invalidChargeCode );
353- }
354-
355- }
356-
357- @ Override
358- public void onError (String message , String responseAsJSONString ) {
359- mView .showProgressIndicator (false );
360- mView .onPaymentError (message );
361- }
362- });
291+ chargeCard (payload , encryptionKey );
363292
364293 }
365294
@@ -549,7 +478,7 @@ public void processSavedCardTransaction(SavedCard savedCard, RavePayInitializer
549478
550479
551480 @ Override
552- public void chargeCardWithSuggestedAuthModel (Payload payload , String zipOrPin , String authModel , String encryptionKey ) {
481+ public void chargeCardWithSuggestedAuthModel (final Payload payload , String zipOrPin , String authModel , String encryptionKey ) {
553482
554483 if (authModel .equalsIgnoreCase (AVS_VBVSECURECODE )) {
555484 payload .setBillingzip (zipOrPin );
@@ -559,60 +488,7 @@ public void chargeCardWithSuggestedAuthModel(Payload payload, String zipOrPin, S
559488
560489 payload .setSuggestedAuth (authModel );
561490
562- String cardRequestBodyAsString = Utils .convertChargeRequestPayloadToJson (payload );
563- String encryptedCardRequestBody = payloadEncryptor .getEncryptedData (cardRequestBodyAsString , encryptionKey ).trim ().replaceAll ("\\ n" , "" );
564-
565- ChargeRequestBody body = new ChargeRequestBody ();
566- body .setAlg ("3DES-24" );
567- body .setPBFPubKey (payload .getPBFPubKey ());
568- body .setClient (encryptedCardRequestBody );
569-
570- mView .showProgressIndicator (true );
571-
572- logEvent (new ChargeAttemptEvent ("Card" ).getEvent (), payload .getPBFPubKey ());
573-
574-
575- networkRequest .charge (body , new Callbacks .OnChargeRequestComplete () {
576- @ Override
577- public void onSuccess (ChargeResponse response , String responseAsJSONString ) {
578-
579- mView .showProgressIndicator (false );
580-
581- if (response .getData () != null && response .getData ().getChargeResponseCode () != null ) {
582- String chargeResponseCode = response .getData ().getChargeResponseCode ();
583-
584- if (chargeResponseCode .equalsIgnoreCase ("00" )) {
585- mView .onChargeCardSuccessful (response );
586- } else if (chargeResponseCode .equalsIgnoreCase ("02" )) {
587-
588- String authModelUsed = response .getData ().getAuthModelUsed ();
589-
590- if (authModelUsed .equalsIgnoreCase (PIN )) {
591- String flwRef = response .getData ().getFlwRef ();
592- String chargeResponseMessage = response .getData ().getChargeResponseMessage ();
593- chargeResponseMessage = (chargeResponseMessage == null || chargeResponseMessage .length () == 0 ) ? "Enter your one time password (OTP)" : chargeResponseMessage ;
594- mView .showOTPLayout (flwRef , chargeResponseMessage );
595- } else if (authModelUsed .equalsIgnoreCase (AVS_VBVSECURECODE )) {
596- String flwRef = response .getData ().getFlwRef ();
597- mView .onAVSVBVSecureCodeModelUsed (response .getData ().getAuthurl (), flwRef );
598- } else {
599- mView .onPaymentError (unknownAuthmsg );
600- }
601- } else {
602- mView .onPaymentError (unknownResCodemsg );
603- }
604- } else {
605- mView .onPaymentError (invalidChargeCode );
606- }
607-
608- }
609-
610- @ Override
611- public void onError (String message , String responseAsJSONString ) {
612- mView .showProgressIndicator (false );
613- mView .onPaymentError (message );
614- }
615- });
491+ chargeCard (payload , encryptionKey );
616492
617493 }
618494
0 commit comments