Saving Customer Card Information

    Allow customers to securely store and update payment information.

    This guide will take you through building your app’s customer payment management flow using CustomerSession, a class designed to make building your app’s checkout flow as easy as possible. It handles collecting, saving, and reusing your user’s payment details.

    Configure your backend

    Refer to Prepare your API for instructions on configuring your backend and connecting to it from the client.

    Let your user select their payment method

    Use PaymentMethodsActivity to display the customer’s payment methods and allow them to select one or add new ones. To launch the activity, use PaymentMethodsActivityStarter#startForResult().

    public class MyActivity extends Activity {
        private void launchPaymentMethodsActivity() {
            new PaymentMethodsActivityStarter(this).startForResult();
        }
    }
    
    class MyActivity : Activity() {
        private fun launchPaymentMethodsActivity() {
            PaymentMethodsActivityStarter(this).startForResult()
        }
    }
    

    In your host Activity or Fragment, implement Activity#onActivityResult and handle the result.

    public class MyActivity extends Activity {
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PaymentMethodsActivityStarter.REQUEST_CODE) {
                final PaymentMethodsActivityStarter.Result result =
                        PaymentMethodsActivityStarter.Result.fromIntent(data);
                final PaymentMethod paymentMethod = result != null ?
                        result.paymentMethod : null;
    
                // use paymentMethod
            }
        }
    }
    
    class MyActivity : Activity() {
        override fun onActivityResult(
            requestCode: Int, resultCode: Int, data: Intent?
        ) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == PaymentMethodsActivityStarter.REQUEST_CODE && data != null) {
                val result = PaymentMethodsActivityStarter.Result.fromIntent(data)
                val paymentMethod = result?.paymentMethod
    
                // use paymentMethod
            }
        }
    }
    

    PaymentMethodsActivity

    Choosing from stored payment methods in the PaymentMethodsActivity

    Note that this activity can launch the AddPaymentMethodActivity if a customer wants to add a new payment method. No changes to your code are necessary to accommodate this.

    AddPaymentMethodActivity

    Adding a new card in the AddPaymentMethodActivity

    CustomerSession without UI

    If you need more flexibility than our UI will allow, you can access all of the functionality of CustomerSession without invoking any UI at all. You’ll still need to initialize your CustomerSession as before. After it has been initialized, you can call the following methods.

    retrieveCurrentCustomer(CustomerSession.CustomerRetrievalListener)

    Retrieves the current Customer object. It runs asynchronous, and first checks whether or not the current customer object is expired. If it is, that object is returned immediately. If not, it makes a call to update the ephemeral key and get a new customer object. It is recommended to call this method the first time you need a customer object. The retrieved Customer object is returned via the provided CustomerRetrievalListener.

    CustomerSession.getInstance().retrieveCurrentCustomer(
        new CustomerRetrievalListener() {
            @Override
            public void onCustomerRetrieved(@NonNull Customer customer) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().retrieveCurrentCustomer(
        object : CustomerSession.CustomerRetrievalListener {
            override fun onCustomerRetrieved(customer: Customer) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        })
    

    updateCurrentCustomer(CustomerSession.CustomerRetrievalListener)

    Forces a call to the server to update the Customer object, regardless of whether or not the current customer is expired. This is good to call when you have just added a source to a customer, as the cached Customer object will not yet have the update.

    CustomerSession.getInstance().updateCurrentCustomer(
        new CustomerRetrievalListener() {
            @Override
            public void onCustomerRetrieved(@NonNull Customer customer) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().updateCurrentCustomer(
        object : CustomerSession.CustomerRetrievalListener {
            override fun onCustomerRetrieved(customer: Customer) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        })
    

    getPaymentMethods(PaymentMethod.Type, CustomerSession.PaymentMethodsRetrievalListener)

    Retrieves all of the customer’s PaymentMethod objects for the given PaymentMethod.Type.

    CustomerSession.getInstance().getPaymentMethods(
        PaymentMethod.Type.Card,
        new PaymentMethodsRetrievalListener() {
            @Override
            public void onPaymentMethodsRetrieved(
                    @NonNull List<PaymentMethod> paymentMethods) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().getPaymentMethods(
        PaymentMethod.Type.Card,
        object : CustomerSession.PaymentMethodsRetrievalListener {
            override fun onPaymentMethodsRetrieved(paymentMethods: List<PaymentMethod>) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        })
    

    attachPaymentMethod(String, CustomerSession.PaymentMethodRetrievalListener)

    Attach a PaymentMethod, specified by payment method ID, to the customer.

    CustomerSession.getInstance().attachPaymentMethod(
        "pm_12345",
        new PaymentMethodRetrievalListener() {
            @Override
            public void onPaymentMethodRetrieved(
                    @NonNull PaymentMethod paymentMethod) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().attachPaymentMethod(
        "pm_12345"
        object : CustomerSession.PaymentMethodRetrievalListener {
            override fun onPaymentMethodRetrieved(paymentMethod: PaymentMethod) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        }
    )
    

    detachPaymentMethod(String, CustomerSession.PaymentMethodRetrievalListener)

    Detach a PaymentMethod, specified by payment method ID, from the customer.

    CustomerSession.getInstance().detachPaymentMethod(
        "pm_12345",
        new PaymentMethodRetrievalListener() {
            @Override
            public void onPaymentMethodRetrieved(
                    @NonNull PaymentMethod paymentMethod) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().detachPaymentMethod(
        "pm_12345",
        object : CustomerSession.PaymentMethodRetrievalListener {
            override fun onPaymentMethodRetrieved(paymentMethod: PaymentMethod) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        }
    )
    

    addCustomerSource(String, @Source.SourceType String, CustomerSession.SourceRetrievalListener)

    Adds a payment method to a customer based on the ID and type of that Source. Note that at the moment, only Source objects of type Source.CARD are supported. The Source object added to the Customer is returned via the provided SourceRetrievalListener.

    CustomerSession.getInstance().addCustomerSource(
        "src_12345",
        Source.CARD,
        new SourceRetrievalListener() {
            @Override
            public void onSourceRetrieved(@NonNull Source source) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().addCustomerSource(
        "src_12345",
        Source.CARD,
        object : CustomerSession.SourceRetrievalListener {
            override fun onSourceRetrieved(source: Source) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        }
    )
    

    setCustomerDefaultSource(String, @Source.SourceType String, CustomerSession.CustomerRetrievalListener)

    Sets the default payment method of the current Customer. Note that at the moment, only sources of type Source.CARD are supported. The updated Customer object is returned via the provided CustomerRetrievalListener.

    CustomerSession.getInstance().setCustomerDefaultSource(
        "src_12345",
        Source.CARD,
        new CustomerRetrievalListener() {
            @Override
            public void onCustomerRetrieved(@NonNull Customer customer) {
            }
    
            @Override
            public void onError(int errorCode, @NonNull String errorMessage,
                                @Nullable StripeError stripeError) {
            }
        }
    );
    
    CustomerSession.getInstance().setCustomerDefaultSource(
        "src_12345",
        Source.CARD,
        object : CustomerSession.CustomerRetrievalListener {
            override fun onCustomerRetrieved(customer: Customer) {
            }
    
            override fun onError(
                errorCode: Int,
                errorMessage: String,
                stripeError: StripeError?
            ) {
            }
        }
    )
    

    Was this page helpful?

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    On this page