Stripe Android - Scan Card

Viewed 614

I'm working on integrating stripe add card.

This is my XML code. I'm using "CardInputWidget" to get card details.

Is there any way to implement scan card feature? Any default method to enable scan card. Can anyone help me with this? Does stripe android SDK provide scan card feature

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_300"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/actionbar_layout" />

    <LinearLayout
        android:layout_below="@+id/citygoToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:id="@+id/emailInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_margin="20dp"
            android:hint="Enter your email" />

        <com.stripe.android.view.CardInputWidget
            android:id="@+id/cardInputWidget"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:padding="20dp"
            android:background="@drawable/edittext_white_border_shape" />

        <Button
            android:text="Save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/payButton"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            style="@style/AppTheme.Button.Pink" />

    </LinearLayout>

</LinearLayout>
2 Answers

You can add your own button, say imageView with "camera_icon"

implement the library "io.card:android-sdk:5.4.2"

after that you can prepare the request

fun onClickScanCard() {
        val scanIntent = Intent(activity, CardIOActivity::class.java)

        scanIntent.putExtra(CardIOActivity.EXTRA_HIDE_CARDIO_LOGO, true)
        scanIntent.putExtra(CardIOActivity.EXTRA_SUPPRESS_MANUAL_ENTRY, true)
        scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, false)
        scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false)
        scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_POSTAL_CODE, false)
        scanIntent.putExtra(CardIOActivity.EXTRA_USE_PAYPAL_ACTIONBAR_ICON, false)

        activity.startActivityForResult(scanIntent, yourRequestCode)
    }

and handle the result. I use binding for set values into the fields you can use another way.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == yourRequestCode && data?.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT) == true) {
            val scanResult = data.getParcelableExtra<CreditCard>(CardIOActivity.EXTRA_SCAN_RESULT)
            scanResult?.also { creditCard ->
                val expiryDate = creditCard.expiryMonth.toString() + creditCard.expiryYear

                binding.cardNumberEditText.setText(creditCard.cardNumber)
                binding.expiryDateEditText.setText(expiryDate)
                binding.zipCodeEditText.setText(creditCard.postalCode)
                binding.cvcEditText.setText(creditCard.cvv)
            }

            activity.storefrontAnalytics.onScannedCard()

            updateEnabledState()
        }
    }

Google recommends contracts to work with startForResult.

Better use CardScanSheet from the official library, which stripe-android provides:

dependencies {
    implementation 'com.stripe:stripe-android:20.5.0'
    implementation 'com.stripe:stripecardscan:20.5.0'
}
Related