Save a card after a payment

    Learn how to save card details after a payment.

    Overview

    Use the Payment Intents API to save card details from a purchase. There are several use cases:

    • Charge a customer for an e-commerce order and store the details for future purchases
    • Initiate the first payment of a series of recurring payments
    • Charge a deposit and store the details to charge the full amount later

    1 Set up Stripe Server-side

    First, you need a Stripe account. Register now.

    Use our official libraries for access to the Stripe API from your application:

    Available as a gem:

    sudo gem install stripe

    If you use bundler, you can use this line:

    gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'

    Available through pip:

    pip install --upgrade stripe

    Alternatively, you can also use easy_install:

    easy_install --upgrade stripe

    The PHP library can be installed via Composer:

    composer require stripe/stripe-php

    Alternatively, you can download the source directly.

    For Gradle, add the following dependency to your build.gradle:

    implementation "com.stripe:stripe-java:{VERSION}"
    (Replace {VERSION} with the actual version number you want to use. You can find the most recent version on Maven Repository or GitHub.)

    For Maven, add the following dependency to your POM:

    <dependency>
      <groupId>com.stripe</groupId>
      <artifactId>stripe-java</artifactId>
      <version>{VERSION}</version>
    </dependency>
    (Replace {VERSION} with the actual version number you want to use. You can find the most recent version on Maven Repository or GitHub.)

    In other environments, manually install the following JARs:

    Install via npm:

    npm install stripe

    Install via go:

    go get github.com/stripe/stripe-go

    Then import the package:

    import (
      "github.com/stripe/stripe-go"
    )

    Install via dotnet:

    dotnet add package Stripe.net
    dotnet restore

    Or using NuGet:

    PM> Install-Package Stripe.net

    2 Create a PaymentIntent Server-side

    Stripe uses a PaymentIntent object to represent your intent to collect payment from a customer, tracking charge attempts and payment state changes throughout the process. Always decide how much to charge on the server side, a trusted environment, as opposed to the client. This prevents malicious customers from being able to choose their own prices.

    To create a payment and save a card for future payments, specify the amount and currency:

    curl https://api.stripe.com/v1/payment_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d amount=1099 \
      -d currency=usd
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::PaymentIntent.create(
      amount: 1099,
      currency: 'usd',
    )
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.PaymentIntent.create(
      amount=1099,
      currency='usd',
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    $intent = \Stripe\PaymentIntent::create([
        'amount' => 1099,
        'currency' => 'usd',
    ])
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Map<String, Object> params = new HashMap<>();
    params.put("amount", 1099);
    params.put("currency", "eur");
    PaymentIntent paymentIntent = PaymentIntent.create(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      const intent = await stripe.paymentIntents.create({
        amount: 1099,
        currency: 'usd',
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentIntentParams{
      Amount: stripe.Int64(1099),
      Currency: stripe.String(string(stripe.CurrencyUSD)),
    }
    intent, err := paymentintent.New(params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var service = new PaymentIntentService();
    var options = new PaymentIntentCreateOptions
    {
        Amount = 1099,
        Currency = "usd",
    };
    PaymentIntent paymentIntent = service.Create(options);
    

    Instead of passing the entire PaymentIntent object to the browser, just pass the client secret. The PaymentIntent’s client secret is a unique key that lets you confirm the payment and update card details on the client, without allowing manipulation of sensitive information, like payment amount.

    3 Collect card details Client-side

    After creating a PaymentIntent on the server and passing its client secret to the browser, you’re ready to collect card information with Stripe Elements on your client. Elements is a set of prebuilt UI components for collecting and validating card number, ZIP code, and expiration date.

    Set up Stripe Elements

    Include the Stripe.js script by adding it to the head of your checkout page. Always load Stripe.js directly from js.stripe.com to remain PCI compliant. Do not include the script in a bundle or host a copy of it yourself.

    <head>
      <title>Checkout</title>
      <script src="https://js.stripe.com/v3/"></script>
    </head>
    

    Next, create an instance of Elements with the following JavaScript:

    // Set your publishable key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    var stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
    var elements = stripe.elements();
    

    Add Elements to your page

    Elements needs a place to live in your payment form. Create empty DOM nodes (containers) with unique IDs in your payment form and then pass those IDs to Elements.

    <div id="card-element">
      <!-- Elements will create input elements here -->
    </div>
    
    <!-- We'll put the error messages in this element -->
    <div id="card-errors" role="alert"></div>
    
    <button id="submit">Pay</button>
    

    When the form above has loaded, create an instance of an Element and mount it to the Element container:

    // Set up Stripe.js and Elements to use in checkout form
    var style = {
      base: {
        color: "#32325d",
      }
    };
    
    var card = elements.create("card", { style: style });
    card.mount("#card-element");
    

    The card Element simplifies the form and minimizes the number of fields required by inserting a single, flexible input field that securely collects all necessary card details.

    Elements validates user input as it is typed. To help your customers catch mistakes, listen to change events on the card Element and display any errors (e.g., postal code validation depends on your customer’s billing country). Use our international test cards to experiment with other postal code formats.

    card.addEventListener('change', function(event) {
      var displayError = document.getElementById('card-errors');
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = '';
      }
    });
    
    card.addEventListener('change', ({error}) => {
      const displayError = document.getElementById('card-errors');
      if (error) {
        displayError.textContent = error.message;
      } else {
        displayError.textContent = '';
      }
    });

    4 Submit the payment to Stripe Client-side

    To complete the payment when the user clicks, retrieve the client secret from the PaymentIntent you created in step two and call stripe.confirmCardPayment:

    stripe.confirmCardPayment(clientSecret, {
      payment_method: {card: card},
      setup_future_usage: 'off_session'
    }).then(function(result) {
      if (result.error) {
        // Show error to your customer
        console.log(result.error.message);
      } else {
        if (result.paymentIntent.status === 'succeeded') {
          // Show a success message to your customer
          // There's a risk of the customer closing the window before callback execution
          // Set up a webhook or plugin to listen for the payment_intent.succeeded event
          // to save the card to a Customer
    
          // The PaymentMethod ID can be found on result.paymentIntent.payment_method
        }
      }
    });
    

    The client secret can be used to complete the payment process with the amount specified on the PaymentIntent. It should not be logged, embedded in URLs, or exposed to anyone other than the customer. The setup_future_usage parameter helps optimize future payments made with the same card. To learn more, see optimizing future payments.

    When the payment completes successfully, the value of the returned PaymentIntent’s status property is succeeded. If the payment was not successful, you can inspect the returned error to determine the cause.

    5 Save the card Server-side

    Once the PaymentIntent has succeeded, associate the card details with a Customer to save it. It’s a best practice to use a webhook to listen for the payment_intent.succeeded event. This tells you that your customer has completed the payment and no further action is required.

    When creating a new Customer, pass a PaymentMethod ID to immediately add a payment method.

    curl https://api.stripe.com/v1/customers \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d payment_method="{{PAYMENT_METHOD_ID}}"
    
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    customer = Stripe::Customer.create({
      payment_method: intent.payment_method,
    })
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    stripe.Customer.create(
      payment_method=intent.payment_method
    )
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    \Stripe\Customer::create([
      'payment_method' => $intent->payment_method,
    ]);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    Map<String, Object> customerParams = new HashMap<String, Object>();
    customerParams.put("payment_method", intent.getPaymentMethod());
    Customer.create(customerParams);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    const customer = await stripe.customers.create({
      payment_method: intent.payment_method,
    });
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    customerParams := &stripe.CustomerParams{
        PaymentMethod: intent.PaymentMethod.ID,
    }
    c, err := customer.New(customerParams)
    var options = new CustomerCreateOptions {
      PaymentMethod = intent.PaymentMethodId,
    };
    
    var customer = new CustomerService();
    Customer customer = service.Create(options);
    

    If you have an existing Customer, you can attach the PaymentMethod to that object instead.

    curl https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/attach  \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}"
    
    payment_method = Stripe::PaymentMethod.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    )
    
    payment_method = stripe.PaymentMethod.attach(
      intent.payment_method,
      customer='{{CUSTOMER_ID}}'
    )
    $payment_method = \Stripe\PaymentMethod::retrieve($intent->payment_method);
    $payment_method->attach(['customer' => '{{CUSTOMER_ID}}']);
    PaymentMethod paymentMethod = PaymentMethod.retrieve(intent.getPaymentMethod());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("customer", "{{CUSTOMER_ID}}");
    paymentMethod.attach(params);
    const paymentMethod = await stripe.paymentMethods.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    );
    params := &stripe.PaymentMethodAttachParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
    }
    p, err := paymentmethod.Attach(intent.PaymentMethod.ID, params)
    var options = new PaymentMethodAttachOptions
    {
        Customer = "{{CUSTOMER_ID}}",
    };
    var service = new PaymentMethodService();
    var paymentMethod = service.Attach(intent.PaymentMethodId, options);

    At this point, associate the ID of the Customer object with your own internal representation of a customer, if you have one. Now you can use the stored PaymentMethod object to collect payments from your customer in the future without prompting them for their payment details again.

    6 Charge the saved card later Server-side

    When you are ready to charge your customer off-session, use the Customer and PaymentMethod IDs to create a PaymentIntent.

    To find a card to charge, list the PaymentMethods associated with your Customer.

    curl https://api.stripe.com/v1/payment_methods \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}" \
      -d type=card
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::PaymentMethod.list({
      customer: '{{CUSTOMER_ID}}',
      type: 'card',
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.PaymentMethod.list(
      customer="{{CUSTOMER_ID}}",
      type="card",
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\PaymentMethod::all([
      'customer' => '{{CUSTOMER_ID}}',
      'type' => 'card',
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentMethodListParams;
    import com.stripe.model.PaymentMethodCollection;
    
    PaymentMethodListParams listParams = new PaymentMethodListParams.Builder().setCustomer("{{CUSTOMER_ID}}")
            .setType(PaymentMethodListParams.Type.CARD).build();
    
    PaymentMethodCollection paymentMethods = PaymentMethod.list(listParams);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      const paymentIntent = await stripe.paymentMethods.list({
        customer: '{{CUSTOMER_ID}}',
        type: 'card',
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentMethodListParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      Type: stripe.String("card"),
    }
    i := paymentmethod.List(params)
    for i.Next() {
      paymentMethod := i.PaymentMethod()
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var options = new PaymentMethodListOptions
    {
      Customer = "{{CUSTOMER_ID}}",
      Type = "card",
    };
    var service = new PaymentMethodService();
    service.List(options);
    

    When you have the Customer and PaymentMethod IDs, create a PaymentIntent with the amount and currency of the payment. Set a few other parameters to make the off-session payment:

    • Set off_session to true to indicate that the customer is not in your checkout flow during this payment attempt. This causes the PaymentIntent to throw an error if authentication is required.
    • Set the value of the PaymentIntent’s confirm property to true, which causes confirmation to occur immediately when the PaymentIntent is created.
    • Set payment_method to the ID of the PaymentMethod and customer to the ID of the Customer.
    curl https://api.stripe.com/v1/payment_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d amount=1099 \
      -d currency=usd \
      -d customer="{{CUSTOMER_ID}}" \
      -d payment_method="{{PAYMENT_METHOD_ID}}" \
      -d off_session=true \
      -d confirm=true
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    begin:
      intent = Stripe::PaymentIntent.create({
          amount: 1099,
          currency: 'usd',
          customer: '{CUSTOMER_ID}',
          payment_method: '{PAYMENT_METHOD_ID}',
          off_session: true,
          confirm: true,
      })
    rescue Stripe::CardError => e
      # Error code will be authentication_required if authentication is needed
      puts "Error is: #{e.error.code}"
      payment_intent_id = e.error.payment_intent.id
      payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
      puts payment_intent.id
    end
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    try:
      stripe.PaymentIntent.create(
        amount=1099,
        currency='usd',
        customer='{{CUSTOMER_ID}}',
        payment_method='{{PAYMENT_METHOD_ID}}',
        off_session=True,
        confirm=True,
      )
    except stripe.error.CardError as e:
      err = e.error
      # Error code will be authentication_required if authentication is needed
      print("Code is: %s" % err.code)
      payment_intent_id = err.payment_intent['id']
      payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    try {
      \Stripe\PaymentIntent::create([
          'amount' => 1099,
          'currency' => 'usd',
          'customer' => '{{CUSTOMER_ID}}',
          'payment_method' => '{{PAYMENT_METHOD_ID}}',
          'off_session' => true,
          'confirm' => true,
      ]);
    } catch (\Stripe\Exception\CardException $e) {
      // Error code will be authentication_required if authentication is needed
      echo 'Error code is:' . $e->getError()->code;
      $payment_intent_id = $e->getError()->payment_intent->id;
      $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id);
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentIntentCreateParams;
    import com.stripe.model.PaymentIntent;
    
    PaymentIntentCreateParams createParams = new PaymentIntentCreateParams.Builder()
      .setCurrency("usd")
      .setAmount(1099)
      .setPaymentMethod("{{PAYMENT_METHOD_ID}}")
      .setCustomer("{{CUSTOMER_ID}}")
      .setConfirm(true)
      .setOffSession(true)
      .build();
    try {
      PaymentIntent.create(createParams);
    } catch (CardException err) {
      // Error code will be authentication_required if authentication is needed
      System.out.println("Error code is : " + e.getCode());
      String paymentIntentId = e.getStripeError().getPaymentIntent().getId();
      PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
      System.out.println(paymentIntent.getId());
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      try {
        const paymentIntent = await stripe.paymentIntents.create({
          amount: 1099,
          currency: 'usd',
          customer: '{{CUSTOMER_ID}}',
          payment_method: '{{PAYMENT_METHOD_ID}}',
          off_session: true,
          confirm: true,
        });
      } catch (err) {
        // Error code will be authentication_required if authentication is needed
        console.log('Error code is: ', err.code);
        payment_intent_id = err.raw.payment_intent.id;
        stripe.paymentIntents.retrieve(
          payment_intent_id,
          function(err, paymentIntentRetrieved) {
            console.log('PI retrieved: ', paymentIntentRetrieved.id);
          }
        );
      }
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentIntentParams{
      Amount: stripe.Int64(1099),
      Currency: stripe.String(string(stripe.CurrencyUSD)),
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      PaymentMethod: stripe.String("{{PAYMENT_METHOD_ID}}"),
      Confirm: true,
      OffSession: true,
    }
    
    _, err := paymentintent.New(params)
    
    if err != nil {
      if stripeErr, ok := err.(*stripe.Error); ok {
        // Error code will be authentication_required if authentication is needed
        fmt.Printf("Error code: %v
    ", stripeErr.Code)
    
        payment_intent_id := stripeErr.PaymentIntent.ID
        payment_intent, _ := paymentintent.Get(payment_intent_id, nil)
    
        fmt.Printf("PI: %v
    ", payment_intent.ID)
      }
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    try {
      var service = new PaymentIntentService();
      var options = new PaymentIntentCreateOptions
      {
          Amount = 1099,
          Currency = "usd",
          CustomerId = "{{CUSTOMER_ID}}",
          PaymentMethodId = "{{PAYMENT_METHOD_ID}}",
          Confirm = true,
          OffSession = true,
      };
      service.Create(options);
    }  catch (StripeException e)
    {
        switch (e.StripeError.ErrorType)
        {
            case "card_error":
                // Error code will be authentication_required if authentication is needed
                Console.WriteLine("Error code: " + e.StripeError.Code);
                var paymentIntentId = e.StripeError.PaymentIntent.Id;
                var paymentIntent = service.Get(paymentIntentId);
    
                Console.WriteLine(paymentIntent.Id);
                break;
            default:
                break;
        }
    }
    

    When a payment attempt fails, the request also fails with a 402 HTTP status code and the status of the PaymentIntent is requires_payment_method. You need to notify your customer to return to your application (e.g., by sending an email or in-app notification) to complete the payment. Check the code of the Error raised by the Stripe API library or check the last_payment_error.decline_code on the PaymentIntent to inspect why the card issuer declined the payment.

    If the payment failed due to an authentication_required decline code, use the declined PaymentIntent’s client secret and payment method with confirmCardPayment to allow the customer to authenticate the payment.

    // Pass the failed PaymentIntent to your client from your server
    stripe.confirmCardPayment(intent.client_secret, {
      payment_method: intent.last_payment_error.payment_method.id
    }).then(function(result) {
      if (result.error) {
        // Show error to your customer
        console.log(result.error.message);
      } else {
        if (result.paymentIntent.status === 'succeeded') {
          // The payment is complete!
        }
      }
    });
    

    If the payment failed for other reasons, such as insufficient funds on the card, send your customer to a payment page to enter a new card. You can reuse the existing PaymentIntent to attempt the payment again with the new card details.

    7 Test the integration

    By this point you should have an integration that:

    1. Collects card details and makes a payment
    2. Saves the card details to a Customer
    3. Charges the card off-session and has a recovery flow to handle declines and authentication requests

    There are several test cards you can use to make sure this integration is ready for production. Use them with any CVC, postal code, and future expiration date.

    Number Description
    4242424242424242 Succeeds and immediately processes the payment.
    4000002500003155 Requires authentication for the initial purchase, but succeeds for subsequent payments (including off-session ones) as long as the card is setup with setup_future_usage.
    4000002760003184 Requires authentication for the initial purchase, and fails for subsequent payments (including off-session ones) with an authentication_required decline code.
    4000008260003178 Requires authentication for the initial purchase, but fails for subsequent payments (including off-session ones) with an insufficient_funds decline code.
    4000000000009995 Always fails (including the initial purchase) with a decline code of insufficient_funds.

    For the full list of test cards see our guide on testing.

    Overview

    Use the Payment Intents API to save card details from a purchase. There are several use cases:

    • Charge a customer for an e-commerce order and store the details for future purchases
    • Initiate the first payment of a series of recurring payments
    • Charge a deposit and store the details to charge the full amount later

    1 Accept a payment Client-side Server-side

    To save a card after a payment, first accept a card payment from your customer, with one difference:

    In step 4, when your client confirms the PaymentIntent, add the setupFutureUsage parameter. This parameter optimizes authorization rates when the customer’s card is used again. To determine which value to use, consider how you want to use this card in the future.

    How you intend to use the card setup_future_usage enum value to use
    On-session payments only STPPaymentIntentSetupFutureUsageOnSession
    Off-session payments only STPPaymentIntentSetupFutureUsageOffSession
    Both on and off-session payments STPPaymentIntentSetupFutureUsageOffSession

    A card set up for on-session payments can still be used to make off-session payments, but there’s a higher likelihood that the bank will reject the off-session payment and require authentication from the cardholder.

    class CheckoutViewController: UIViewController {
    
        // ...
    
        @objc
        func pay() {
            guard let paymentIntentClientSecret = paymentIntentClientSecret else {
                return;
            }
            // Collect card details
            let cardParams = cardTextField.cardParams
            let paymentMethodParams = STPPaymentMethodParams(card: cardParams, billingDetails: nil, metadata: nil)
            let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
            paymentIntentParams.paymentMethodParams = paymentMethodParams
            paymentIntentParams.setupFutureUsage = NSNumber(value: STPPaymentIntentSetupFutureUsage.offSession.rawValue)
    
    See all 42 lines // Submit the payment let paymentHandler = STPPaymentHandler.shared() paymentHandler.confirmPayment(withParams: paymentIntentParams, authenticationContext: self) { (status, paymentIntent, error) in switch (status) { case .failed: self.displayAlert(title: "Payment failed", message: error?.localizedDescription ?? "") break case .canceled: self.displayAlert(title: "Payment canceled", message: error?.localizedDescription ?? "") break case .succeeded: self.displayAlert(title: "Payment succeeded", message: paymentIntent?.description ?? "", restartDemo: true) break @unknown default: fatalError() break } } } } extension CheckoutViewController: STPAuthenticationContext { func authenticationPresentingViewController() -> UIViewController { return self } }
    @interface CheckoutViewController ()  <STPAuthenticationContext>
    
    // ...
    
    @end
    
    @implementation CheckoutViewController
    
    // ...
    
    - (void)pay {
        if (!self.paymentIntentClientSecret) {
            NSLog(@"PaymentIntent hasn't been created");
            return;
        }
    
        // Collect card details
        STPPaymentMethodCardParams *cardParams = self.cardTextField.cardParams;
        STPPaymentMethodParams *paymentMethodParams = [STPPaymentMethodParams paramsWithCard:cardParams billingDetails:nil metadata:nil];
        STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:self.paymentIntentClientSecret];
        paymentIntentParams.paymentMethodParams = paymentMethodParams;
        paymentIntentParams.setupFutureUsage = @(STPPaymentIntentSetupFutureUsageOffSession);
    
    See all 53 lines // Submit the payment STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler]; [paymentHandler confirmPayment:paymentIntentParams withAuthenticationContext:self completion:^(STPPaymentHandlerActionStatus status, STPPaymentIntent *paymentIntent, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ switch (status) { case STPPaymentHandlerActionStatusFailed: { [self displayAlertWithTitle:@"Payment failed" message:error.localizedDescription ?: @"" restartDemo:NO]; break; } case STPPaymentHandlerActionStatusCanceled: { [self displayAlertWithTitle:@"Payment canceled" message:error.localizedDescription ?: @"" restartDemo:NO]; break; } case STPPaymentHandlerActionStatusSucceeded: { [self displayAlertWithTitle:@"Payment succeeded" message:paymentIntent.description ?: @"" restartDemo:YES]; break; } default: break; } }); }]; } # pragma mark STPAuthenticationContext - (UIViewController *)authenticationPresentingViewController { return self; } @end

    2 Save the card Server-side

    When the PaymentIntent succeeds, it contains a PaymentMethod identifier representing the customer’s card details. Associate this PaymentMethod with a Customer.

    When creating a new Customer, pass a PaymentMethod ID to immediately add a payment method.

    curl https://api.stripe.com/v1/customers \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d payment_method="{{PAYMENT_METHOD_ID}}"
    
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    customer = Stripe::Customer.create({
      payment_method: intent.payment_method,
    })
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    stripe.Customer.create(
      payment_method=intent.payment_method
    )
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    \Stripe\Customer::create([
      'payment_method' => $intent->payment_method,
    ]);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    Map<String, Object> customerParams = new HashMap<String, Object>();
    customerParams.put("payment_method", intent.getPaymentMethod());
    Customer.create(customerParams);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    const customer = await stripe.customers.create({
      payment_method: intent.payment_method,
    });
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    customerParams := &stripe.CustomerParams{
        PaymentMethod: intent.PaymentMethod.ID,
    }
    c, err := customer.New(customerParams)
    var options = new CustomerCreateOptions {
      PaymentMethod = intent.PaymentMethodId,
    };
    
    var customer = new CustomerService();
    Customer customer = service.Create(options);
    

    If you have an existing Customer, you can attach the PaymentMethod to that object instead.

    curl https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/attach  \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}"
    
    payment_method = Stripe::PaymentMethod.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    )
    
    payment_method = stripe.PaymentMethod.attach(
      intent.payment_method,
      customer='{{CUSTOMER_ID}}'
    )
    $payment_method = \Stripe\PaymentMethod::retrieve($intent->payment_method);
    $payment_method->attach(['customer' => '{{CUSTOMER_ID}}']);
    PaymentMethod paymentMethod = PaymentMethod.retrieve(intent.getPaymentMethod());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("customer", "{{CUSTOMER_ID}}");
    paymentMethod.attach(params);
    const paymentMethod = await stripe.paymentMethods.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    );
    params := &stripe.PaymentMethodAttachParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
    }
    p, err := paymentmethod.Attach(intent.PaymentMethod.ID, params)
    var options = new PaymentMethodAttachOptions
    {
        Customer = "{{CUSTOMER_ID}}",
    };
    var service = new PaymentMethodService();
    var paymentMethod = service.Attach(intent.PaymentMethodId, options);

    At this point, associate the ID of the Customer object with your own internal representation of a customer, if you have one. Now you can use the stored PaymentMethod object to collect payments from your customer in the future without prompting them for their payment details again.

    Great! You’ve saved the card for reuse. You can use the list API to retrieve a Customer’s saved cards.

    To charge the card again on-session, include their Customer ID when you create the PaymentIntent and reuse their saved PaymentMethod ID when you confirm it.

    To charge the card again off-session, see the next steps.

    3 Charge the card off-session Server-side

    When you are ready to charge your customer off-session, use the Customer and PaymentMethod IDs to create a PaymentIntent.

    To find a card to charge, list the PaymentMethods associated with your Customer.

    curl https://api.stripe.com/v1/payment_methods \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}" \
      -d type=card
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::PaymentMethod.list({
      customer: '{{CUSTOMER_ID}}',
      type: 'card',
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.PaymentMethod.list(
      customer="{{CUSTOMER_ID}}",
      type="card",
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\PaymentMethod::all([
      'customer' => '{{CUSTOMER_ID}}',
      'type' => 'card',
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentMethodListParams;
    import com.stripe.model.PaymentMethodCollection;
    
    PaymentMethodListParams listParams = new PaymentMethodListParams.Builder().setCustomer("{{CUSTOMER_ID}}")
            .setType(PaymentMethodListParams.Type.CARD).build();
    
    PaymentMethodCollection paymentMethods = PaymentMethod.list(listParams);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      const paymentIntent = await stripe.paymentMethods.list({
        customer: '{{CUSTOMER_ID}}',
        type: 'card',
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentMethodListParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      Type: stripe.String("card"),
    }
    i := paymentmethod.List(params)
    for i.Next() {
      paymentMethod := i.PaymentMethod()
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var options = new PaymentMethodListOptions
    {
      Customer = "{{CUSTOMER_ID}}",
      Type = "card",
    };
    var service = new PaymentMethodService();
    service.List(options);
    

    When you have the Customer and PaymentMethod IDs, create a PaymentIntent with the amount and currency of the payment. Set a few other parameters to make the off-session payment:

    • Set off_session to true to indicate that the customer is not in your checkout flow during this payment attempt. This causes the PaymentIntent to throw an error if authentication is required.
    • Set the value of the PaymentIntent’s confirm property to true, which causes confirmation to occur immediately when the PaymentIntent is created.
    • Set payment_method to the ID of the PaymentMethod and customer to the ID of the Customer.
    curl https://api.stripe.com/v1/payment_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d amount=1099 \
      -d currency=usd \
      -d customer="{{CUSTOMER_ID}}" \
      -d payment_method="{{PAYMENT_METHOD_ID}}" \
      -d off_session=true \
      -d confirm=true
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    begin:
      intent = Stripe::PaymentIntent.create({
          amount: 1099,
          currency: 'usd',
          customer: '{CUSTOMER_ID}',
          payment_method: '{PAYMENT_METHOD_ID}',
          off_session: true,
          confirm: true,
      })
    rescue Stripe::CardError => e
      # Error code will be authentication_required if authentication is needed
      puts "Error is: #{e.error.code}"
      payment_intent_id = e.error.payment_intent.id
      payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
      puts payment_intent.id
    end
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    try:
      stripe.PaymentIntent.create(
        amount=1099,
        currency='usd',
        customer='{{CUSTOMER_ID}}',
        payment_method='{{PAYMENT_METHOD_ID}}',
        off_session=True,
        confirm=True,
      )
    except stripe.error.CardError as e:
      err = e.error
      # Error code will be authentication_required if authentication is needed
      print("Code is: %s" % err.code)
      payment_intent_id = err.payment_intent['id']
      payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    try {
      \Stripe\PaymentIntent::create([
          'amount' => 1099,
          'currency' => 'usd',
          'customer' => '{{CUSTOMER_ID}}',
          'payment_method' => '{{PAYMENT_METHOD_ID}}',
          'off_session' => true,
          'confirm' => true,
      ]);
    } catch (\Stripe\Exception\CardException $e) {
      // Error code will be authentication_required if authentication is needed
      echo 'Error code is:' . $e->getError()->code;
      $payment_intent_id = $e->getError()->payment_intent->id;
      $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id);
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentIntentCreateParams;
    import com.stripe.model.PaymentIntent;
    
    PaymentIntentCreateParams createParams = new PaymentIntentCreateParams.Builder()
      .setCurrency("usd")
      .setAmount(1099)
      .setPaymentMethod("{{PAYMENT_METHOD_ID}}")
      .setCustomer("{{CUSTOMER_ID}}")
      .setConfirm(true)
      .setOffSession(true)
      .build();
    try {
      PaymentIntent.create(createParams);
    } catch (CardException err) {
      // Error code will be authentication_required if authentication is needed
      System.out.println("Error code is : " + e.getCode());
      String paymentIntentId = e.getStripeError().getPaymentIntent().getId();
      PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
      System.out.println(paymentIntent.getId());
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      try {
        const paymentIntent = await stripe.paymentIntents.create({
          amount: 1099,
          currency: 'usd',
          customer: '{{CUSTOMER_ID}}',
          payment_method: '{{PAYMENT_METHOD_ID}}',
          off_session: true,
          confirm: true,
        });
      } catch (err) {
        // Error code will be authentication_required if authentication is needed
        console.log('Error code is: ', err.code);
        payment_intent_id = err.raw.payment_intent.id;
        stripe.paymentIntents.retrieve(
          payment_intent_id,
          function(err, paymentIntentRetrieved) {
            console.log('PI retrieved: ', paymentIntentRetrieved.id);
          }
        );
      }
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentIntentParams{
      Amount: stripe.Int64(1099),
      Currency: stripe.String(string(stripe.CurrencyUSD)),
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      PaymentMethod: stripe.String("{{PAYMENT_METHOD_ID}}"),
      Confirm: true,
      OffSession: true,
    }
    
    _, err := paymentintent.New(params)
    
    if err != nil {
      if stripeErr, ok := err.(*stripe.Error); ok {
        // Error code will be authentication_required if authentication is needed
        fmt.Printf("Error code: %v
    ", stripeErr.Code)
    
        payment_intent_id := stripeErr.PaymentIntent.ID
        payment_intent, _ := paymentintent.Get(payment_intent_id, nil)
    
        fmt.Printf("PI: %v
    ", payment_intent.ID)
      }
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    try {
      var service = new PaymentIntentService();
      var options = new PaymentIntentCreateOptions
      {
          Amount = 1099,
          Currency = "usd",
          CustomerId = "{{CUSTOMER_ID}}",
          PaymentMethodId = "{{PAYMENT_METHOD_ID}}",
          Confirm = true,
          OffSession = true,
      };
      service.Create(options);
    }  catch (StripeException e)
    {
        switch (e.StripeError.ErrorType)
        {
            case "card_error":
                // Error code will be authentication_required if authentication is needed
                Console.WriteLine("Error code: " + e.StripeError.Code);
                var paymentIntentId = e.StripeError.PaymentIntent.Id;
                var paymentIntent = service.Get(paymentIntentId);
    
                Console.WriteLine(paymentIntent.Id);
                break;
            default:
                break;
        }
    }
    

    4 Handle declines or authentication requests

    Inspect the status property of the PaymentIntent to confirm that the payment completed successfully. If the payment attempt succeeded, the PaymentIntent’s status is succeeded and the off-session payment is complete.

    Start a recovery flow

    If the PaymentIntent has any other status, the payment did not succeed and the request fails. Notify your customer to return to your application (e.g., by email, text, push notification) to complete the payment. We recommend creating a recovery flow in your app that shows why the payment failed initially and lets your customer retry.

    In your recovery flow, retrieve the PaymentIntent via its client secret. Check the PaymentIntent’s last_payment_error to inspect why the payment attempt failed. For card errors, you can show the user the last payment error’s message. Otherwise, you can show a generic failure message.

    func startRecoveryFlow(clientSecret: String) {
        // Retrieve the PaymentIntent
        STPAPIClient.shared().retrievePaymentIntent(withClientSecret: clientSecret) { (paymentIntent, error) in
            guard error == nil, let lastPaymentError = paymentIntent?.lastPaymentError else {
                // Handle error (e.g. allow your customer to retry)
                return
            }
            var failureReason = "Payment failed, try again." // Default to a generic error message
            if lastPaymentError.type == .card {
                failureReason = lastPaymentError.message
            }
            // Display the failure reason to your customer
            // ...
        }
    }
    
    - (void)startRecoveryFlow:(NSString *)clientSecret {
        // Retrieve the PaymentIntent
        [[STPAPIClient sharedClient] retrievePaymentIntentWithClientSecret:clientSecret completion:^(STPPaymentIntent * _Nullable paymentIntent, NSError * _Nullable error) {
            if (error || paymentIntent.status == STPPaymentIntentStatusSucceeded) {
                // Handle error (e.g. allow your customer to retry)
                return;
            }
            NSString *failureReason = @"Payment failed, try again."; // Default to a generic error message
            if (paymentIntent.lastPaymentError.type == STPPaymentIntentLastPaymentErrorTypeCard) {
                // For card errors, the error's message can be shown to your customer
                failureReason = paymentIntent.lastPaymentError.message;
            }
            // Display the failure reason to your customer
            // ...
        }];
    }
    

    Let your customer try again

    Give the customer the option to update or remove their saved saved card and try payment again in your recovery flow. Follow the same steps you did to accept their initial payment with one difference—confirm the original, failed PaymentIntent by reusing its client secret instead of creating a new one.

    5 Test the integration

    By this point you should have an integration that:

    1. Collects card details and makes a payment
    2. Saves the card details to a Customer
    3. Charges the card off-session and has a recovery flow to handle declines and authentication requests

    There are several test cards you can use to make sure this integration is ready for production. Use them with any CVC, postal code, and future expiration date.

    Number Description
    4242424242424242 Succeeds and immediately processes the payment.
    4000002500003155 Requires authentication for the initial purchase, but succeeds for subsequent payments (including off-session ones) as long as the card is setup with setup_future_usage.
    4000002760003184 Requires authentication for the initial purchase, and fails for subsequent payments (including off-session ones) with an authentication_required decline code.
    4000008260003178 Requires authentication for the initial purchase, but fails for subsequent payments (including off-session ones) with an insufficient_funds decline code.
    4000000000009995 Always fails (including the initial purchase) with a decline code of insufficient_funds.

    For the full list of test cards see our guide on testing.

    Overview

    Use the Payment Intents API to save card details from a purchase. There are several use cases:

    • Charge a customer for an e-commerce order and store the details for future purchases
    • Initiate the first payment of a series of recurring payments
    • Charge a deposit and store the details to charge the full amount later

    1 Accept a Payment Client-side Server-side

    To save a card after a payment, first accept a card payment from your customer, with one difference:

    In step 4, when your client confirms the PaymentIntent, add the setup_future_usage parameter. This parameter optimizes authorization rates when the customer’s card is used again. To determine which value to use, consider how you want to use this card in the future.

    How you intend to use the card setup_future_usage enum value to use
    On-session payments only on_session
    Off-session payments only off_session
    Both on and off-session payments off_session

    A card set up for on-session payments can still be used to make off-session payments, but there’s a higher likelihood that the bank will reject the off-session payment and require authentication from the cardholder.

    class CheckoutActivity : AppCompatActivity() {
        // ...
        private lateinit var paymentIntentClientSecret: String
        private lateinit var stripe: Stripe
    
        private fun startCheckout() {
            // ...
    
            // Hook up the pay button to the card widget and stripe instance
            val payButton: Button = findViewById(R.id.payButton)
            payButton.setOnClickListener {
                val params = cardInputWidget.paymentMethodCreateParams
                if (params != null) {
                    val confirmParams = ConfirmPaymentIntentParams
                        .createWithPaymentMethodCreateParams(params, paymentIntentClientSecret, null, false,
                            mapOf("setup_future_usage" to "off_session"))
                    stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey)
    See all 46 lines stripe.confirmPayment(this, confirmParams) } } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) val weakActivity = WeakReference<Activity>(this) // Handle the result of stripe.confirmPayment stripe.onPaymentResult(requestCode, data, object : ApiResultCallback<PaymentIntentResult> { override fun onSuccess(result: PaymentIntentResult) { val paymentIntent = result.intent val status = paymentIntent.status if (status == StripeIntent.Status.Succeeded) { val gson = GsonBuilder().setPrettyPrinting().create() displayAlert(weakActivity.get(), "Payment succeeded", gson.toJson(paymentIntent), restartDemo = true) } else { displayAlert(weakActivity.get(), "Payment failed", paymentIntent.lastPaymentError?.message ?: "") } } override fun onError(e: Exception) { displayAlert(weakActivity.get(), "Payment failed", e.toString()) } }) } }
    public class CheckoutActivity extends AppCompatActivity {
        // ...
        private String paymentIntentClientSecret;
        private Stripe stripe
    
        private void startCheckout() {
            // ...
    
            // Hook up the pay button to the card widget and stripe instance
            Button payButton = findViewById(R.id.payButton);
            payButton.setOnClickListener((View view) -> {
                PaymentMethodCreateParams params = cardInputWidget.getPaymentMethodCreateParams();
                if (params != null) {
                    Map<String, String> extraParams = new HashMap<>();
                    extraParams.put("setup_future_usage", "off_session");
    
                    ConfirmPaymentIntentParams confirmParams = ConfirmPaymentIntentParams
                            .createWithPaymentMethodCreateParams(params, paymentIntentClientSecret, null, false, extraParams);
                    stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey;
    See all 83 lines stripe.confirmPayment(this, confirmParams); } }); } // ... @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Handle the result of stripe.confirmPayment stripe.onPaymentResult(requestCode, data, new PaymentResultCallback(this)); } // ... private static final class PaymentResultCallback implements ApiResultCallback<PaymentIntentResult> { @NonNull private final WeakReference<CheckoutActivity> activityRef; PaymentResultCallback(@NonNull CheckoutActivity activity) { activityRef = new WeakReference<>(activity); } @Override public void onSuccess(@NonNull PaymentIntentResult result) { final CheckoutActivity activity = activityRef.get(); if (activity == null) { return; } PaymentIntent paymentIntent = result.getIntent(); PaymentIntent.Status status = paymentIntent.getStatus(); if (status == PaymentIntent.Status.Succeeded) { // Payment completed successfully Gson gson = new GsonBuilder().setPrettyPrinting().create(); activity.displayAlert( "Payment completed", gson.toJson(paymentIntent), true ); } else if (status == PaymentIntent.Status.RequiresPaymentMethod) { // Payment failed activity.displayAlert( "Payment failed", Objects.requireNonNull(paymentIntent.getLastPaymentError()).message, false ); } } @Override public void onError(@NonNull Exception e) { final CheckoutActivity activity = activityRef.get(); if (activity == null) { return; } // Payment request failed – allow retrying using the same payment method activity.displayAlert("Error", e.toString(), false); } } }

    2 Save the card Server-side

    When the PaymentIntent succeeds, it contains a PaymentMethod identifier representing the customer’s card details. Associate this PaymentMethod with a Customer.

    When creating a new Customer, pass a PaymentMethod ID to immediately add a payment method.

    curl https://api.stripe.com/v1/customers \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d payment_method="{{PAYMENT_METHOD_ID}}"
    
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    customer = Stripe::Customer.create({
      payment_method: intent.payment_method,
    })
    # This creates a new Customer and attaches the PaymentMethod in one API call.
    stripe.Customer.create(
      payment_method=intent.payment_method
    )
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    \Stripe\Customer::create([
      'payment_method' => $intent->payment_method,
    ]);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    Map<String, Object> customerParams = new HashMap<String, Object>();
    customerParams.put("payment_method", intent.getPaymentMethod());
    Customer.create(customerParams);
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    const customer = await stripe.customers.create({
      payment_method: intent.payment_method,
    });
    // This creates a new Customer and attaches the PaymentMethod in one API call.
    customerParams := &stripe.CustomerParams{
        PaymentMethod: intent.PaymentMethod.ID,
    }
    c, err := customer.New(customerParams)
    var options = new CustomerCreateOptions {
      PaymentMethod = intent.PaymentMethodId,
    };
    
    var customer = new CustomerService();
    Customer customer = service.Create(options);
    

    If you have an existing Customer, you can attach the PaymentMethod to that object instead.

    curl https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/attach  \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}"
    
    payment_method = Stripe::PaymentMethod.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    )
    
    payment_method = stripe.PaymentMethod.attach(
      intent.payment_method,
      customer='{{CUSTOMER_ID}}'
    )
    $payment_method = \Stripe\PaymentMethod::retrieve($intent->payment_method);
    $payment_method->attach(['customer' => '{{CUSTOMER_ID}}']);
    PaymentMethod paymentMethod = PaymentMethod.retrieve(intent.getPaymentMethod());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("customer", "{{CUSTOMER_ID}}");
    paymentMethod.attach(params);
    const paymentMethod = await stripe.paymentMethods.attach(
      intent.payment_method,
      {
        customer: '{{CUSTOMER_ID}}',
      }
    );
    params := &stripe.PaymentMethodAttachParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
    }
    p, err := paymentmethod.Attach(intent.PaymentMethod.ID, params)
    var options = new PaymentMethodAttachOptions
    {
        Customer = "{{CUSTOMER_ID}}",
    };
    var service = new PaymentMethodService();
    var paymentMethod = service.Attach(intent.PaymentMethodId, options);

    At this point, associate the ID of the Customer object with your own internal representation of a customer, if you have one. Now you can use the stored PaymentMethod object to collect payments from your customer in the future without prompting them for their payment details again.

    Great! You’ve saved the card for reuse. You can use the list API to retrieve a Customer’s saved cards.

    To charge the card again on-session, include their Customer ID when you create the PaymentIntent and reuse their saved PaymentMethod ID when you confirm it.

    To charge the card again off-session, see the next steps.

    3 Charge the card off-session Server-side

    When you are ready to charge your customer off-session, use the Customer and PaymentMethod IDs to create a PaymentIntent.

    To find a card to charge, list the PaymentMethods associated with your Customer.

    curl https://api.stripe.com/v1/payment_methods \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d customer="{{CUSTOMER_ID}}" \
      -d type=card
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::PaymentMethod.list({
      customer: '{{CUSTOMER_ID}}',
      type: 'card',
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.PaymentMethod.list(
      customer="{{CUSTOMER_ID}}",
      type="card",
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\PaymentMethod::all([
      'customer' => '{{CUSTOMER_ID}}',
      'type' => 'card',
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentMethodListParams;
    import com.stripe.model.PaymentMethodCollection;
    
    PaymentMethodListParams listParams = new PaymentMethodListParams.Builder().setCustomer("{{CUSTOMER_ID}}")
            .setType(PaymentMethodListParams.Type.CARD).build();
    
    PaymentMethodCollection paymentMethods = PaymentMethod.list(listParams);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      const paymentIntent = await stripe.paymentMethods.list({
        customer: '{{CUSTOMER_ID}}',
        type: 'card',
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentMethodListParams{
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      Type: stripe.String("card"),
    }
    i := paymentmethod.List(params)
    for i.Next() {
      paymentMethod := i.PaymentMethod()
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var options = new PaymentMethodListOptions
    {
      Customer = "{{CUSTOMER_ID}}",
      Type = "card",
    };
    var service = new PaymentMethodService();
    service.List(options);
    

    When you have the Customer and PaymentMethod IDs, create a PaymentIntent with the amount and currency of the payment. Set a few other parameters to make the off-session payment:

    • Set off_session to true to indicate that the customer is not in your checkout flow during this payment attempt. This causes the PaymentIntent to throw an error if authentication is required.
    • Set the value of the PaymentIntent’s confirm property to true, which causes confirmation to occur immediately when the PaymentIntent is created.
    • Set payment_method to the ID of the PaymentMethod and customer to the ID of the Customer.
    curl https://api.stripe.com/v1/payment_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d amount=1099 \
      -d currency=usd \
      -d customer="{{CUSTOMER_ID}}" \
      -d payment_method="{{PAYMENT_METHOD_ID}}" \
      -d off_session=true \
      -d confirm=true
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    begin:
      intent = Stripe::PaymentIntent.create({
          amount: 1099,
          currency: 'usd',
          customer: '{CUSTOMER_ID}',
          payment_method: '{PAYMENT_METHOD_ID}',
          off_session: true,
          confirm: true,
      })
    rescue Stripe::CardError => e
      # Error code will be authentication_required if authentication is needed
      puts "Error is: #{e.error.code}"
      payment_intent_id = e.error.payment_intent.id
      payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
      puts payment_intent.id
    end
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    try:
      stripe.PaymentIntent.create(
        amount=1099,
        currency='usd',
        customer='{{CUSTOMER_ID}}',
        payment_method='{{PAYMENT_METHOD_ID}}',
        off_session=True,
        confirm=True,
      )
    except stripe.error.CardError as e:
      err = e.error
      # Error code will be authentication_required if authentication is needed
      print("Code is: %s" % err.code)
      payment_intent_id = err.payment_intent['id']
      payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    try {
      \Stripe\PaymentIntent::create([
          'amount' => 1099,
          'currency' => 'usd',
          'customer' => '{{CUSTOMER_ID}}',
          'payment_method' => '{{PAYMENT_METHOD_ID}}',
          'off_session' => true,
          'confirm' => true,
      ]);
    } catch (\Stripe\Exception\CardException $e) {
      // Error code will be authentication_required if authentication is needed
      echo 'Error code is:' . $e->getError()->code;
      $payment_intent_id = $e->getError()->payment_intent->id;
      $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id);
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    import com.stripe.param.PaymentIntentCreateParams;
    import com.stripe.model.PaymentIntent;
    
    PaymentIntentCreateParams createParams = new PaymentIntentCreateParams.Builder()
      .setCurrency("usd")
      .setAmount(1099)
      .setPaymentMethod("{{PAYMENT_METHOD_ID}}")
      .setCustomer("{{CUSTOMER_ID}}")
      .setConfirm(true)
      .setOffSession(true)
      .build();
    try {
      PaymentIntent.create(createParams);
    } catch (CardException err) {
      // Error code will be authentication_required if authentication is needed
      System.out.println("Error code is : " + e.getCode());
      String paymentIntentId = e.getStripeError().getPaymentIntent().getId();
      PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
      System.out.println(paymentIntent.getId());
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      try {
        const paymentIntent = await stripe.paymentIntents.create({
          amount: 1099,
          currency: 'usd',
          customer: '{{CUSTOMER_ID}}',
          payment_method: '{{PAYMENT_METHOD_ID}}',
          off_session: true,
          confirm: true,
        });
      } catch (err) {
        // Error code will be authentication_required if authentication is needed
        console.log('Error code is: ', err.code);
        payment_intent_id = err.raw.payment_intent.id;
        stripe.paymentIntents.retrieve(
          payment_intent_id,
          function(err, paymentIntentRetrieved) {
            console.log('PI retrieved: ', paymentIntentRetrieved.id);
          }
        );
      }
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentIntentParams{
      Amount: stripe.Int64(1099),
      Currency: stripe.String(string(stripe.CurrencyUSD)),
      Customer: stripe.String("{{CUSTOMER_ID}}"),
      PaymentMethod: stripe.String("{{PAYMENT_METHOD_ID}}"),
      Confirm: true,
      OffSession: true,
    }
    
    _, err := paymentintent.New(params)
    
    if err != nil {
      if stripeErr, ok := err.(*stripe.Error); ok {
        // Error code will be authentication_required if authentication is needed
        fmt.Printf("Error code: %v
    ", stripeErr.Code)
    
        payment_intent_id := stripeErr.PaymentIntent.ID
        payment_intent, _ := paymentintent.Get(payment_intent_id, nil)
    
        fmt.Printf("PI: %v
    ", payment_intent.ID)
      }
    }
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    try {
      var service = new PaymentIntentService();
      var options = new PaymentIntentCreateOptions
      {
          Amount = 1099,
          Currency = "usd",
          CustomerId = "{{CUSTOMER_ID}}",
          PaymentMethodId = "{{PAYMENT_METHOD_ID}}",
          Confirm = true,
          OffSession = true,
      };
      service.Create(options);
    }  catch (StripeException e)
    {
        switch (e.StripeError.ErrorType)
        {
            case "card_error":
                // Error code will be authentication_required if authentication is needed
                Console.WriteLine("Error code: " + e.StripeError.Code);
                var paymentIntentId = e.StripeError.PaymentIntent.Id;
                var paymentIntent = service.Get(paymentIntentId);
    
                Console.WriteLine(paymentIntent.Id);
                break;
            default:
                break;
        }
    }
    

    4 Handle declines or authentication requests

    Inspect the status property of the PaymentIntent to confirm that the payment completed successfully. If the payment attempt succeeded, the PaymentIntent’s status is succeeded and the off-session payment is complete.

    Start a recovery flow

    If the PaymentIntent has any other status, the payment did not succeed and the request fails. Notify your customer to return to your application (e.g., by email, text, push notification) to complete the payment. We recommend creating a recovery flow in your app that shows why the payment failed initially and lets your customer retry.

    In your recovery flow, retrieve the PaymentIntent via its client secret. Check the PaymentIntent’s last_payment_error to inspect why the payment attempt failed. For card errors, you can show the user the last payment error’s message. Otherwise, you can show a generic failure message.

    fun startRecoveryFlow(clientSecret: String) {
        AsyncTask.execute {
            val stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey)
            val paymentIntent = stripe.retrievePaymentIntentSynchronous(clientSecret)
            var failureReason = "Payment failed, try again" // Default to a generic error message
            paymentIntent?.lastPaymentError?.let { lastPaymentError ->
                if (lastPaymentError.type == PaymentIntent.Error.Type.CardError) {
                    lastPaymentError.message?.let { errorMessage ->
                        failureReason = errorMessage
                    }
                }
            }
            // Display the failure reason to your customer
        }
    }
    
    public void startRecoveryFlow(@NonNull  String clientSecret) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                final Stripe stripe = new Stripe(
                    getApplicationContext(),
                    PaymentConfiguration.getInstance(this).getPublishableKey()
                );
                try {
                    final PaymentIntent paymentIntent =
                        stripe.retrievePaymentIntentSynchronous(clientSecret);
                    final PaymentIntent.Error lastPaymentError = paymentIntent != null ?
                        paymentIntent.getLastPaymentError() : null;
                    final String failureReason;
                    if (lastPaymentError != null &&
                        PaymentIntent.Error.Type.CardError.equals(lastPaymentError.getType())) {
                        failureReason = lastPaymentError.getMessage();
                    } else {
                        failureReason = "Payment failed, try again"; // Default to a generic error message
                    }
                    // Display the failure reason to your customer
                } catch (Exception e) {
                    // Handle error
                }
            }
        });
    }
    

    Let your customer try again

    Give the customer the option to update or remove their saved saved card and try payment again in your recovery flow. Follow the same steps you did to accept their initial payment with one difference—confirm the original, failed PaymentIntent by reusing its client secret instead of creating a new one.

    5 Test the integration

    By this point you should have an integration that:

    1. Collects card details and makes a payment
    2. Saves the card details to a Customer
    3. Charges the card off-session and has a recovery flow to handle declines and authentication requests

    There are several test cards you can use to make sure this integration is ready for production. Use them with any CVC, postal code, and future expiration date.

    Number Description
    4242424242424242 Succeeds and immediately processes the payment.
    4000002500003155 Requires authentication for the initial purchase, but succeeds for subsequent payments (including off-session ones) as long as the card is setup with setup_future_usage.
    4000002760003184 Requires authentication for the initial purchase, and fails for subsequent payments (including off-session ones) with an authentication_required decline code.
    4000008260003178 Requires authentication for the initial purchase, but fails for subsequent payments (including off-session ones) with an insufficient_funds decline code.
    4000000000009995 Always fails (including the initial purchase) with a decline code of insufficient_funds.

    For the full list of test cards see our guide on testing.

    See also

    Now that you have a working integration, learn about handling post-payment events:

    Was this page helpful?

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

    On this page