将Braintree dropin UI集成到KotlinJetpack Compose中

ff29svar  于 2023-10-23  发布在  Kotlin
关注(0)|答案(2)|浏览(114)

我正试图将android/java dropin UI代码从www.example.com转换https://developer.paypal.com/braintree/docs/guides/drop-in/setup-and-integration#starting-drop-in为jetpack compose应用程序。到目前为止我已经

@Composable
fun Account(user: FinUser) {
    val context = LocalContext.current
    val customerToken = user.userData["customerToken"] as String
    val dropInRequest = DropInRequest()
        .clientToken(customerToken)
    val dropInHintLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartIntentSenderForResult()
    ) {
        print("pause here")
    }
    val dropInIntent = dropInRequest.getIntent(context)
    val dropInPendingIntent = PendingIntent.getBroadcast(
        context, 200, dropInIntent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )
    Column(){
        Column(
            Modifier
                .padding(top = 0.dp)
                .clickable { launchDropInUi(
                    dropInHintLauncher=dropInHintLauncher,
                    dropInPendingIntent=dropInPendingIntent) }) {
            Divider(color = Color.LightGray, thickness = 1.dp)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp, 10.dp)
            ) {
                Column() {
                    Text("Payment", color = Color.Gray)
                    Text("*********9999", color = Color.Black)
                }
                Spacer(modifier = Modifier.fillMaxWidth())
            }
            Divider(color = Color.LightGray, thickness = 1.dp)
        }
    }
}

fun launchDropInUi(dropInHintLauncher: ManagedActivityResultLauncher<IntentSenderRequest, ActivityResult>, dropInPendingIntent: PendingIntent){
    dropInHintLauncher.launch(
        IntentSenderRequest.Builder(dropInPendingIntent).build()
    )
}

当我点击我的行时,没有dropin UI弹出窗口,但它确实注册了点击并运行了launchDropInUi函数。

epfja78i

epfja78i1#

我发现了问题。我需要使用

val dropInHintLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    )

@Composable
fun Account(user: FinUser) {
    val context = LocalContext.current
    val customerToken = user.userData["customerToken"] as String
    val dropInRequest = DropInRequest()
        .clientToken(customerToken)
    val dropInHintLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ){ result: ActivityResult ->
        if (result.resultCode == Activity.RESULT_OK) {
            //  you will get result here in result.data
            val data: DropInResult? = result.data?.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT)
        }else{
            print("throw error popup")
        }
    }
    val dropInIntent = dropInRequest.getIntent(context)
    Column(){
        Column(
            Modifier
                .padding(top = 0.dp)
                .clickable { launchDropInUi(
                    dropInHintLauncher =dropInHintLauncher,
                    dropInIntent =dropInIntent) }) {
            Divider(color = Color.LightGray, thickness = 1.dp)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp, 10.dp)
            ) {
                Column() {
                    Text("Payment", color = Color.Gray)
                    Text("*********9999", color = Color.Black)
                }
                Spacer(modifier = Modifier.fillMaxWidth())
            }
            Divider(color = Color.LightGray, thickness = 1.dp)
        }
    }
}
fun launchDropInUi(dropInHintLauncher: ManagedActivityResultLauncher<Intent, ActivityResult>, dropInIntent: Intent){
    dropInHintLauncher.launch(dropInIntent)
}
vsikbqxv

vsikbqxv2#

我发现了这个Bug并修复了它,
我已经将ClementActivity更改为FragmentActivity,并且仍然可以访问编写,就像这样。

class MainActivity : FragmentActivity(), DropInListener {
    val clientToken: String = "sandbox_8hych3nm_cmbsqrt3wy6rfrvx"
    var dropInClient: DropInClient? = null
    var dropInRequest: DropInRequest? = null
    var REQUEST_CODE = 6969
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Payment_integrationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    BraintreeButton()
                }
            }
        }
        dropInRequest = DropInRequest()
        dropInClient = DropInClient(this@MainActivity, clientToken)
        dropInClient!!.setListener(this)
    }

    override fun onDropInSuccess(dropInResult: DropInResult) {
        Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show()
    }

    override fun onDropInFailure(error: Exception) {
        Toast.makeText(this, "Payment Failed", Toast.LENGTH_SHORT).show()
    }

    @Composable
    fun BraintreeButton() {
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(onClick = {
                dropInClient!!.launchDropIn(dropInRequest)
            }) {
                Text(text = "Buy Now")
            }
        }
    }
}

相关问题