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 the PaymentMethodsActivity to display a list of payment methods to the user, so they can select their preferred payment method. To launch the activity, use PaymentMethodsActivity#newIntent(), then start the Intent using Activity#startActivityForResult().

    // start with your activity
    final Intent payIntent = PaymentMethodsActivity.newIntent(this);
    startActivityForResult(payIntent, REQUEST_CODE_SELECT_SOURCE);
    

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SELECT_SOURCE && resultCode == RESULT_OK) {
            final PaymentMethod paymentMethod = data
                    .getParcelableExtra(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT);
    
            if (paymentMethod != null && paymentMethod.card != null) {
              // Note: only PaymentMethods of type 'card' are supported at this time
            }
        }
    }
    

    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)

    As you might expect, this function retrieves the current customer object. It is 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) {
                }
            });
    

    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) {
                }
            });
    

    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) {
                }
            });
    

    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) {
                }
            });
    

    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) {
                }
            });
    

    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) {
                }
            });
    

    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) {
                }
            });
    

    Was this page helpful? Yes No

    Send

    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