Jus Pay latency Fix

This commit is contained in:
amitkumar
2022-01-29 11:12:13 +05:30
parent 44b5bccb96
commit 09c3f42d7d
9 changed files with 269 additions and 103 deletions

View File

@@ -358,6 +358,9 @@ dependencies {
implementation 'com.google.dagger:hilt-android:2.38.1'
kapt 'com.google.dagger:hilt-compiler:2.38.1'
//JusPay
implementation 'in.juspay:hypersdk:2.0.4-rc.26'
// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.38.1'

View File

@@ -14,10 +14,12 @@ import com.navi.amc.investorapp.extension.showToast
import com.navi.analytics.neoeyed.NeoEyedUtil
import com.navi.common.constants.GI
import com.navi.common.constants.NPS_SUBMIT_DIALOG
import com.navi.common.juspay.HyperServicesHolder
import com.navi.common.model.CtaData
import com.navi.common.model.PreviousScreenNameRequest
import com.navi.common.sharedpref.PreferenceManager
import com.navi.common.utils.Constants.LOGIN_SOURCE
import com.navi.insurance.health.activity.JusPayUtil
import com.navi.insurance.navigator.NaviInsuranceDeeplinkNavigator
import com.navi.insurance.util.AMOUNT
import com.navi.insurance.util.Constants.PAYMENTS_STATUS_IS_USER_CANCELLED_EXTRA
@@ -54,6 +56,7 @@ import com.naviapp.utils.Constants.OPEN_APP_COUNT_SINCE_LAST_LOGIN
import com.naviapp.utils.Constants.PREVIOUS_SCREEN
import com.naviapp.utils.Constants.PTP_CAPTURE
import com.naviapp.utils.Constants.SUCCESS
import java.util.*
class DashboardActivity : DashboardBaseActivity(),
FragmentInteractionListener, View.OnClickListener,
@@ -83,6 +86,15 @@ class DashboardActivity : DashboardBaseActivity(),
fetchPtpDialogData()
onInitProviderConfig()
fetchUxCamConfig(Constants.SA, PAGE_HOME)
//Initialising JusPay
HyperServicesHolder.initiateSDK(
this,
JusPayUtil.getSDKPayload(
JusPayUtil.getInitiatePayload(),
UUID.randomUUID().toString()
)
)
}
private fun initObservers() {

View File

@@ -63,4 +63,7 @@ dependencies {
// Gson
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
//JusPay
implementation 'in.juspay:hypersdk:2.0.4-rc.26'
}

View File

@@ -0,0 +1,137 @@
package com.navi.common.juspay
import `in`.juspay.hypersdk.core.MerchantViewType
import `in`.juspay.hypersdk.data.JuspayResponseHandler
import `in`.juspay.hypersdk.ui.HyperPaymentsCallback
import `in`.juspay.services.HyperServices
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import androidx.fragment.app.FragmentActivity
import org.json.JSONObject
import java.lang.ref.WeakReference
import java.util.*
object HyperServicesHolder : HyperPaymentsCallback {
private var hyperServices: WeakReference<HyperServices>? = null
lateinit var hyperServiceCallback: HyperPaymentsCallback
enum class EventsType {
onEvent, onStartWaitingDialogCreated, onWebViewReady, getMerchantView
}
private val eventQueue: Queue<QueuedEvents> = LinkedList()
var isInitiated = false
fun initiateSDK(activity: FragmentActivity, initiatePayload: JSONObject) {
hyperServices = WeakReference<HyperServices>(HyperServices(activity))
hyperServices?.get()?.initiate(activity, initiatePayload, this)
}
fun processSDK(
activity: FragmentActivity,
initiatePayload: JSONObject,
processPayload: JSONObject,
paymentsCallback: HyperPaymentsCallback
) {
if (hyperServices?.get()?.isInitialised == false) {
initiateSDK(activity, initiatePayload)
}
hyperServiceCallback = paymentsCallback
runQueueEvents() // executing events in queue when callback is set
hyperServices?.get()?.process(activity, processPayload)
}
// callback handlers which add events to queue when callback is not set
// queued events are fired later when call back is set during process call
override fun onEvent(event: JSONObject?, handler: JuspayResponseHandler?) {
val eventName = if (event == null) "" else event.optString("event", "")
if (eventName == "initiate_result")
isInitiated = true
if (!this::hyperServiceCallback.isInitialized) {
val qEvent = QueuedEvents()
qEvent.eventType = EventsType.onEvent
qEvent.event = event
qEvent.handler = handler
eventQueue.add(qEvent)
} else {
hyperServiceCallback.onEvent(event, handler)
}
}
override fun onStartWaitingDialogCreated(parent: View?) {
if (!this::hyperServiceCallback.isInitialized) {
val qEvent = QueuedEvents()
qEvent.eventType = EventsType.onStartWaitingDialogCreated
qEvent.parent = parent
eventQueue.add(qEvent)
} else {
hyperServiceCallback.onStartWaitingDialogCreated(parent);
}
}
override fun getMerchantView(parent: ViewGroup?, viewType: MerchantViewType): View? {
if (!this::hyperServiceCallback.isInitialized) {
val qEvent = QueuedEvents()
qEvent.eventType = EventsType.getMerchantView
qEvent.viewGroup = parent
qEvent.mViewType = viewType
eventQueue.add(qEvent)
} else {
hyperServiceCallback.getMerchantView(parent, viewType)
}
return null
}
override fun createJuspaySafeWebViewClient(): WebViewClient? {
return null;
}
private fun runQueueEvents() {
val head = eventQueue.poll()
if (head != null) {
when (head.eventType) {
EventsType.onEvent ->
if (this::hyperServiceCallback.isInitialized)
hyperServiceCallback.onEvent(head.event, head.handler)
EventsType.onStartWaitingDialogCreated ->
if (this::hyperServiceCallback.isInitialized)
hyperServiceCallback.onStartWaitingDialogCreated(head.parent)
EventsType.getMerchantView ->
if (this::hyperServiceCallback.isInitialized)
hyperServiceCallback.getMerchantView(head.viewGroup, head.mViewType)
else -> {
}
}
runQueueEvents()
}
}
fun handleBackPress(): Boolean {
if (hyperServices?.get()?.isInitialised == true) {
// returns not of
// if hyper-services is handling backPress
return !(hyperServices?.get()?.onBackPressed() ?: false)
}
return true
}
}
internal class QueuedEvents {
var event: JSONObject? = null
var handler: JuspayResponseHandler? = null
var eventType: HyperServicesHolder.EventsType? = null
var viewGroup: ViewGroup? = null
var parent: View? = null
var mViewType: MerchantViewType? = null
}

View File

@@ -158,7 +158,6 @@ dependencies {
implementation 'in.payu:native-otp-assist:1.2.0'
implementation 'in.payu:payu-gpay:1.4.0'
implementation 'in.payu:phonepe-intent:1.6.1'
implementation 'in.juspay:hypersdk:2.0.4-rc.26'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'com.github.bumptech.glide:glide:4.10.0'
@@ -175,4 +174,7 @@ dependencies {
testImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptTest 'com.google.dagger:hilt-compiler:2.38.1'
//JusPay
implementation 'in.juspay:hypersdk:2.0.4-rc.26'
}

View File

@@ -1,6 +1,6 @@
package com.navi.insurance.health.activity
import com.navi.insurance.models.JusPayInitiatePayload
import com.navi.insurance.BuildConfig
import com.navi.insurance.models.JusPayProcessPayload
import org.json.JSONObject
@@ -29,6 +29,13 @@ object JusPayUtil {
const val USER_CANCELLED = "BACKPRESSED"
const val PROCESS_RESULT = "process_result"
const val ENVIRONMENT_PRODUCTION = "production"
const val ENVIRONMENT_SANDBOX = "sandbox"
const val CLIENT_ID = "navi"
const val MERCHANT_ID = "navi"
const val ACTION_INITIATE = "initiate"
const val FLAVOR_PROD = "prod"
fun getSDKPayload(payload: JSONObject?, requestId: String): JSONObject {
val sdkPayload = JSONObject()
@@ -50,10 +57,7 @@ object JusPayUtil {
processPayload.put(ARG_MERCHANT_KEY_ID, this.merchantKeyId)
putValueInJSON(
processPayload,
jusPayProcessPayload.action,
jusPayProcessPayload.clientId,
jusPayProcessPayload.merchantId,
jusPayProcessPayload.environment
jusPayProcessPayload.action
)
processPayload
} ?: kotlin.run {
@@ -61,35 +65,24 @@ object JusPayUtil {
}
}
fun getInitiateCallPayload(justPayInitiatePayload: JusPayInitiatePayload?): JSONObject? {
return justPayInitiatePayload?.run {
val initiatePayload = JSONObject()
putValueInJSON(
initiatePayload,
justPayInitiatePayload.action,
justPayInitiatePayload.clientId,
justPayInitiatePayload.merchantId,
justPayInitiatePayload.environment
)
initiatePayload
} ?: kotlin.run {
null
}
}
private fun putValueInJSON(
json: JSONObject,
action: String?,
clientId: String?,
merchantId: String?,
environment: String?
action: String?
) {
json.put(ARG_ACTION, action)
json.put(ARG_CLIENT_ID, clientId)
json.put(ARG_MERCHANT_ID, merchantId)
json.put(ARG_ENVIRONMENT, environment)
json.put(ARG_CLIENT_ID, CLIENT_ID)
json.put(ARG_MERCHANT_ID, MERCHANT_ID)
json.put(ARG_ENVIRONMENT, getEnvironment())
}
fun getInitiatePayload(): JSONObject {
val initiatePayload = JSONObject()
putValueInJSON(initiatePayload, ACTION_INITIATE)
return initiatePayload
}
private fun getEnvironment() =
if (BuildConfig.FLAVOR == FLAVOR_PROD) ENVIRONMENT_PRODUCTION else ENVIRONMENT_SANDBOX
}

View File

@@ -1,18 +1,21 @@
package com.navi.insurance.health.activity
import `in`.juspay.hypersdk.core.MerchantViewType
import `in`.juspay.hypersdk.data.JuspayResponseHandler
import `in`.juspay.hypersdk.ui.HyperPaymentsCallbackAdapter
import `in`.juspay.services.HyperServices
import `in`.juspay.hypersdk.ui.HyperPaymentsCallback
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.gson.Gson
import com.navi.common.juspay.HyperServicesHolder
import com.navi.common.sharedpref.CommonPrefConstants
import com.navi.common.sharedpref.PreferenceManager
import com.navi.insurance.R
@@ -22,7 +25,7 @@ import com.navi.insurance.databinding.ScreenPaymentBinding
import com.navi.insurance.health.PolicyPaymentType
import com.navi.insurance.health.activity.JusPayUtil.ARG_EVENT
import com.navi.insurance.health.activity.JusPayUtil.ARG_STATUS
import com.navi.insurance.health.activity.JusPayUtil.getInitiateCallPayload
import com.navi.insurance.health.activity.JusPayUtil.getInitiatePayload
import com.navi.insurance.health.activity.JusPayUtil.getProcessCallPayload
import com.navi.insurance.health.activity.JusPayUtil.getSDKPayload
import com.navi.insurance.health.interfaces.JusPayListener
@@ -62,6 +65,8 @@ import com.razorpay.PaymentResultWithDataListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.util.*
import kotlin.collections.HashMap
class PaymentActivity : BaseActivity(), PaymentResultWithDataListener, PaymentListener,
@@ -80,13 +85,10 @@ class PaymentActivity : BaseActivity(), PaymentResultWithDataListener, PaymentLi
var isUserCancelled = false
var policyId: String = ""
private val analyticsEventTracker = NaviInsuranceAnalytics.instance.PaymentActivity()
var hyperInstance: HyperServices? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.screen_payment)
hyperInstance = HyperServices(this, binding.formFragment)
binding.paymentLoadingPaymentMsg.visibility = View.VISIBLE
binding.paymentVerifyingPaymentMsg.visibility = View.GONE
binding.paymentLoading.visibility = View.VISIBLE
@@ -260,57 +262,63 @@ class PaymentActivity : BaseActivity(), PaymentResultWithDataListener, PaymentLi
Pair(NaviInsuranceAnalytics.PAYMENT_REFERENCE_ID, paymentReferenceId)
)
)
hyperInstance?.let {
if (!it.isInitialised) {
it.initiate(
getSDKPayload(
getInitiateCallPayload(jusPayMetaData?.initiatePayload),
paymentReferenceId
),
object :
HyperPaymentsCallbackAdapter() {
override fun onEvent(data: JSONObject, handler: JuspayResponseHandler?) {
lifecycleScope.launch(Dispatchers.Main) {
try {
val event: String? = data.getString(ARG_EVENT)
analyticsEventTracker.jusPayInitiated(
mapOf(
Pair("screen", screenName),
Pair("paymentReferenceId", paymentReferenceId),
Pair("event", event ?: "")
)
)
if (event == JusPayUtil.PROCESS_RESULT) {
val response: JSONObject? =
data.optJSONObject(JusPayUtil.ARG_PAYLOAD)
val status = response?.getString(ARG_STATUS)
if (JusPayUtil.USER_CANCELLED.equals(status, true)) {
onJusPayPaymentCancel(status)
} else if (JusPayUtil.CHARGED.equals(
status,
true
) || JusPayUtil.PENDING_VBV.equals(
status,
true
) || JusPayUtil.AUTHORIZING.equals(status, true)
) {
onJusPayPaymentSuccess(status)
} else {
onJusPayPaymentError(status)
}
}
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
onJusPayPaymentError("exception")
}
HyperServicesHolder.processSDK(
this,
initiatePayload = getSDKPayload(getInitiatePayload(), UUID.randomUUID().toString()),
processPayload = getSDKPayload(
getProcessCallPayload(jusPayMetaData?.processPayload),
requestId
),
object : HyperPaymentsCallback {
override fun onStartWaitingDialogCreated(p0: View?) {
}
override fun onEvent(data: JSONObject?, hander: JuspayResponseHandler?) {
try {
val event: String? = data?.getString(ARG_EVENT)
analyticsEventTracker.jusPayInitiated(
mapOf(
Pair("screen", screenName),
Pair("paymentReferenceId", paymentReferenceId),
Pair("event", event ?: "")
)
)
if (event == JusPayUtil.PROCESS_RESULT) {
val response: JSONObject? =
data.optJSONObject(JusPayUtil.ARG_PAYLOAD)
val status = response?.getString(ARG_STATUS)
if (JusPayUtil.USER_CANCELLED.equals(status, true)) {
onJusPayPaymentCancel(status)
} else if (JusPayUtil.CHARGED.equals(
status,
true
) || JusPayUtil.PENDING_VBV.equals(
status,
true
) || JusPayUtil.AUTHORIZING.equals(status, true)
) {
onJusPayPaymentSuccess(status)
} else {
onJusPayPaymentError(status)
}
}
})
}
it.process(
getSDKPayload(getProcessCallPayload(jusPayMetaData?.processPayload), requestId)
)
}
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
onJusPayPaymentError("exception")
}
}
override fun getMerchantView(p0: ViewGroup?, p1: MerchantViewType?): View? {
return null
}
override fun createJuspaySafeWebViewClient(): WebViewClient? {
return null
}
})
// }
}
private fun isPayUEnabled(it: PaymentRequest) =
@@ -908,8 +916,10 @@ class PaymentActivity : BaseActivity(), PaymentResultWithDataListener, PaymentLi
onPaymentCancel()
}
override fun onDestroy() {
super.onDestroy()
hyperInstance?.terminate()
override fun onBackPressed() {
val handleBackPress = HyperServicesHolder.handleBackPress()
if (handleBackPress) {
super.onBackPressed()
}
}
}

View File

@@ -32,23 +32,10 @@ data class PaymentMetaDataInfo(
)
data class JusPayMetaData(
@SerializedName("initiatePayload")
var initiatePayload: JusPayInitiatePayload? = null,
@SerializedName("processPayload")
var processPayload: JusPayProcessPayload? = null
)
open class JusPayInitiatePayload(
@SerializedName("action")
var action: String? = null,
@SerializedName("clientId")
var clientId: String? = null,
@SerializedName("environment")
var environment: String? = null,
@SerializedName("merchantId")
var merchantId: String? = null
)
data class JusPayProcessPayload(
@SerializedName("amount")
var amount: String? = null,
@@ -65,5 +52,13 @@ data class JusPayProcessPayload(
@SerializedName("orderId")
var orderId: String? = null,
@SerializedName("signature")
var signature: String? = null
) : JusPayInitiatePayload()
var signature: String? = null,
@SerializedName("action")
var action: String? = null,
@SerializedName("clientId")
var clientId: String? = null,
@SerializedName("environment")
var environment: String? = null,
@SerializedName("merchantId")
var merchantId: String? = null
)

View File

@@ -5,11 +5,13 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.navi.common.deeplink.DeepLinkManager
import com.navi.common.deeplink.util.DeeplinkConstants
import com.navi.common.juspay.HyperServicesHolder
import com.navi.common.model.CtaData
import com.navi.insurance.analytics.NaviInsuranceAnalytics
import com.navi.insurance.health.activity.BuyInsuranceActivity
import com.navi.insurance.health.activity.ChatActivity
import com.navi.insurance.health.activity.DashboardActivity
import com.navi.insurance.health.activity.JusPayUtil
import com.navi.insurance.health.repository.OfferRepository
import com.navi.insurance.health.repository.PolicyRepository
import com.navi.insurance.quoteredesign.QuoteActivity
@@ -21,6 +23,7 @@ import com.navi.insurance.util.isUserLogged
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
class DeepLinkActivity : AppCompatActivity() {
@@ -38,6 +41,14 @@ class DeepLinkActivity : AppCompatActivity() {
}
private fun handleIntent() {
//Initialising JusPay
HyperServicesHolder.initiateSDK(
this,
JusPayUtil.getSDKPayload(
JusPayUtil.getInitiatePayload(),
UUID.randomUUID().toString()
)
)
if (intent == null) {
navigateToSuperAppSplash()
} else {