Skip to content

Commit

Permalink
Merge pull request #247 from tinkoff-mobile-tech/EACQAPW-4616_spb_new…
Browse files Browse the repository at this point in the history
…_deeplink

EACQAPW - sbp доработки нового алгоритма, мд + bump version
  • Loading branch information
jQwout authored Apr 26, 2023
2 parents 3f9c782 + 6f9ae2e commit 30df493
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package ru.tinkoff.acquiring.sdk.exceptions
*
* @param throwable - родительская ошибка
* @param message - дополнительное сообщение
* @param deeplink - сырой диплинк от backend
* @param deeplink - диплинк для октрытия прилоежния
* @param paymentId - идентификатор платежной сессии
*
* Created by i.golovachev
Expand Down
14 changes: 14 additions & 0 deletions ui/src/main/java/ru/tinkoff/acquiring/sdk/models/BankChooseInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.tinkoff.acquiring.sdk.models


/**
* Created by i.golovachev
*/
class BankChooseInfo(
val appsAndLinks: Map<String, String>
) : java.io.Serializable {

val apps: Set<String> get() = appsAndLinks.keys

fun getDeeplink(packageName: String) : String = appsAndLinks.getValue(packageName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ package ru.tinkoff.acquiring.sdk.models.result
/**
* @author Mariya Chernyadieva
*/
internal class BankChooseResult(val packageName: String) : AsdkResult
internal class BankChooseResult(val packageName: String, val deeplink: String) : AsdkResult
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import ru.tinkoff.acquiring.sdk.R
import ru.tinkoff.acquiring.sdk.models.*
import ru.tinkoff.acquiring.sdk.models.BrowserButtonClickedEvent
import ru.tinkoff.acquiring.sdk.models.ConfirmButtonClickedEvent
import ru.tinkoff.acquiring.sdk.models.OpenBankClickedEvent
Expand All @@ -34,6 +35,7 @@ import ru.tinkoff.acquiring.sdk.models.options.screen.BaseAcquiringOptions
import ru.tinkoff.acquiring.sdk.models.result.BankChooseResult
import ru.tinkoff.acquiring.sdk.ui.fragments.BanksNotFoundFragment
import ru.tinkoff.acquiring.sdk.ui.fragments.BankChooseFragment
import ru.tinkoff.acquiring.sdk.utils.lazyUnsafe
import ru.tinkoff.acquiring.sdk.viewmodel.BaseAcquiringViewModel

/**
Expand All @@ -42,24 +44,26 @@ import ru.tinkoff.acquiring.sdk.viewmodel.BaseAcquiringViewModel
internal class BankChooseActivity: TransparentActivity() {

private lateinit var viewModel: BaseAcquiringViewModel
private val banksInfo: BankChooseInfo by lazyUnsafe {
intent.getSerializableExtra(EXTRA_BANKS) as BankChooseInfo
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val banksList = intent.getStringArrayExtra(EXTRA_BANKS)
initViews(banksList.isNullOrEmpty())
initViews(banksInfo.appsAndLinks.isEmpty())

viewModel = provideViewModel(BaseAcquiringViewModel::class.java) as BaseAcquiringViewModel

if (savedInstanceState == null) {
if (banksList.isNullOrEmpty()) {
if (banksInfo.appsAndLinks.isEmpty()) {
showFragment(BanksNotFoundFragment())
} else {
showFragment(BankChooseFragment.newInstance(banksList.toCollection(arrayListOf())))
showFragment(BankChooseFragment.newInstance(banksInfo.apps.toCollection(arrayListOf())))
}
}

if (banksList.isNullOrEmpty()) {
if (banksInfo.appsAndLinks.isEmpty()) {
prepareToolbar()
val container = findViewById<View>(R.id.acq_activity_fl_container)
container.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
Expand Down Expand Up @@ -89,7 +93,12 @@ internal class BankChooseActivity: TransparentActivity() {
startActivity(browseIntent)
}
is OpenBankClickedEvent -> {
finishWithSuccess(BankChooseResult(screenState.packageName))
finishWithSuccess(
BankChooseResult(
packageName = screenState.packageName,
deeplink = banksInfo.getDeeplink(screenState.packageName)
)
)
}
}
}
Expand All @@ -99,9 +108,9 @@ internal class BankChooseActivity: TransparentActivity() {
private const val EXTRA_BANKS = "extra_banks"
private const val EXTRA_PAYLOAD_LINK = "extra_payload_link"

fun createIntent(context: Context, options: BaseAcquiringOptions, supportedBanks: List<String>, payloadLink: String): Intent {
fun createIntent(context: Context, options: BaseAcquiringOptions, supportedBanks: BankChooseInfo, payloadLink: String): Intent {
val intent = createIntent(context, options, BankChooseActivity::class.java)
intent.putExtra(EXTRA_BANKS, supportedBanks.toTypedArray())
intent.putExtra(EXTRA_BANKS, supportedBanks)
intent.putExtra(EXTRA_PAYLOAD_LINK, payloadLink)
return intent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import ru.tinkoff.acquiring.sdk.models.result.BankChooseResult
import ru.tinkoff.acquiring.sdk.models.result.CardResult
import ru.tinkoff.acquiring.sdk.models.result.PaymentResult
import ru.tinkoff.acquiring.sdk.threeds.ThreeDsSubmitV2Delegate
import ru.tinkoff.acquiring.sdk.ui.activities.PaymentActivity.Companion.EXTRA_SBP_BANK_DEEPLINK
import ru.tinkoff.acquiring.sdk.ui.activities.PaymentActivity.Companion.EXTRA_SBP_BANK_PACKAGE_NAME
import ru.tinkoff.acquiring.sdk.viewmodel.ViewModelProviderFactory
import ru.tinkoff.acquiring.sdk.viewmodel.YandexPaymentViewModel
Expand Down Expand Up @@ -162,7 +163,10 @@ internal open class BaseAcquiringActivity : AppCompatActivity() {
intent.putExtra(TinkoffAcquiring.EXTRA_REBILL_ID, result.rebillId)
}
is CardResult -> intent.putExtra(TinkoffAcquiring.EXTRA_CARD_ID, result.cardId)
is BankChooseResult -> intent.putExtra(EXTRA_SBP_BANK_PACKAGE_NAME, result.packageName)
is BankChooseResult -> with(intent) {
putExtra(EXTRA_SBP_BANK_PACKAGE_NAME, result.packageName)
putExtra(EXTRA_SBP_BANK_DEEPLINK, result.deeplink)
}
}

setResult(Activity.RESULT_OK, intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@ import ru.tinkoff.acquiring.sdk.R
import ru.tinkoff.acquiring.sdk.exceptions.AcquiringSdkException
import ru.tinkoff.acquiring.sdk.exceptions.NetworkException
import ru.tinkoff.acquiring.sdk.exceptions.NspkOpenException
import ru.tinkoff.acquiring.sdk.models.AsdkState
import ru.tinkoff.acquiring.sdk.models.*
import ru.tinkoff.acquiring.sdk.models.BrowseFpsBankScreenState
import ru.tinkoff.acquiring.sdk.models.BrowseFpsBankState
import ru.tinkoff.acquiring.sdk.models.DefaultState
import ru.tinkoff.acquiring.sdk.models.ErrorButtonClickedEvent
import ru.tinkoff.acquiring.sdk.models.ErrorScreenState
import ru.tinkoff.acquiring.sdk.models.FinishWithErrorScreenState
import ru.tinkoff.acquiring.sdk.models.FpsBankFormShowedScreenState
import ru.tinkoff.acquiring.sdk.models.FpsScreenState
import ru.tinkoff.acquiring.sdk.models.FpsState
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.OpenTinkoffPayBankScreenState
import ru.tinkoff.acquiring.sdk.models.OpenTinkoffPayBankState
import ru.tinkoff.acquiring.sdk.models.PaymentScreenState
import ru.tinkoff.acquiring.sdk.models.RejectedCardScreenState
import ru.tinkoff.acquiring.sdk.models.RejectedState
import ru.tinkoff.acquiring.sdk.models.Screen
import ru.tinkoff.acquiring.sdk.models.ScreenState
import ru.tinkoff.acquiring.sdk.models.SingleEvent
Expand All @@ -56,7 +51,7 @@ import ru.tinkoff.acquiring.sdk.threeds.ThreeDsHelper
import ru.tinkoff.acquiring.sdk.ui.customview.NotificationDialog
import ru.tinkoff.acquiring.sdk.ui.fragments.PaymentFragment
import ru.tinkoff.acquiring.sdk.viewmodel.PaymentViewModel
import java.lang.IllegalStateException
import kotlin.IllegalStateException

/**
* @author Mariya Chernyadieva
Expand Down Expand Up @@ -136,7 +131,7 @@ internal class PaymentActivity : TransparentActivity() {
finishWithCancel()
} else {
data?.getStringExtra(EXTRA_SBP_BANK_PACKAGE_NAME)?.let { packageName ->
openSbpPackage(packageName)
openSbpPackage(packageName, checkNotNull(data.getStringExtra(EXTRA_SBP_BANK_DEEPLINK)))
}
}
}
Expand Down Expand Up @@ -220,32 +215,31 @@ internal class PaymentActivity : TransparentActivity() {
val intent = BankChooseActivity.createIntent(this, options, supportedBanks, deepLink)
startActivityForResult(intent, SBP_BANK_CHOOSE_REQUEST_CODE)
} else {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(deepLink)
val chooserIntent = Intent.createChooser(intent, getString(R.string.acq_fps_chooser_title))
startActivityForResult(chooserIntent, SBP_BANK_REQUEST_CODE)
val nspkOpenException = NspkOpenException(
throwable = IllegalStateException("nspk date are null")
)
finishWithError(nspkOpenException)
}
}

@SuppressLint("QueryPermissionsNeeded")
private fun getBankApps(link: String, banks: List<NspkC2bResponse.NspkAppInfo>?): List<String> {
private fun getBankApps(link: String, banks: List<NspkC2bResponse.NspkAppInfo>?): BankChooseInfo {
val sbpIntent = Intent(Intent.ACTION_VIEW)
return banks?.flatMap {
sbpIntent.setDataAndNormalize(prepareNspkDeeplinkWithScheme(it.schema, link))
packageManager.queryIntentActivities(sbpIntent, 0).map { it.activityInfo.packageName }
val appAndLinks = buildMap {
banks?.forEach { appInfo ->
val deepLink = prepareNspkDeeplinkWithScheme(appInfo.schema, link)
sbpIntent.setDataAndNormalize(deepLink)
packageManager.queryIntentActivities(sbpIntent, 0).forEach {
put(it.activityInfo.packageName, deepLink.toString())
}
}
}
?.distinct() ?: emptyList()
return BankChooseInfo(appAndLinks)
}

private fun openSbpDeepLinkInBank(packageName: String,
browseFpsBankScreenState: BrowseFpsBankScreenState
) {
val info = browseFpsBankScreenState.banks?.first {
val pkgName = it.packageName ?: return@first false
packageName.contains(pkgName)
}!!
private fun openSbpDeepLinkInBank(deepLink: String) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = prepareNspkDeeplinkWithScheme(info.schema, browseFpsBankScreenState.deepLink)
intent.data = Uri.parse(deepLink)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
AcquiringSdk.log("try open intent with : Intent.ACTION_VIEW -d \"${intent.data}\"")
startActivityForResult(intent, SBP_BANK_REQUEST_CODE)
Expand Down Expand Up @@ -278,15 +272,15 @@ internal class PaymentActivity : TransparentActivity() {
return paymentViewModel.screenChangeEventLiveData.value?.value as BrowseFpsBankScreenState
}

private fun openSbpPackage(packageName: String) {
private fun openSbpPackage(packageName: String, deepLink: String) {
val fpsState = getBrowseFpsBankScreenState()
try {
openSbpDeepLinkInBank(packageName, fpsState)
openSbpDeepLinkInBank(deepLink)
} catch (e: Exception) {
val nspkOpenException = NspkOpenException(
throwable = e,
message = "$packageName cannot open via deeplink",
deeplink = fpsState.deepLink,
message = "$packageName cannot open via deeplink $deepLink",
deeplink = deepLink,
paymentId = fpsState.paymentId
)
finishWithError(nspkOpenException, fpsState.paymentId)
Expand All @@ -298,5 +292,6 @@ internal class PaymentActivity : TransparentActivity() {
private const val SBP_BANK_CHOOSE_REQUEST_CODE = 113

internal const val EXTRA_SBP_BANK_PACKAGE_NAME = "sbp_bank_package_name"
internal const val EXTRA_SBP_BANK_DEEPLINK = "sbp_bank_deeplink"
}
}

0 comments on commit 30df493

Please sign in to comment.