@ -5,6 +5,7 @@ import android.util.Log;
import com.gmeremit.online.gmeremittance_native.R ;
import com.gmeremit.online.gmeremittance_native.base.BasePresenter ;
import com.gmeremit.online.gmeremittance_native.customwidgets.CustomAlertDialog ;
import com.gmeremit.online.gmeremittance_native.topup.local.LocalTopUpThrowable ;
import com.gmeremit.online.gmeremittance_native.topup.local.model.ButtonsGrid ;
import com.gmeremit.online.gmeremittance_native.topup.local.model.CardInfoDTO ;
import com.gmeremit.online.gmeremittance_native.topup.local.model.LocalTopUpDetailDTO ;
@ -18,24 +19,32 @@ import com.gmeremit.online.gmeremittance_native.utils.Constants;
import com.gmeremit.online.gmeremittance_native.utils.Utils ;
import com.gmeremit.online.gmeremittance_native.utils.https.GenericApiObserverResponseV2 ;
import com.gmeremit.online.gmeremittance_native.utils.https.GenericResponseDataModel ;
import com.gmeremit.online.gmeremittance_native.utils.https.HTTPConstants ;
import com.gmeremit.online.gmeremittance_native.utils.https.HttpClientV2 ;
import com.gmeremit.online.gmeremittance_native.utils.security.SecurityUtils ;
import com.google.gson.reflect.TypeToken ;
import java.io.IOException ;
import java.lang.reflect.Type ;
import java.net.SocketTimeoutException ;
import java.net.UnknownHostException ;
import java.util.List ;
import java.util.concurrent.TimeUnit ;
import java.util.regex.Pattern ;
import io.reactivex.Observable ;
import io.reactivex.android.schedulers.AndroidSchedulers ;
import io.reactivex.disposables.CompositeDisposable ;
import io.reactivex.functions.BiFunction ;
import io.reactivex.observers.DisposableObserver ;
import io.reactivex.schedulers.Schedulers ;
import io.reactivex.subjects.BehaviorSubject ;
import retrofit2.HttpException ;
public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPresenterInterface {
private static final int RETRIES_LIMIT = 3 ;
private final LocalTopUpContractInterface view ;
private final LocalTopUpInteractorInterface . LocalTopupGateway gateway ;
private final CompositeDisposable compositeDisposable ;
@ -67,7 +76,7 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
fetchUserBalance ( ) ;
this . userMsisdn = gateway . getUserMsisdn ( ) ;
if ( userMsisdn ! = null & & userMsisdn . length ( ) > 0 )
view . showUserMsisdn ( userMsisdn . replaceAll ( "010" , "" ) ) ;
view . showUserMsisdn ( userMsisdn . replaceAll ( "010" , "" ) ) ;
}
private void fetchUserBalance ( ) {
@ -95,13 +104,28 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
private void fetchLocalTopUpData ( ) {
this . compositeDisposable . add (
Observable . zip (
getLocalTopUpOnBasisCardType ( REGULAR_CARD_TYPE , userMsisdn )
. onErrorReturnItem ( new LocalTopUpResponseDTO ( ) ) ,
getLocalTopUpOnBasisCardType ( FIXED_CARD_TYPE , userMsisdn )
. onErrorReturnItem ( new LocalTopUpResponseDTO ( ) ) ,
getLocalTopUpOnBasisCardType ( DATA_PACK_CARD_TYPE , userMsisdn )
. onErrorReturnItem ( new LocalTopUpResponseDTO ( ) ) ,
getLocalTopUpOnBasisCardType ( REGULAR_CARD_TYPE , userMsisdn ) ,
getLocalTopUpOnBasisCardType ( FIXED_CARD_TYPE , userMsisdn ) ,
getLocalTopUpOnBasisCardType ( DATA_PACK_CARD_TYPE , userMsisdn ) ,
LocalTopUpRelatedData : : new )
/ / . retryWhen ( errors - > errors . zipWith (
/ / Observable . range ( 1 , RETRIES_LIMIT + 1 ) ,
/ / ( BiFunction < Throwable , Integer , Observable < Integer > > ) ( error , retryCount ) - > {
/ / if ( retryCount > RETRIES_LIMIT )
/ / return Observable . error ( error ) ;
/ / else if ( error instanceof IOException ) {
/ / return Observable . just ( retryCount ) ;
/ / / / return Observable . error ( new LocalTopUpThrowable ( HTTPConstants . HTTP_RESPONSE_NO_INTERNET ) ) ;
/ /
/ / }
/ / else if ( error instanceof HttpException | | error instanceof LocalTopUpThrowable )
/ / {
/ / return Observable . error ( error ) ;
/ / }
/ / else {
/ / return Observable . just ( retryCount ) ;
/ / }
/ / } ) . flatMap ( retryCount - > Observable . timer ( 200 , TimeUnit . SECONDS ) ) )
. subscribeOn ( Schedulers . io ( ) )
. doOnSubscribe ( d - > {
view . hideKeyBoard ( ) ;
@ -170,38 +194,30 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
@Override
public void updatePhoneNumber ( String phoneNo ) {
view . showUserMsisdnError ( null ) ;
if ( phoneNo = = null | | phoneNo . length ( ) < 1 )
if ( phoneNo = = null | | phoneNo . length ( ) < 1 )
return ;
String formattedPhoneNo = phoneNo . replaceAll ( "[^\\d]" , "" ) ;
if ( formattedPhoneNo . length ( ) < 11 )
{
userMsisdn = null ;
String formattedPhoneNo = phoneNo . replaceAll ( "[^\\d]" , "" ) ;
if ( formattedPhoneNo . length ( ) < 11 ) {
userMsisdn = null ;
view . showUserMsisdn ( "010" ) ;
view . showUserMsisdnError ( getStringfromStringId ( R . string . kyc_mobile_invalid_number_error ) ) ;
view . showTopUpScreens ( false ) ;
}
else
{
formattedPhoneNo = formattedPhoneNo . substring ( formattedPhoneNo . length ( ) - 11 ) ;
boolean b2 = Pattern . compile ( "010[0-9]{8}" ) . matcher ( formattedPhoneNo ) . matches ( ) ;
} else {
formattedPhoneNo = formattedPhoneNo . substring ( formattedPhoneNo . length ( ) - 11 ) ;
boolean b2 = Pattern . compile ( "010[0-9]{8}" ) . matcher ( formattedPhoneNo ) . matches ( ) ;
if ( b2 ) {
userMsisdn = formattedPhoneNo ;
view . showUserMsisdn ( formattedPhoneNo . replaceAll ( "010" , "" ) ) ;
}
else
{
if ( b2 ) {
userMsisdn = formattedPhoneNo ;
view . showUserMsisdn ( formattedPhoneNo . replaceAll ( "010" , "" ) ) ;
} else {
view . showUserMsisdn ( "010" ) ;
userMsisdn = null ;
userMsisdn = null ;
view . showUserMsisdnError ( getStringfromStringId ( R . string . kyc_mobile_invalid_number_error ) ) ;
view . showTopUpScreens ( false ) ;
}
}
}
@Override
@ -286,7 +302,6 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
LocalTopUpTxnRequestBodyDTO localTopUpTxnRequestBodyDTO = new LocalTopUpTxnRequestBodyDTO ( ) ;
localTopUpTxnRequestBodyDTO . setBuyType ( REGULAR_CARD_TYPE ) ;
localTopUpTxnRequestBodyDTO . setCustomerId ( this . gateway . getUserIDNumber ( ) ) ;
localTopUpTxnRequestBodyDTO . setOrderNo ( "gmesub_000000002" ) ;
localTopUpTxnRequestBodyDTO . setChargeType ( "" ) ;
localTopUpTxnRequestBodyDTO . setPhoneNo ( userMsisdn ) ;
localTopUpTxnRequestBodyDTO . setPrice ( regularTopUpValidator . selectedDeno . getPrice ( ) ) ;
@ -300,14 +315,11 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
localTopUpTxnRequestBodyDTO . setBuyType ( FIXED_CARD_TYPE ) ;
localTopUpTxnRequestBodyDTO . setCustomerId ( this . gateway . getUserIDNumber ( ) ) ;
localTopUpTxnRequestBodyDTO . setOrderNo ( "gmesub_000000006" ) ;
localTopUpTxnRequestBodyDTO . setPhoneNo ( userMsisdn ) ;
if ( fixedTopUpValidator . selectedCarrierPlan ! = null ) {
if ( fixedTopUpValidator . selectedCarrierPlan ! = null ) {
localTopUpTxnRequestBodyDTO . setChargeType ( fixedTopUpValidator . selectedCarrierPlan . getCardType ( ) ) ;
localTopUpTxnRequestBodyDTO . setPrice ( fixedTopUpValidator . selectedCarrierPlan . getFacePrice ( ) ) ;
}
else
{
} else {
localTopUpTxnRequestBodyDTO . setChargeType ( fixedTopUpValidator . selectedCarrierType . getCardCode ( ) ) ;
localTopUpTxnRequestBodyDTO . setPrice ( fixedTopUpValidator . selectedCarrierType . getFacePrice ( ) ) ;
}
@ -322,7 +334,6 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
localTopUpTxnRequestBodyDTO . setBuyType ( DATA_PACK_CARD_TYPE ) ;
localTopUpTxnRequestBodyDTO . setCustomerId ( this . gateway . getUserIDNumber ( ) ) ;
localTopUpTxnRequestBodyDTO . setOrderNo ( "gmesub_000000013" ) ;
localTopUpTxnRequestBodyDTO . setChargeType ( dataTopUpValidator . selectedCarrierPlan . getCardCode ( ) ) ;
localTopUpTxnRequestBodyDTO . setPhoneNo ( userMsisdn ) ;
localTopUpTxnRequestBodyDTO . setPrice ( dataTopUpValidator . selectedCarrierPlan . getFacePrice ( ) ) ;
@ -360,9 +371,12 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
GenericResponseDataModel < LocalTopUpResponseDTO > data = HttpClientV2 . getDeserializer ( ) . fromJson ( response . string ( ) , productType ) ;
if ( data . getErrorCode ( ) . equalsIgnoreCase ( Constants . SUCCESS_CODE_V2 ) )
return Observable . just ( data . getData ( ) ) ;
else if ( data . getErrorCode ( ) . equalsIgnoreCase ( "5" ) )
return Observable . error ( new LocalTopUpThrowable ( "You have a recent request for recharging.\\nWait for the results of this request.\" and then move Dashboard" ) ) ;
else
return Observable . error ( new Throwable ( data . getMsg ( ) ) ) ;
return Observable . error ( new LocalTopUp Throwable( data . getMsg ( ) ) ) ;
} ) ;
}
@ -441,14 +455,13 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
if ( result ) {
this . selectedCarrierPlan = selectedCarrierPlan ;
view . enableFixedTopUpButton ( result , getStringfromStringId ( R . string . continue_text ) ) ;
String data = null , voice = null , sms = null ;
if ( selectedCarrierPlan . getPlanInfo ( ) ! = null )
{
data = selectedCarrierPlan . getPlanInfo ( ) . getData ( ) ! = null & & selectedCarrierPlan . getPlanInfo ( ) . getData ( ) . length ( ) > 0 ? selectedCarrierPlan . getPlanInfo ( ) . getData ( ) : Constants . UNLIMITED_TEXT ;
voice = selectedCarrierPlan . getPlanInfo ( ) . getVoice ( ) ! = null & & selectedCarrierPlan . getPlanInfo ( ) . getVoice ( ) . length ( ) > 0 ? selectedCarrierPlan . getPlanInfo ( ) . getVoice ( ) : Constants . UNLIMITED_TEXT ;
sms = selectedCarrierPlan . getPlanInfo ( ) . getUnlimited ( ) ! = null & & selectedCarrierPlan . getPlanInfo ( ) . getUnlimited ( ) . length ( ) > 0 ? selectedCarrierPlan . getPlanInfo ( ) . getUnlimited ( ) : Constants . UNLIMITED_TEXT ;
String data = null , voice = null , sms = null ;
if ( selectedCarrierPlan . getPlanInfo ( ) ! = null ) {
data = selectedCarrierPlan . getPlanInfo ( ) . getData ( ) ;
voice = selectedCarrierPlan . getPlanInfo ( ) . getVoice ( ) ;
sms = selectedCarrierPlan . getPlanInfo ( ) . getSms ( ) ;
}
LocalTopUpDetailDTO localTopUpDetailDTO = new LocalTopUpDetailDTO ( userMsisdn , selectedCarrierPlan . getCardName ( ) , data , voice , sms , Constants . KOREAN_WON + " " + Utils . formatCurrencyWithoutTruncatingDecimal ( selectedCarrierPlan . getFacePrice ( ) ) ) ;
LocalTopUpDetailDTO localTopUpDetailDTO = new LocalTopUpDetailDTO ( userMsisdn , selectedCarrierPlan . getCardName ( ) , data , voice , sms , Constants . KOREAN_WON + " " + Utils . formatCurrencyWithoutTruncatingDecimal ( selectedCarrierPlan . getFacePrice ( ) ) ) ;
view . showPlanToFixedLocalTopUp ( localTopUpDetailDTO ) ;
} else {
this . selectedCarrierPlan = null ;
@ -496,7 +509,16 @@ public class LocalTopUpPresenter extends BasePresenter implements LocalTopUpPres
@Override
public void onError ( Throwable e ) {
Log . d ( "LocalTopUpError" , e . getMessage ( ) ) ;
String message = e . getMessage ( ) ;
if ( e instanceof HttpException )
{
message = HTTPConstants . getErrorMessageFromCode ( ( ( HttpException ) e ) . code ( ) ) ;
}
else if ( e instanceof IOException )
{
message = HTTPConstants . HTTP_RESPONSE_NO_INTERNET ;
}
view . showPopUpMessage ( message , CustomAlertDialog . AlertType . FAILED , alertType - > view . exitView ( ) ) ;
}
@Override