Skip to content

Instantly share code, notes, and snippets.

@thenishchalraj
Last active April 25, 2021 14:28
Show Gist options
  • Save thenishchalraj/befdb2a3428bc74237ea71426172365f to your computer and use it in GitHub Desktop.
Save thenishchalraj/befdb2a3428bc74237ea71426172365f to your computer and use it in GitHub Desktop.
Just the main functions calling the stripe's card dialog and fetching data when positive response from the user i.e. OK clicked in Kotlin.
private fun onAddCardButtonClicked() {
mBinding.buttonAddCard.setOnClickListener {
val view = layoutInflater.inflate(R.layout.dialog_add_card, null)
val cardInputWidget: CardInputWidget = view.findViewById(R.id.widget_add_card)
AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.add_card))
.setView(view)
.setPositiveButton(getString(R.string.yes)) { dialog, _ ->
try {
val paymentMethodCreateParams =
cardInputWidget.paymentMethodCreateParams
if (paymentMethodCreateParams != null)
addStripeCard(paymentMethodCreateParams)
/*
add anything else you want either
in the if condition, or outside that
*/
} catch (e: Exception) {
e.printStackTrace()
// add a Toast to show the exception
}
dialog.cancel()
}
.setNegativeButton(getString(R.string.no)) { _, _ -> }
.create().show()
}
}
private fun addStripeCard(paymentMethodCreateParams: PaymentMethodCreateParams) {
stripe!!.createPaymentMethod(
paymentMethodCreateParams, null, null,
object : ApiResultCallback<PaymentMethod> {
override fun onSuccess(result: PaymentMethod) {
Log.i("[SUCCESS] Payment Method: ", result.toString())
Log.i("[SUCCESS] Stripe Token ID: ", result.id.toString())
// add a call to own server to save the details
}
override fun onError(e: java.lang.Exception) {
// add a Toast to show the exception
}
})
}
@thenishchalraj
Copy link
Author

Click here to go to the blog where this was used, better explanation of how to integrate stripe in android with Kotlin, also React Native and Java code included.

Took some time in the evening to write a blog about my struggle with @stripe adding card when the card method was deprecated.

For the community. Cheers!#stripe #android #reactnative #kotlin #java #payment #card #medium #blog

Here is the link to blog:https://t.co/jvRz8ThxZP

— Nishchal Raj (@thenishchalraj) April 25, 2021

Re-tweet on Twitter.

Share on LinkedIn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment