TP-72001 | Polling handling and removed redundant code (#11848)

This commit is contained in:
shreyansu raj
2024-07-19 17:58:24 +05:30
committed by GitHub
parent 148e76d338
commit 5d03f1f06c
3 changed files with 107 additions and 182 deletions

View File

@@ -9,14 +9,23 @@ package com.naviapp.custom_payments
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.navi.base.model.CtaData
import com.navi.base.utils.orElse
import com.navi.base.utils.orZero
import com.navi.common.firebasedb.FirebaseDataHelper
import com.navi.common.firebasedb.FirebaseDataReceiveListener
import com.navi.common.firebasedb.FirebaseResponse
import com.navi.common.firebasedb.FirebaseStatusType
import com.navi.common.firebasedb.PAYMENT_STATUS
import com.navi.common.listeners.FragmentInterchangeListener
import com.navi.common.model.ModuleNameV2
import com.navi.common.utils.ApiPollScheduler
import com.navi.common.utils.observeNullable
import com.navi.payment.model.common.PaymentSdkInitParams
import com.navi.payment.model.common.PaymentSdkTypes
@@ -65,6 +74,10 @@ class CustomPaymentActivity :
private val naviCheckoutViewModel by viewModels<NaviCheckoutViewModel>()
private var loanAccountNumber: String? = null
private var firebaseDataReceiveListener: FirebaseDataReceiveListener? = null
private var firebaseDataHelper: FirebaseDataHelper? = null
private var apiPollScheduler: ApiPollScheduler? = null
override fun onCreate(savedInstanceState: Bundle?) {
binding = DataBindingUtil.setContentView(this, R.layout.activity_custom_payment)
super.onCreate(savedInstanceState)
@@ -182,7 +195,6 @@ class CustomPaymentActivity :
private fun observePaymentListener() {
paymentVM.initPaymentResponse.observeNullable(this) { paymentResponse ->
hideLoader()
val paymentSdkInitParams =
PaymentSdkInitParams(
token = paymentResponse?.tokenDetails?.token.orEmpty(),
@@ -198,12 +210,17 @@ class CustomPaymentActivity :
)
naviCheckoutViewModel.initiatePayment(paymentSdkInitParams)
}
paymentVM.paymentStatusData.observeNullable(this) {
handleFirebaseResult(it.orEmpty(), PAYMENT_STATUS)
}
}
private fun observeInitiatePaymentStatus() {
lifecycleScope.launchWhenResumed {
naviCheckoutViewModel.paymentResponse.collect { data ->
if (data) {
hideLoader()
NaviPaymentRouter.startPayment(
composePaymentsResultLauncher,
this@CustomPaymentActivity
@@ -224,10 +241,10 @@ class CustomPaymentActivity :
com.navi.payment.utils.Constants.STATUS.uppercase(Locale.getDefault())
)
when (status) {
PaymentSdkTypes.DISMISS_LOADER.name -> Unit
PaymentSdkTypes.DISMISS_LOADER.name -> {}
PaymentSdkTypes.TRANSACTION_SUCCESS.name -> {
showLoader()
redirectToPaymentSuccessScreen()
apiPollInit()
}
else -> Unit
}
@@ -245,4 +262,73 @@ class CustomPaymentActivity :
startActivity(intent)
finish()
}
private fun apiPollInit() {
val config = paymentVM.initPaymentResponse.value?.requestConfig
val requestId = paymentVM.initPaymentResponse.value?.requestId.orEmpty()
firebaseInit(
requestId,
PAYMENT_STATUS,
paymentVM.initPaymentResponse.value?.notificationPath.orEmpty()
)
apiPollScheduler?.stopApiPoll()
apiPollScheduler =
ApiPollScheduler(
initialDelay = config?.initialDelay?.toLong().orZero(),
pollInterval =
config
?.interval
?.toLong()
.orElse(ApiPollScheduler.API_POLL_REPEAT_PERIOD_SECONDS),
numberOfRetry = config?.numOfRetries.orElse(ApiPollScheduler.API_POLL_RETRY_COUNT),
doOnTimeout = { hideLoader() }
) {
paymentVM.fetchSyncPaymentStatus(requestId)
}
apiPollScheduler?.scheduleApiPoll()
}
private fun firebaseInit(requestId: String, type: String, notificationPath: String) {
firebaseDataReceiveListener =
object : FirebaseDataReceiveListener {
override fun onDataReceive(firebaseResponse: FirebaseResponse?) {
firebaseResponse?.let {
apiPollScheduler?.stopApiPoll()
handleFirebaseResult(it.status.orEmpty(), type)
}
}
}
firebaseDataHelper = null
firebaseDataHelper = FirebaseDataHelper()
firebaseDataHelper?.initFirebaseDataReceiver(
lifecycle,
firebaseDataReceiveListener,
type,
requestId,
notificationPath
)
}
private fun handleFirebaseResult(status: String, type: String) {
when (type) {
PAYMENT_STATUS -> {
if (
TextUtils.equals(status, FirebaseStatusType.SUCCESS) ||
TextUtils.equals(status, FirebaseStatusType.FAILURE)
) {
hideLoader()
apiPollScheduler?.stopApiPoll()
handlePgRepaymentStatus(status)
}
}
}
}
private fun handlePgRepaymentStatus(status: String) {
when (status) {
FirebaseStatusType.SUCCESS -> {
redirectToPaymentSuccessScreen()
}
}
}
}

View File

@@ -121,10 +121,10 @@ import com.navi.pay.utils.DATE_TIME_FORMAT_DATE_MONTH_NAME_AT_TIME
import com.navi.pay.utils.KEY_CHECK_BALANCE_ACTION
import com.navi.pay.utils.KEY_VALUE_MAPPING
import com.navi.payment.listener.PaymentListener
import com.navi.payment.listener.PaymentSuccessListener
import com.navi.payment.model.clientmodels.PostPaymentData
import com.navi.payment.model.common.GenericErrorResponse
import com.navi.payment.model.common.PaymentSdkInitParams
import com.navi.payment.model.common.PaymentSdkTypes
import com.navi.payment.model.common.SDKIntegrationConfig
import com.navi.payment.model.initiatesdk.PaymentPrefetchMethodRequest
import com.navi.payment.model.paymentmethod.PaymentMethodResponse
@@ -135,7 +135,6 @@ import com.navi.payment.nativepayment.sharedviewmodel.NaviCheckoutViewModel
import com.navi.payment.paymenthandler.model.PaymentRequest
import com.navi.payment.paymenthandler.model.PaymentStatusData
import com.navi.payment.paymentscreen.model.PaymentActivityResponse
import com.navi.payment.paymentscreen.ui.PaymentMethodSuccessFragment
import com.navi.payment.paymentscreen.utils.PaymentNavigator
import com.navi.payment.paymentscreen.viewmodel.PaymentScreenSharedViewModel
import com.navi.payment.utils.Constants.FULL_PAYMENT_SCREEN
@@ -152,7 +151,6 @@ import com.naviapp.appupdate.activities.UpdateAppActivity
import com.naviapp.common.fragment.CommonBottomSheet
import com.naviapp.common.fragment.CommonDialogBox
import com.naviapp.common.fragment.DialogBoxWithSpinnerView
import com.naviapp.common.fragment.InfoBottomSheetV3
import com.naviapp.common.fragment.NetPromoterScoreFragment
import com.naviapp.common.fragment.OfferDialogFragment
import com.naviapp.common.fragment.PtpErrorFragment
@@ -196,7 +194,6 @@ import com.naviapp.models.response.OfferDialogType
import com.naviapp.models.response.PaymentInfo
import com.naviapp.nps.viewmodel.NpsVM
import com.naviapp.payment.activities.NaviPaymentActivity
import com.naviapp.payment.fragments.PaymentRedirectionStatus
import com.naviapp.payment.fragments.PaymentType
import com.naviapp.payment.listeners.PaymentInitListener
import com.naviapp.payment.models.Amount
@@ -1670,80 +1667,6 @@ class HomePageActivity :
apiPollScheduler = null
}
private fun getPaymentRedirectionStatus(status: String? = null): PaymentRedirectionStatus {
return when (status) {
FirebaseStatusType.SUCCESS -> {
if (paymentVM.shouldRedirectToFeatureCompletion(loanType, paymentType)) {
PaymentRedirectionStatus.SUCCESS_WITH_REDIRECTION_TO_CSAT_PAGE
} else {
PaymentRedirectionStatus.SUCCESS_WITH_REDIRECTION_TO_HOME_PAGE
}
}
FirebaseStatusType.FAILURE -> {
if (paymentVM.shouldRedirectToFeatureCompletion(loanType, paymentType)) {
if (loanType == Constants.TYPE_PERSONAL_LOAN) {
PaymentRedirectionStatus.PL_FAILURE_WITH_BOTTOM_SHEET
} else {
PaymentRedirectionStatus.FAILURE_WITH_REDIRECTION_TO_CSAT_PAGE
}
} else {
PaymentRedirectionStatus.FAILURE_WITH_REDIRECTION_TO_HOME_PAGE
}
}
else -> {
PaymentRedirectionStatus.NO_PAYMENT
}
}
}
private fun showPaymentSuccessLoader(
lottieFieldData: LottieFieldData,
onAnimationEnd: (() -> Unit)? = null
) {
val paymentSuccessFragment =
PaymentMethodSuccessFragment.newInstance(
Bundle().apply {
putParcelable(
com.navi.payment.utils.Constants.LOTTIE_FIELD_DATA,
lottieFieldData
)
}
)
paymentSuccessFragment.setPaymentSuccessListener(
object : PaymentSuccessListener {
override fun onBackPress() {}
override fun onAnimationEnd() {
onAnimationEnd?.invoke()
}
}
)
safelyShowDialogFragment(paymentSuccessFragment, PaymentMethodSuccessFragment.TAG)
}
private fun getLottieForPaymentSuccessful(): LottieFieldData {
val lottieCode =
if (paymentType == PaymentType.EMI.name) {
LottieEnums.FULL_SCREEN_EMI_SUCCESSFUL.name
} else {
LottieEnums.FULL_SCREEN_PAYMENT_SUCCESSFUL.name
}
return LottieFieldData(lottieCode = lottieCode, infiniteRepeat = false, repeatCount = 0)
}
private fun redirectToPaymentCompletionScreen(status: String?) {
paymentInitListener?.redirectToPaymentCompletionScreen(
loanAccountNumber = loanAccountNumber,
loanAccountRequestId = paymentVM.initPaymentResponse.value?.requestId.orEmpty(),
isSyncFlow = paymentVM.initPaymentResponse.value?.syncFlow.orFalse(),
status = status,
amount = amount,
paymentType = paymentType,
loanType = loanType
)
unInitData()
}
private fun unInitData() {
paymentRequestData = null
apiCallLastTime = 0
@@ -1751,25 +1674,6 @@ class HomePageActivity :
loanAccountNumber = null
}
private fun onPaymentSuccessOpenHomePage(isDelayNeeded: Boolean, status: String?) {
paymentInitListener?.onPaymentSuccessOpenHomePage(
isDelayNeeded,
isHomePageNeededToRelaunch,
status
)
unInitData()
}
private fun getLottieForPaymentFailed(): LottieFieldData {
val lottieCode =
if (paymentType == PaymentType.EMI.name) {
LottieEnums.FULL_SCREEN_EMI_FAILED.name
} else {
LottieEnums.FULL_SCREEN_PAYMENT_FAILED.name
}
return LottieFieldData(lottieCode = lottieCode, infiniteRepeat = false, repeatCount = 0)
}
private fun observePgRepaymentStatus() {
dashboardSharedVM.pgRepaymentStatus.observeNullable(this) {
it?.let { callPgRepaymentStatusApi(it) }
@@ -1834,69 +1738,17 @@ class HomePageActivity :
}
}
private fun onPaymentFailed() {
openPaymentFailureBottomSheet()
}
private fun openPaymentFailureBottomSheet() {
paymentFailureBottomSheetData?.let {
it.analyticsEvent?.name?.let { name ->
NaviTrackEvent.trackEventOnClickStream(name, it.analyticsEvent?.properties)
}
val bottomSheet = InfoBottomSheetV3.getInstance(it)
safelyShowBottomSheet(bottomSheet, InfoBottomSheetV3.TAG)
}
}
private fun handlePaymentStatus(isDelayNeeded: Boolean = false, status: String? = null) {
when (getPaymentRedirectionStatus(status)) {
PaymentRedirectionStatus.TRANSACTION_SUCCESS -> {
showPaymentSuccessLoader(
lottieFieldData =
LottieFieldData(
lottieCode = LottieEnums.PAYMENTS_COIN_LOADER.name,
infiniteRepeat = false,
repeatCount = 1
)
)
}
PaymentRedirectionStatus.SUCCESS_WITH_REDIRECTION_TO_CSAT_PAGE -> {
showPaymentSuccessLoader(
lottieFieldData = getLottieForPaymentSuccessful(),
onAnimationEnd = { redirectToPaymentCompletionScreen(status) }
)
dashboardSharedVM.setPgRepaymentResponseStatus(null)
}
PaymentRedirectionStatus.SUCCESS_WITH_REDIRECTION_TO_HOME_PAGE -> {
showPaymentSuccessLoader(
lottieFieldData = getLottieForPaymentSuccessful(),
onAnimationEnd = { onPaymentSuccessOpenHomePage(isDelayNeeded, status) }
)
dashboardSharedVM.setPgRepaymentResponseStatus(null)
}
PaymentRedirectionStatus.PL_FAILURE_WITH_BOTTOM_SHEET -> {
apiPollScheduler?.stopApiPoll()
when (status) {
FirebaseStatusType.SUCCESS -> {
hideLoader()
onPaymentFailed()
dashboardSharedVM.setPgRepaymentResponseStatus(null)
}
PaymentRedirectionStatus.FAILURE_WITH_REDIRECTION_TO_CSAT_PAGE -> {
showPaymentSuccessLoader(
lottieFieldData = getLottieForPaymentFailed(),
onAnimationEnd = { redirectToPaymentCompletionScreen(status) }
PaymentSdkResultUtil.redirectToPaymentSuccessScreen(
this,
loanAccountNumber,
paymentVM.initPaymentResponse.value?.requestId.toString()
)
dashboardSharedVM.setPgRepaymentResponseStatus(null)
}
PaymentRedirectionStatus.FAILURE_WITH_REDIRECTION_TO_HOME_PAGE -> {
showPaymentSuccessLoader(
lottieFieldData = getLottieForPaymentFailed(),
onAnimationEnd = { onPaymentSuccessOpenHomePage(isDelayNeeded, status) }
)
dashboardSharedVM.setPgRepaymentResponseStatus(null)
}
else -> {
apiPollScheduler?.stopApiPoll()
FirebaseStatusType.FAILURE -> {
hideLoader()
}
}
@@ -2061,6 +1913,7 @@ class HomePageActivity :
?.toLong()
.orElse(ApiPollScheduler.API_POLL_REPEAT_PERIOD_SECONDS),
numberOfRetry = config?.numOfRetries.orElse(ApiPollScheduler.API_POLL_RETRY_COUNT),
doOnTimeout = { hideLoader() }
) {
paymentVM.fetchSyncPaymentStatus(requestId)
}
@@ -2352,11 +2205,13 @@ class HomePageActivity :
?.getString(
com.navi.payment.utils.Constants.STATUS.uppercase(Locale.getDefault())
)
PaymentSdkResultUtil.paymentSdkCallbackStatus(
status,
this,
loanAccountNumber,
paymentVM.initPaymentResponse.value?.requestId.toString()
)
when (status) {
PaymentSdkTypes.DISMISS_LOADER.name -> Unit
PaymentSdkTypes.TRANSACTION_SUCCESS.name -> {
showLoader()
apiPollInit()
}
else -> Unit
}
}
}

View File

@@ -8,7 +8,6 @@
package com.naviapp.personalloanrevamp.common.utils
import android.content.Intent
import com.navi.payment.model.common.PaymentSdkTypes
import com.naviapp.home.compose.activity.HomePageActivity
import com.naviapp.payment.activities.FeedbackActivity
import com.naviapp.utils.Constants.LOAN_TYPE
@@ -19,22 +18,7 @@ import com.naviapp.utils.LOAN_ACCOUNT_REQUEST_ID
object PaymentSdkResultUtil {
fun paymentSdkCallbackStatus(
status: String?,
activity: HomePageActivity,
loanAccountNumber: String?,
requestId: String
) {
when (status) {
PaymentSdkTypes.DISMISS_LOADER.name -> Unit
PaymentSdkTypes.TRANSACTION_SUCCESS.name -> {
redirectToPaymentSuccessScreen(activity, loanAccountNumber, requestId)
}
else -> Unit
}
}
private fun redirectToPaymentSuccessScreen(
fun redirectToPaymentSuccessScreen(
activity: HomePageActivity,
loanAccountNumber: String?,
requestId: String