Last active
April 25, 2021 14:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Re-tweet on Twitter.
Share on LinkedIn.