Skip to content

Commit

Permalink
Fix for DKA
Browse files Browse the repository at this point in the history
  • Loading branch information
i.khafizov committed Aug 19, 2022
1 parent 170447d commit b3a0724
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ internal object AsdkLocalization {
resources = parser.parse()
}

fun isInitialized() = this::resources.isInitialized

private fun resolveLanguage(language: Language?): Language {
return language ?: when (Locale.getDefault().language) {
"ru", "ua", "kz", "by", "az", "os", "hy", "tg", "tk" -> Language.RU //страны СНГ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@ internal class LocalizationResources {

@SerializedName("SbpWidget.AppsNotFound.ButtonBrowser")
var sbpWidgetAppsNotFoundButtonBrowser: String? = null

@SerializedName("ThreeDs.Confirmation")
var threeDsConfirmation: String? = null

@SerializedName("ThreeDs.Cancel")
var threeDsCancel: String? = null
}
20 changes: 20 additions & 0 deletions ui/src/main/java/ru/tinkoff/acquiring/sdk/threeds/ThreeDsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import ru.tinkoff.acquiring.sdk.AcquiringSdk
import ru.tinkoff.acquiring.sdk.localization.AsdkLocalization
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsCertInfo.CertType.Companion.toWrapperType
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper.Companion.cancelButtonCustomization
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper.Companion.setSdkAppId
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper.Companion.submitButtonCustomization
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper.Companion.toolbarCustomization
import java.util.UUID
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -52,12 +56,28 @@ object ThreeDsHelper {
private val okHttpClient = OkHttpClient()
private val gson = Gson()

internal var threeDsStatus: ThreeDsStatus? = null

suspend fun initWrapper(context: Context): ThreeDSWrapper {
val localisation = if (AsdkLocalization.isInitialized()) AsdkLocalization.resources else null

val wrapper = ThreeDSWrapper(when (AcquiringSdk.isDeveloperMode) {
true -> ThreeDSWrapper.EmbeddedCertsInfo.TEST
else -> ThreeDSWrapper.EmbeddedCertsInfo.PROD
}).init(context, ThreeDSWrapper.newConfigParameters {
setSdkAppId(getSdkAppId(context).toString())
}, null, ThreeDSWrapper.newUiCustomization {
toolbarCustomization {
headerText = localisation?.threeDsConfirmation ?: "Confirmation"
buttonText = localisation?.threeDsCancel ?: "Cancel"
backgroundColor = "#888888"
}
submitButtonCustomization {
backgroundColor = "#ffdd2d"
}
cancelButtonCustomization {
textColor = "#ffffff"
}
})
val config = updateCertsConfigIfNeeded()
wrapper.updateCertsIfNeeded(config)
Expand Down
16 changes: 16 additions & 0 deletions ui/src/main/java/ru/tinkoff/acquiring/sdk/threeds/ThreeDsStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.tinkoff.acquiring.sdk.threeds

import ru.tinkoff.acquiring.sdk.models.ThreeDsData

sealed class ThreeDsStatus

class ThreeDsStatusSuccess(
val threeDsData: ThreeDsData,
val transStatus: String
): ThreeDsStatus()

class ThreeDsStatusCanceled(): ThreeDsStatus()

class ThreeDsStatusError(
val error: Throwable
): ThreeDsStatus()
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import ru.tinkoff.acquiring.sdk.models.ScreenState
import ru.tinkoff.acquiring.sdk.models.ThreeDsScreenState
import ru.tinkoff.acquiring.sdk.models.result.AsdkResult
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsHelper
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusCanceled
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusError
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusSuccess
import ru.tinkoff.acquiring.sdk.ui.customview.BottomContainer
import ru.tinkoff.acquiring.sdk.viewmodel.ThreeDsViewModel

Expand Down Expand Up @@ -64,6 +67,10 @@ internal open class TransparentActivity : BaseAcquiringActivity() {
showBottomView = it.getBoolean(STATE_SHOW_BOTTOM)
}

initThreeDs()
}

private fun initThreeDs() {
threeDsViewModel = provideViewModel(ThreeDsViewModel::class.java) as ThreeDsViewModel
threeDsViewModel.run {
loadStateLiveData.observe(this@TransparentActivity) { handleLoadState(it) }
Expand All @@ -79,6 +86,18 @@ internal open class TransparentActivity : BaseAcquiringActivity() {
}
}

override fun onResume() {
super.onResume()

when (val threeDsStatus = ThreeDsHelper.threeDsStatus) {
is ThreeDsStatusSuccess -> threeDsViewModel.submitAuthorization(threeDsStatus.threeDsData, threeDsStatus.transStatus)
is ThreeDsStatusCanceled -> finishWithCancel()
is ThreeDsStatusError -> finishWithError(threeDsStatus.error)
else -> Unit
}
ThreeDsHelper.threeDsStatus = null
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == THREE_DS_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK && data != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import com.emvco3ds.sdk.spec.RuntimeErrorEvent
import com.emvco3ds.sdk.spec.Transaction
import ru.tinkoff.acquiring.sdk.AcquiringSdk
import ru.tinkoff.acquiring.sdk.exceptions.AcquiringSdkException
import ru.tinkoff.acquiring.sdk.models.ErrorScreenState
import ru.tinkoff.acquiring.sdk.models.FinishWithErrorScreenState
import ru.tinkoff.acquiring.sdk.models.LoadState
import ru.tinkoff.acquiring.sdk.models.LoadedState
import ru.tinkoff.acquiring.sdk.models.LoadingState
import ru.tinkoff.acquiring.sdk.models.ThreeDsData
Expand All @@ -36,6 +33,9 @@ import ru.tinkoff.acquiring.sdk.models.result.AsdkResult
import ru.tinkoff.acquiring.sdk.models.result.CardResult
import ru.tinkoff.acquiring.sdk.models.result.PaymentResult
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsHelper
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusCanceled
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusError
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsStatusSuccess
import ru.tinkoff.acquiring.sdk.ui.activities.BaseAcquiringActivity
import ru.tinkoff.core.components.threedswrapper.ChallengeStatusReceiverAdapter
import ru.tinkoff.core.components.threedswrapper.ThreeDSWrapper
Expand Down Expand Up @@ -71,50 +71,42 @@ internal class ThreeDsViewModel(
override fun completed(event: CompletionEvent?) {
super.completed(event)
wrapper.cleanup(activity)
coroutine.doOnMain {
changeScreenState(LoadingState)
submitAuthorization(threeDsData, event!!.transactionStatus)
}
ThreeDsHelper.threeDsStatus = ThreeDsStatusSuccess(threeDsData, event!!.transactionStatus)
}

override fun cancelled() {
super.cancelled()
wrapper.cleanup(activity)
coroutine.doOnMain {
activity.finishWithCancel()
}
ThreeDsHelper.threeDsStatus = ThreeDsStatusCanceled()
}

override fun timedout() {
super.timedout()
wrapper.cleanup(activity)
coroutine.doOnMain {
changeScreenState(ErrorScreenState("3DS SDK transaction timeout"))
}
val error = RuntimeException("3DS SDK transaction timeout")
ThreeDsHelper.threeDsStatus = ThreeDsStatusError(error)
}

override fun protocolError(event: ProtocolErrorEvent?) {
super.protocolError(event)
wrapper.cleanup(activity)
val errorMessage = "3DS SDK protocol error: sdkTransactionID - ${event?.sdkTransactionID}, message - ${event?.errorMessage}"
coroutine.doOnMain {
changeScreenState(FinishWithErrorScreenState(RuntimeException(errorMessage)))
}
val error = RuntimeException("3DS SDK protocol error: sdkTransactionID - ${event?.sdkTransactionID}, message - ${event?.errorMessage}")
ThreeDsHelper.threeDsStatus = ThreeDsStatusError(error)
}

override fun runtimeError(event: RuntimeErrorEvent?) {
super.runtimeError(event)
wrapper.cleanup(activity)
val errorMessage = "3DS SDK runtime error: code - ${event?.errorCode}, message - ${event?.errorMessage}"
coroutine.doOnMain {
changeScreenState(FinishWithErrorScreenState(AcquiringSdkException(RuntimeException(errorMessage))))
}
val error = RuntimeException("3DS SDK runtime error: code - ${event?.errorCode}, message - ${event?.errorMessage}")
ThreeDsHelper.threeDsStatus = ThreeDsStatusError(error)
}
}, ThreeDsHelper.maxTimeout)
}
}

private fun submitAuthorization(threeDsData: ThreeDsData, transStatus: String) {
fun submitAuthorization(threeDsData: ThreeDsData, transStatus: String) {
changeScreenState(LoadingState)

val request = sdk.submit3DSAuthorization(threeDsData.tdsServerTransId!!, transStatus)

coroutine.call(request,
Expand Down
4 changes: 3 additions & 1 deletion ui/src/main/res/raw/acq_localization_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@
"SbpWidget.AppsNotFound.Title": "Banking applications not found",
"SbpWidget.AppsNotFound.Description": "To pay via FPS, you must have installed banking application",
"SbpWidget.AppsNotFound.Button": "OK",
"SbpWidget.AppsNotFound.ButtonBrowser": "Information on the FPS website"
"SbpWidget.AppsNotFound.ButtonBrowser": "Information on the FPS website",
"ThreeDs.Confirmation": "Confirmation",
"ThreeDs.Cancel": "Cancel"
}
4 changes: 3 additions & 1 deletion ui/src/main/res/raw/acq_localization_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@
"SbpWidget.AppsNotFound.Title": "Не найдено ни одного банковского приложения",
"SbpWidget.AppsNotFound.Description": "Для оплаты через СБП необходимо иметь установленными банковские приложения",
"SbpWidget.AppsNotFound.Button": "Понятно",
"SbpWidget.AppsNotFound.ButtonBrowser": "Информация на сайте сбп"
"SbpWidget.AppsNotFound.ButtonBrowser": "Информация на сайте сбп",
"ThreeDs.Confirmation": "Подтверждение",
"ThreeDs.Cancel": "Отмена"
}

0 comments on commit b3a0724

Please sign in to comment.