Advertisement
hmunoz-stripe

heath-ideal-android-kotlin

Sep 8th, 2021
1,750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.24 KB | None | 0 0
  1. package com.example.tse_onboarding_16_7_1
  2.  
  3. import android.content.Intent
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.widget.Button
  8. import android.widget.Toast
  9. import com.stripe.android.*
  10. import com.stripe.android.model.ConfirmPaymentIntentParams
  11. import com.stripe.android.model.PaymentMethod
  12. import com.stripe.android.view.CardInputWidget
  13. import com.stripe.android.model.PaymentMethodCreateParams
  14. import androidx.lifecycle.lifecycleScope
  15. import com.google.gson.Gson
  16. import com.stripe.android.model.StripeIntent
  17. import com.stripe.android.paymentsheet.PaymentSheet
  18. import com.stripe.android.paymentsheet.PaymentSheet.CustomerConfiguration
  19. import com.stripe.android.paymentsheet.PaymentSheetResult
  20. import kotlinx.coroutines.launch
  21.  
  22.  
  23. class MainActivity : AppCompatActivity() {
  24.  
  25.     private lateinit var payButton : Button
  26.     private lateinit var cardInputWidget : CardInputWidget
  27.     private lateinit var stripe : Stripe
  28.     private lateinit var clientSecret : String
  29.     private lateinit var paymentSheet : PaymentSheet
  30.  
  31.     override fun onCreate(savedInstanceState: Bundle?) {
  32.         super.onCreate(savedInstanceState)
  33.         setContentView(R.layout.activity_main)
  34.  
  35.         PaymentConfiguration.init(applicationContext, "pk_test_YOUR_KEY_HERE")
  36.         stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey)
  37.  
  38.         payButton = findViewById(R.id.payButton)
  39.  
  40.         payButton.setOnClickListener({
  41.             startCheckout()
  42.         })
  43.     }
  44.  
  45.     fun startCheckout() {
  46.         Log.d("my-samples", "startCheckout() called")
  47.  
  48.         var idealPMParams = PaymentMethodCreateParams.create(
  49.             PaymentMethodCreateParams.Ideal("abn_amro"),
  50.             PaymentMethod.BillingDetails(name = "Jenny Rosen")
  51.         )
  52.  
  53.         var confirmParams = ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
  54.             paymentMethodCreateParams = idealPMParams,
  55.             clientSecret = "pi_123_secret_456"
  56.         )
  57.  
  58.         stripe.confirmPayment(this, confirmParams)
  59.     }
  60.  
  61.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  62.         super.onActivityResult(requestCode, resultCode, data)
  63.  
  64.         if (stripe.isPaymentResult(requestCode, data)) {
  65.  
  66.             val bundle = data!!.extras;
  67.  
  68.             Log.d("my-samples-bundle", bundle.toString())
  69.  
  70.             if (bundle != null)  {
  71.                 for (key in bundle.keySet()) {
  72.                     var bundleVal : Any
  73.                     if (bundle.get(key) != null) {
  74.                         bundleVal = bundle.get(key)!!
  75.                     }
  76.                     else {
  77.                        bundleVal = "NULL"
  78.                     }
  79.                     Log.d("my-samples-bundle", key + " : " + bundleVal)
  80.                 }
  81.             }
  82.  
  83.             val gson = Gson()
  84.             val jsonData = gson.toJson(data)
  85.             val jsonExtras = gson.toJson(data!!.extras)
  86.             val jsonRequestCode = gson.toJson(requestCode)
  87.             val jsonResultCode = gson.toJson(resultCode)
  88.  
  89.             Log.d("my-samples-onAcRes", jsonData)
  90.             Log.d("my-samples-onAcRes", jsonExtras)
  91.             Log.d("my-samples-onAcRes", jsonRequestCode)
  92.             Log.d("my-samples-onAcRes", jsonResultCode)
  93.  
  94.  
  95.             lifecycleScope.launch {
  96.                 runCatching {
  97.                     Log.d("my-samples", data.toString())
  98.                     Log.d("my-samples", data!!.extras.toString())
  99.                     Log.d("my-samples", requestCode.toString())
  100.                     stripe.getPaymentIntentResult(requestCode, data!!).intent
  101.                 }.fold(
  102.                     onSuccess = { paymentIntent ->
  103.                         val status = paymentIntent.status
  104.                         Log.d("my-samples", status.toString())
  105.                         Log.e("my-samples", "this is payment intent --------- $paymentIntent")
  106.                         when (status) {
  107.                             StripeIntent.Status.Processing -> {
  108.                                 // Payment authorized
  109.                                 Log.d("my-samples","payment processing----------")
  110.                             }
  111.                             StripeIntent.Status.Canceled -> {
  112.                                 Log.d("my-samples","payment cancelled----------")
  113.                             }
  114.                             StripeIntent.Status.Succeeded -> {
  115.                                 Log.d("my-samples","payment success----------")
  116.                             }
  117.                             StripeIntent.Status.RequiresAction -> {
  118.                                 Log.d("my-samples","payment required action----------")
  119.                             }
  120.                             else -> {
  121.                                 // Payment failed/cancelled
  122.                                 Log.d("my-samples","api success but payment failed----------")
  123.                             }
  124.                         }
  125.                     },
  126.                     onFailure = {
  127.                         // Payment failed
  128.                         Log.d("my-samples","-payment failed----------")
  129.                     }
  130.                 )
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement