Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tse_onboarding_16_7_1
- import android.content.Intent
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.util.Log
- import android.widget.Button
- import android.widget.Toast
- import com.stripe.android.*
- import com.stripe.android.model.ConfirmPaymentIntentParams
- import com.stripe.android.model.PaymentMethod
- import com.stripe.android.view.CardInputWidget
- import com.stripe.android.model.PaymentMethodCreateParams
- import androidx.lifecycle.lifecycleScope
- import com.google.gson.Gson
- import com.stripe.android.model.StripeIntent
- import com.stripe.android.paymentsheet.PaymentSheet
- import com.stripe.android.paymentsheet.PaymentSheet.CustomerConfiguration
- import com.stripe.android.paymentsheet.PaymentSheetResult
- import kotlinx.coroutines.launch
- class MainActivity : AppCompatActivity() {
- private lateinit var payButton : Button
- private lateinit var cardInputWidget : CardInputWidget
- private lateinit var stripe : Stripe
- private lateinit var clientSecret : String
- private lateinit var paymentSheet : PaymentSheet
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- PaymentConfiguration.init(applicationContext, "pk_test_YOUR_KEY_HERE")
- stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey)
- payButton = findViewById(R.id.payButton)
- payButton.setOnClickListener({
- startCheckout()
- })
- }
- fun startCheckout() {
- Log.d("my-samples", "startCheckout() called")
- var idealPMParams = PaymentMethodCreateParams.create(
- PaymentMethodCreateParams.Ideal("abn_amro"),
- PaymentMethod.BillingDetails(name = "Jenny Rosen")
- )
- var confirmParams = ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
- paymentMethodCreateParams = idealPMParams,
- clientSecret = "pi_123_secret_456"
- )
- stripe.confirmPayment(this, confirmParams)
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (stripe.isPaymentResult(requestCode, data)) {
- val bundle = data!!.extras;
- Log.d("my-samples-bundle", bundle.toString())
- if (bundle != null) {
- for (key in bundle.keySet()) {
- var bundleVal : Any
- if (bundle.get(key) != null) {
- bundleVal = bundle.get(key)!!
- }
- else {
- bundleVal = "NULL"
- }
- Log.d("my-samples-bundle", key + " : " + bundleVal)
- }
- }
- val gson = Gson()
- val jsonData = gson.toJson(data)
- val jsonExtras = gson.toJson(data!!.extras)
- val jsonRequestCode = gson.toJson(requestCode)
- val jsonResultCode = gson.toJson(resultCode)
- Log.d("my-samples-onAcRes", jsonData)
- Log.d("my-samples-onAcRes", jsonExtras)
- Log.d("my-samples-onAcRes", jsonRequestCode)
- Log.d("my-samples-onAcRes", jsonResultCode)
- lifecycleScope.launch {
- runCatching {
- Log.d("my-samples", data.toString())
- Log.d("my-samples", data!!.extras.toString())
- Log.d("my-samples", requestCode.toString())
- stripe.getPaymentIntentResult(requestCode, data!!).intent
- }.fold(
- onSuccess = { paymentIntent ->
- val status = paymentIntent.status
- Log.d("my-samples", status.toString())
- Log.e("my-samples", "this is payment intent --------- $paymentIntent")
- when (status) {
- StripeIntent.Status.Processing -> {
- // Payment authorized
- Log.d("my-samples","payment processing----------")
- }
- StripeIntent.Status.Canceled -> {
- Log.d("my-samples","payment cancelled----------")
- }
- StripeIntent.Status.Succeeded -> {
- Log.d("my-samples","payment success----------")
- }
- StripeIntent.Status.RequiresAction -> {
- Log.d("my-samples","payment required action----------")
- }
- else -> {
- // Payment failed/cancelled
- Log.d("my-samples","api success but payment failed----------")
- }
- }
- },
- onFailure = {
- // Payment failed
- Log.d("my-samples","-payment failed----------")
- }
- )
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement