Set up recurring payments

    Learn how to save card details and charge your customers later.

    Overview

    The Setup Intents API lets you save a customer’s card without an initial payment. This is helpful if you want to onboard customers now, set them up for payments, and charge them in the future—when they’re offline.

    Use this integration to set up recurring payments or to create one-time payments with a final amount determined later, often after the customer receives your service.

    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 SetupIntent Server-side

    A SetupIntent is an object that represents your intent to set up a customer’s card for future payments.

    The SetupIntent object contains a client secret, a unique key that you need to pass to Stripe.js on the client side to collect card details. The client secret lets you perform certain actions on the client, such as confirming the setup and updating payment method details, while hiding sensitive information like customer. The client secret can be used to validate and authenticate card details via the credit card networks. Because of its sensitive nature, the client secret should not be logged, embedded in URLs, or exposed to anyone other than the customer.

    If your application uses server-side rendering, use your template framework to embed the client secret in the page using a data attribute or a hidden HTML element.

    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="<%= @intent.client_secret %>">
      Save Card
    </button>
    get '/card-wallet' do
        @intent = Stripe::SetupIntent.create
        erb :card_wallet
    end
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="{{ client_secret }}">
      Save Card
    </button>
    @app.route('/card-wallet')
    def card_wallet():
      intent = stripe.SetupIntent.create()
      return render_template('card_wallet.html', client_secret=intent.client_secret)
    <?php
        $intent = \Stripe\SetupIntent::create();
    ?>
    ...
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="<?= $intent->client_secret ?>">
      Save Card
    </button>
    ...
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="{{ client_secret }}">
      Save Card
    </button>
    import java.util.HashMap;
    import java.util.Map;
    
    import com.stripe.model.SetupIntent;
    
    import spark.ModelAndView;
    
    import static spark.Spark.get;
    
    public class StripeJavaQuickStart {
      public static void main(String[] args) {
        get("/card-wallet", (request, response) -> {
          SetupIntent intent = // ... Fetch or create the SetupIntent
    
          Map<String, String> map = new HashMap();
          map.put("client_secret", intent.getClientSecret());
    
          return new ModelAndView(map, "card_wallet.hbs");
        }, new HandlebarsTemplateEngine());
      }
    }
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="{{ client_secret }}">
      Save Card
    </button>
    const express = require('express');
    const expressHandlebars = require('express-handlebars');
    const app = express();
    
    app.engine('.hbs', expressHandlebars({ extname: '.hbs' }));
    app.set('view engine', '.hbs');
    app.set('views', './views');
    
    app.get('/card-wallet', async (req, res) => {
      const intent =  await stripe.setupIntents.create();
      res.render('card_wallet', { client_secret: intent.client_secret });
    });
    
    app.listen(3000, () => {
      console.log('Running on port 3000')
    });
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="{{ .ClientSecret }}">
      Save Card
    </button>
    package main
    
    import (
      "html/template"
      "net/http"
    
      stripe "github.com/stripe/stripe-go"
    )
    
    type WalletData struct {
      ClientSecret string
    }
    
    func main() {
      cardWalletTmpl := template.Must(template.ParseFiles("views/card_wallet.html"))
    
      http.HandleFunc("/card-wallet", func(w http.ResponseWriter, r *http.Request) {
        params := &stripe.SetupIntentParams{}
        intent, err := setupintent.New(params)
        data := WalletData{
          ClientSecret: intent.ClientSecret,
        }
        cardWalletTmpl.Execute(w, data)
      })
    
      http.ListenAndServe(":3000", nil)
    }
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret='@ViewData["ClientSecret"]'>
      Save Card
    </button>
    using System;
    using Microsoft.AspNetCore.Mvc;
    using Stripe;
    
    namespace StripeExampleApi.Controllers
    {
        [Route("/[controller]")]
        public class CardWalletController : Controller
        {
            public IActionResult Index()
            {
              var options = new SetupIntentCreateOptions{};
              var service = new SetupIntentService();
              SetupIntent intent = service.Create(options);
              ViewData["ClientSecret"] = intent.ClientSecret;
              return View();
            }
        }
    }

    If you only plan on using the card for payments when your customer is present during the checkout flow, set the usage parameter to on_session to optimize authorization rates.

    3 Collect card details Client-side

    The Setup Intents API is fully integrated with Stripe.js, which lets you use the Elements UI library to securely collect card details on the client side.

    To get started with Elements, include the following script on 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.

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

    To best leverage Stripe’s advanced fraud functionality, include this script on every page on your site, not just the checkout page. Including the script on every page allows Stripe to detect anomalous behavior that may be indicative of fraud as users browse your website.

    Add Elements to your page

    Next, create an instance of the Stripe object, providing your publishable API key as the first parameter. Afterwards, create an instance of the Elements object and use it to mount a Card element in the DOM.

    var stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
    
    var elements = stripe.elements();
    var cardElement = elements.create('card');
    cardElement.mount('#card-element');
    const stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
    
    const elements = stripe.elements();
    const cardElement = elements.create('card');
    cardElement.mount('#card-element');

    To complete the setup, retrieve the client secret from the SetupIntent created in step two and use stripe.confirmCardSetup and the Card element to complete the setup. When the setup completes successfully, the value of the returned SetupIntent’s status property is succeeded.

    var cardholderName = document.getElementById('cardholder-name');
    var cardButton = document.getElementById('card-button');
    var clientSecret = cardButton.dataset.secret;
    
    cardButton.addEventListener('click', function(ev) {
    
      stripe.confirmCardSetup(
        clientSecret,
        {
          payment_method: {
            card: cardElement,
            billing_details: {name: cardholderName.value}
          }
        }
      ).then(function(result) {
        if (result.error) {
          // Display error.message in your UI.
        } else {
          // The setup has succeeded. Display a success message.
        }
      });
    });
    const cardholderName = document.getElementById('cardholder-name');
    const cardButton = document.getElementById('card-button');
    const clientSecret = cardButton.dataset.secret;
    
    cardButton.addEventListener('click', async (ev) => {
      const {setupIntent, error} = await stripe.confirmCardSetup(
        clientSecret,
        {
          payment_method: {
            card: cardElement,
            billing_details: {name: cardholderName.value}
          }
        }
      );
    
      if (error) {
        // Display error.message in your UI.
      } else {
        if (setupIntent.status === 'succeeded') {
          // The setup has succeeded. Display a success message. Send
          // setupIntent.payment_method to your server to save the card to a Customer
        }
      }
    });

    The SetupIntent verifies that the card information your customer is using is valid on the network. Do not maintain long-lived, unhandled SetupIntents as they may no longer be valid when you call stripe.confirmCardSetup.

    Use the test card, 4000 0025 0000 3155, along with any CVC, postal code, and future expiration date to try out the authentication process during a test payment.

    You now have a flow to collect card details and handle any authentication requests.

    Next, send the resulting PaymentMethod ID (from result.setupIntent.payment_method) to your server and follow the remaining steps to save the card to a customer and charge the card in the future.

    4 Save the card to a customer Server-side

    Once the SetupIntent has succeeded, associate the card details with a Customer. When creating a new Customer, pass a PaymentMethod ID to immediately add a payment method.

    Send the result from confirmCardSetup to your server or set up a webhook to listen for the setup_intent.succeeded event.

    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 saved PaymentMethod object to collect payments from your customer in the future without prompting them for their card details again. Use list to retrieve a customer’s saved PaymentMethods when you need to create a payment.

    5 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.

    6 Test the integration

    By this point you should have an integration that:

    1. Collects card details without charging the customer by using a SetupIntent
    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

    The Setup Intents API lets you save a customer’s card without an initial payment. This is helpful if you want to onboard customers now, set them up for payments, and charge them in the future—when they’re offline.

    Use this integration to set up recurring payments or to create one-time payments with a final amount determined later, often after the customer receives your service.

    1 Set up Stripe Client-side Server-side

    First, you need a Stripe account. Register now.

    Server-side

    This integration requires endpoints on your server that talk to the Stripe API. Use our official libraries for access to the Stripe API from your server:

    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

    Client-side

    The iOS SDK is open source, fully documented, and compatible with apps supporting iOS 9 or above.

    1. If you haven't already, install the latest version of CocoaPods.
    2. If you don't have an existing Podfile, run the following command to create one:
      pod init
    3. Add this line to your Podfile:
      pod 'Stripe'
    4. Run the following command:
      pod install
    5. Don't forget to use the .xcworkspace file to open your project in Xcode, instead of the .xcodeproj file, from here on out.
    6. In the future, to update to the latest version of the SDK, just run:
      pod update Stripe
    1. If you haven't already, install the latest version of Carthage.
    2. Add this line to your Cartfile:
      github "stripe/stripe-ios"
    3. Follow the Carthage installation instructions.
    4. In the future, to update to the latest version of the SDK, run the following command:
      carthage update stripe-ios --platform ios
    1. Head to our GitHub releases page and download and unzip Stripe.framework.zip.
    2. Drag Stripe.framework to the "Embedded Binaries" section of your Xcode project's "General" settings. Make sure to select "Copy items if needed".
    3. Head to the "Build Phases" section of your Xcode project settings, and create a new "Run Script Build Phase". Paste the following snippet into the text field:
      bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Stripe.framework/integrate-dynamic-framework.sh"
    4. In the future, to update to the latest version of our SDK, just repeat steps 1 and 2.

    When your app starts, configure the SDK with your Stripe publishable key so that it can make requests to the Stripe API.

    import UIKit
    import Stripe
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            Stripe.setDefaultPublishableKey("pk_test_TYooMQauvdEDq54NiTphI7jx")
            // do any other necessary launch configuration
            return true
        }
    }
    #import "AppDelegate.h"
    #import <Stripe/Stripe.h>
    
    @implementation AppDelegate
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [Stripe setDefaultPublishableKey:@"pk_test_TYooMQauvdEDq54NiTphI7jx"];
        // do any other necessary launch configuration
        return YES;
    }
    @end

    2 Create a SetupIntent Server-side

    A SetupIntent is an object that represents your intent to set up a payment method for future payments.

    The SetupIntent object contains a client secret, a unique key that you pass to your app. The client secret lets you perform certain actions on the client, such as confirming the setup and updating payment method details, while hiding sensitive information like customer. The client secret can be used to validate and authenticate card details via the credit card networks. Because of its sensitive nature, the client secret should not be logged, embedded in URLs, or exposed to anyone other than the customer.

    Server-side

    On your server, make an endpoint that creates a SetupIntent and returns its client secret to your app.

    curl https://api.stripe.com/v1/setup_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
    
    # 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'
    
    setup_intent = Stripe::SetupIntent.create
    client_secret = setup_intent['client_secret']
    # Pass the client secret to the client
    
    setup_intent = stripe.SetupIntent.create()
    client_secret = setup_intent.client_secret
    # Pass the client secret to the client
    
    $setup_intent = \Stripe\SetupIntent::create();
    $client_secret = $setup_intent->client_secret
    // Pass the client secret to the client
    
    Map<String, Object> params = new HashMap<>();
    SetupIntent setupIntent = SetupIntent.create(params);
    String clientSecret = setupIntent.getClientSecret();
    // Pass the client secret to the client
    
    (async () => {
      const setupIntent = await stripe.setupIntents.create({})
      const clientSecret = setupIntent.client_secret
      // Pass the client secret to the client
    })();
    
    params := &stripe.SetupIntentParams{}
    intent, err := setupintent.New(params)
    clientSecret := intent.ClientSecret
    // Pass the client secret to the client
    
    var options = new SetupIntentCreateOptions{};
    var service = new SetupIntentService();
    SetupIntent setupIntent = service.Create(options);
    var clientSecret = setupIntent.ClientSecret;
    // Pass the client secret to the client
    

    If you only plan on using the card for payments when your customer is present during the checkout flow, set the usage parameter to on_session to optimize authorization rates.

    Client-side

    On the client, request a SetupIntent from your server.

    import Stripe
    
    class CheckoutViewController: UIViewController {
        var setupIntentClientSecret: String?
    
        func startCheckout() {
            // Request a SetupIntent from your server and store its client secret
            // Click Open on GitHub to see a full implementation
        }
    }
    #import "CheckoutViewController.h"
    #import <Stripe/Stripe.h>
    
    @interface CheckoutViewController ()  <STPAuthenticationContext>
    @property (nonatomic, copy) NSString *setupIntentClientSecret;
    @end
    
    @implementation CheckoutViewController
    
    - (void)startCheckout {
        // Request a SetupIntent from your server and store its client secret
        // Click Open on GitHub to see a full implementation
    }
    
    @end
    

    3 Collect card details Client-side

    When the customer submits the payment form, collect card details from the customer using STPPaymentCardTextField, a drop-in UI component provided by the SDK.

    STPPaymentCardTextField performs on-the-fly validation and formatting.

    Pass the collected information into new STPPaymentMethodCardParams and STPPaymentMethodBillingDetails instances to create an STPSetupIntentConfirmParams instance.

    class CheckoutViewController: UIViewController {
        lazy var cardTextField: STPPaymentCardTextField = {
            let cardTextField = STPPaymentCardTextField()
            return cardTextField
        }()
    
        func pay() {
            // Collect card details
            let cardParams = cardTextField.cardParams
            // Fill in any billing details...
            let billingDetails = STPPaymentMethodBillingDetails()
    
            // Create SetupIntent confirm parameters with the above
            let paymentMethodParams = STPPaymentMethodParams(card: cardParams, billingDetails: billingDetails, metadata: nil)
            let setupIntentParams = STPSetupIntentConfirmParams(clientSecret: setupIntentClientSecret)
            setupIntentParams.paymentMethodParams = paymentMethodParams
    
            // ...continued in next step
        }
    }
    
    @interface CheckoutViewController ()
    @property (nonatomic, weak) STPPaymentCardTextField *cardTextField;
    @end
    
    @implementation CheckoutViewController
    
    - (void)pay {
        // Collect card details
        STPPaymentMethodCardParams *cardParams = self.cardTextField.cardParams;
        // Fill in any billing details...
        STPPaymentMethodBillingDetails *billingDetails = [STPPaymentMethodBillingDetails new];
    
        // Create SetupIntent confirm parameters with the above
        STPPaymentMethodParams *paymentMethodParams = [STPPaymentMethodParams paramsWithCard:cardParams billingDetails:billingDetails metadata:nil];
        STPSetupIntentConfirmParams *setupIntentParams = [[STPSetupIntentConfirmParams alloc] initWithClientSecret:self.setupIntentClientSecret];
        setupIntentParams.paymentMethodParams = paymentMethodParams;
    
        // ...continued in next step
    }
    
    @end
    

    To complete the setup, pass the STPSetupIntentConfirmParams object to the confirmSetupIntent method on STPPaymentHandler sharedManager.

    If the customer must perform additional steps to complete the payment, such as authentication, STPPaymentHandler presents view controllers using the STPAuthenticationContext passed in and walks them through that process. See Supporting 3D Secure Authentication on iOS to learn more.

    class CheckoutViewController: UIViewController {
        // ...
    
        func pay() {
            // ...
    
            // Complete the setup
            let paymentHandler = STPPaymentHandler.shared()
            paymentHandler.confirmSetupIntent(withParams: setupIntentParams, authenticationContext: self) { status, setupIntent, error in
                switch (status) {
                case .failed:
                    // Setup failed
                    break
                case .canceled:
                    // Setup canceled
                    break
                case .succeeded:
                    // Setup succeeded
                    break
                @unknown default:
                    fatalError()
                    break
                }
            }
        }
    }
    
    extension CheckoutViewController: STPAuthenticationContext {
        func authenticationPresentingViewController() -> UIViewController {
            return self
        }
    }
    
    
    @interface CheckoutViewController ()  <STPAuthenticationContext>
    @end
    
    @implementation CheckoutViewController
    
    - (void)pay {
        // ...
        STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler];
        [paymentHandler confirmSetupIntent:setupIntentParams withAuthenticationContext:self completion:^(STPPaymentHandlerActionStatus status, STPSetupIntent *setupIntent, NSError *error) {
            switch (status) {
                case STPPaymentHandlerActionStatusFailed: {
                    // Setup failed
                    break;
                }
                case STPPaymentHandlerActionStatusCanceled: {
                    // Setup canceled
                    break;
                }
                case STPPaymentHandlerActionStatusSucceeded: {
                    // Setup succeeded
                    break;
                }
                default:
                    break;
            }
        }];
    }
    
    # pragma mark STPAuthenticationContext
    - (UIViewController *)authenticationPresentingViewController {
        return self;
    }
    

    The SetupIntent verifies that the card information your customer is using is valid on the network. Do not maintain long-lived, unhandled SetupIntents as they may no longer be valid when you call confirmSetupIntent.

    You now have a flow to collect card details and handle any authentication requests. Use the test card, 4000 0025 0000 3155, along with any CVC, postal code, and future expiration date to see an example authentication flow.

    Next, send the resulting PaymentMethod ID (from setupIntent.paymentMethodID) to your server and follow the remaining steps to save the card to a customer and charge the card in the future.

    4 Save the card to a customer Server-side

    Once the SetupIntent has succeeded, associate the card details 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 saved PaymentMethod object to collect payments from your customer in the future without prompting them for their card details again. Use list to retrieve a customer’s saved PaymentMethods when you need to create a payment.

    5 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;
        }
    }
    

    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
            // ...continued in next step
        }
    }
    
    - (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
            // ...continued in next step
        }];
    }
    

    Let your customer try again

    Give the customer the option to try payment again on-session and save the card if the customer enters a new payment method by following the steps here with two differences:

    • Reuse the existing PaymentIntent
    • If the payment failed because it requires authentication, try again with the existing PaymentMethod
    func startRecoveryFlow(clientSecret: String) {
        // ...continued from previous step
        // Reuse the existing PaymentIntent's client secret
        let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
        if paymentIntent.lastPaymentError.code == STPPaymentIntentLastPaymentErrorCodeAuthenticationFailure {
            // Payment failed because authentication is required, reuse the PaymentMethod
            paymentIntentParams.paymentMethodId = paymentIntent.lastPaymentError.paymentMethod.stripeId
        } else {
            // Collect a new PaymentMethod from the customer...
        }
    
        // Submit the payment...
    }
    
    - (void)startRecoveryFlow:(NSString *)clientSecret {
        // ...continued from previous step
        // Reuse the existing PaymentIntent's client secret
        STPPaymentIntentParams *paymentIntentParams = [STPPaymentIntentParams alloc] initWithClientSecret:clientSecret]
        if ([paymentIntent.lastPaymentError.code isEqualToString:STPPaymentIntentLastPaymentErrorCodeAuthenticationFailure]) {
            // Payment failed because authentication is required, reuse the PaymentMethod
            paymentIntentParams.paymentMethodId = paymentIntent.lastPaymentError.paymentMethod.stripeId;
        } else {
            // Collect a new PaymentMethod from the customer...
        }
    
        // Submit the payment...
    }
    

    6 Test the integration

    By this point you should have an integration that:

    1. Collects card details without charging the customer by using a SetupIntent
    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

    The Setup Intents API lets you save a customer’s card without an initial payment. This is helpful if you want to onboard customers now, set them up for payments, and charge them in the future—when they’re offline.

    Use this integration to set up recurring payments or to create one-time payments with a final amount determined later, often after the customer receives your service.

    1 Set up Stripe Client-side Server-side

    First, you need a Stripe account. Register now.

    Server-side

    This integration requires endpoints on your server that talk to the Stripe API. Use our official libraries for access to the Stripe API from your server:

    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

    Client-side

    The Android SDK is open source and fully documented.

    To install the SDK, add stripe-android to the dependencies block of your app/build.gradle file:

    apply plugin: 'com.android.application'
    
    android { ... }
    
    dependencies {
      // ...
    
      // Stripe Android SDK
      implementation 'com.stripe:stripe-android:12.4.0'
    }
    

    Configure the SDK with your Stripe publishable key so that it can make requests to the Stripe API, such as in your Application subclass:

    import com.stripe.android.PaymentConfiguration
    
    class MyApp: Application() {
        override fun onCreate() {
            super.onCreate()
            PaymentConfiguration.init(applicationContext, "pk_test_TYooMQauvdEDq54NiTphI7jx")
        }
    }
    
    import com.stripe.android.PaymentConfiguration;
    
    public class MyApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            PaymentConfiguration.init(getApplicationContext(), "pk_test_TYooMQauvdEDq54NiTphI7jx");
        }
    }
    

    2 Create a SetupIntent Server-side

    A SetupIntent is an object that represents your intent to set up a payment method for future payments.

    The SetupIntent object contains a client secret, a unique key that you pass to your app. The client secret lets you perform certain actions on the client, such as confirming the setup and updating payment method details, while hiding sensitive information like customer. The client secret can be used to validate and authenticate card details via the credit card networks. Because of its sensitive nature, the client secret should not be logged, embedded in URLs, or exposed to anyone other than the customer.

    Server-side

    On your server, make an endpoint that creates a SetupIntent and returns its client secret to your app.

    curl https://api.stripe.com/v1/setup_intents \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
    
    # 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'
    
    setup_intent = Stripe::SetupIntent.create
    client_secret = setup_intent['client_secret']
    # Pass the client secret to the client
    
    setup_intent = stripe.SetupIntent.create()
    client_secret = setup_intent.client_secret
    # Pass the client secret to the client
    
    $setup_intent = \Stripe\SetupIntent::create();
    $client_secret = $setup_intent->client_secret
    // Pass the client secret to the client
    
    Map<String, Object> params = new HashMap<>();
    SetupIntent setupIntent = SetupIntent.create(params);
    String clientSecret = setupIntent.getClientSecret();
    // Pass the client secret to the client
    
    (async () => {
      const setupIntent = await stripe.setupIntents.create({})
      const clientSecret = setupIntent.client_secret
      // Pass the client secret to the client
    })();
    
    params := &stripe.SetupIntentParams{}
    intent, err := setupintent.New(params)
    clientSecret := intent.ClientSecret
    // Pass the client secret to the client
    
    var options = new SetupIntentCreateOptions{};
    var service = new SetupIntentService();
    SetupIntent setupIntent = service.Create(options);
    var clientSecret = setupInent.ClientSecret;
    // Pass the client secret to the client
      

    If you only plan on using the card for payments when your customer is present during the checkout flow, set the usage parameter to on_session to optimize authorization rates.

    Client-side

    On the client, request a SetupIntent from your server.

    class CheckoutActivity : AppCompatActivity() {
       private lateinit var setupIntentClientSecret: String
    
       private fun loadPage() {
           // Request a SetupIntent from your server and store its client secret
           // Click Open on GitHub to see a full implementation
       }
    }
    public class CheckoutActivity extends AppCompatActivity {
        private String setupIntentClientSecret;
    
        private void loadPage() {
            // Request a SetupIntent from your server and store its client secret
            // Click Open on GitHub to see a full implementation
        }
    }
    

    3 Collect card details Client-side

    When the customer submits the payment form, collect their card details using CardInputWidget, a drop-in UI component provided by the SDK.

    CardInputWidget performs on-the-fly validation and formatting.

    Call the getPaymentMethodCard method to retrieve the card details. Pass the collected information into new PaymentMethodCreateParams and PaymentMethod.BillingDetails instances to create a ConfirmSetupIntentParams instance.

    // Collect card details
    val cardInputWidget =
        findViewById<CardInputWidget>(R.id.cardInputWidget)
    val paymentMethodCard = cardInputWidget.paymentMethodCard
    
    val emailInput = findViewById<EditText>(R.id.emailInput)
    val billingDetails = PaymentMethod.BillingDetails.Builder()
        // ...
        .build()
    
    // Create SetupIntent confirm parameters with the above
    if (paymentMethodCard != null) {
        val paymentMethodParams = PaymentMethodCreateParams
            .create(paymentMethodCard, billingDetails, null)
        val confirmParams = ConfirmSetupIntentParams
            .create(paymentMethodParams, setupIntentClientSecret)
        stripe.confirmSetupIntent(this, confirmParams)
    }
    
    // Collect card details
    CardInputWidget cardInputWidget = findViewById(R.id.cardInputWidget);
    PaymentMethodCreateParams.Card card = cardInputWidget.getPaymentMethodCard();
    
    PaymentMethod.BillingDetails billingDetails = (new PaymentMethod.BillingDetails.Builder())
            // ...
            .build();
    if (card != null) {
        // Create SetupIntent confirm parameters with the above
        PaymentMethodCreateParams paymentMethodParams = PaymentMethodCreateParams
                .create(card, billingDetails);
        ConfirmSetupIntentParams confirmParams = ConfirmSetupIntentParams
                .create(paymentMethodParams, setupIntentClientSecret);
        stripe.confirmSetupIntent(this, confirmParams);
    }
    

    To complete the setup, pass the SetupIntentParams object with the current Activity to Stripe#confirmSetupIntent(). The SetupIntent verifies that the card information your customer is using is valid on the network. Do not maintain long-lived, unhandled SetupIntents as they may no longer be valid when you call Stripe#confirmSetupIntent().

    Some payment methods require additional authentication steps in order to complete payment. The SDK manages the payment confirmation and authentication flow, which may involve presenting additional screens required for authentication. For more information on 3D Secure authentication and customizing the authentication experience, see Supporting 3D Secure Authentication on Android.

    The result of the flow returns to your calling Activity via Activity#onActivityResult(). Handle the result by calling Stripe#onSetupResult() within Activity#onActivityResult(). The SetupIntentResult returned in ApiResultCallback#onSuccess() has two fields:

    • setupIntent: A SetupIntent object retrieved after confirmation and authentication
    • status: A SetupIntentResult.Status value that indicates the outcome of authentication
      • SUCCEEDED - confirmation or authentication succeeded
      • FAILED - confirmation or authentication failed
      • CANCELED - the customer canceled required authentication
      • TIMEDOUT - the authentication attempt timed-out
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val weakActivity = WeakReference<Activity>(this)
    
        // Handle the result of stripe.confirmSetupIntent
        stripe.onSetupResult(requestCode, data, object : ApiResultCallback<SetupIntentResult> {
            override fun onSuccess(result: SetupIntentResult) {
                val setupIntent = result.intent
                val status = setupIntent.status
                if (status == StripeIntent.Status.Succeeded) {
                    // Setup completed successfully
                } else if (status == StripeIntent.Status.RequiresPaymentMethod) {
                    // Setup failed
                }
            }
    
            override fun onError(e: Exception) {
                // Setup request failed
            }
        })
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        WeakReference<Activity> weakActivity = new WeakReference<>(this);
    
        // Handle the result of stripe.confirmSetupIntent
        stripe.onSetupResult(requestCode, data, new ApiResultCallback<SetupIntentResult>() {
            @Override
            public void onSuccess(@NonNull SetupIntentResult result) {
                SetupIntent setupIntent = result.getIntent();
                SetupIntent.Status status = setupIntent.getStatus();
                if (status == SetupIntent.Status.Succeeded) {
                    // Setup completed successfully
                } else if (status == SetupIntent.Status.RequiresPaymentMethod) {
                    // Setup failed – allow retrying
                }
            }
    
            @Override
            public void onError(@NonNull Exception e) {
                // Setup request failed
            }
        });
    }
    

    You now have a flow to collect card details and handle any authentication requests. Use the test card, 4000 0025 0000 3155, along with any CVC, postal code, and future expiration date to see an example authentication flow.

    Next, send the resulting PaymentMethod ID (from setupIntent.getPaymentMethodId()) to your server and follow the remaining steps to save the card to a customer and charge the card in the future.

    4 Save the card to a customer Server-side

    Once the SetupIntent has succeeded, associate the card details 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 saved PaymentMethod object to collect payments from your customer in the future without prompting them for their card details again. Use list to retrieve a customer’s saved PaymentMethods when you need to create a payment.

    5 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;
        }
    }
    

    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 in your app 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.

    class RecoveryFlowActivity : Activity() {
        private lateinit var stripe: Stripe
    
        fun startRecoveryFlow(clientSecret: String) {
            stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey)
            AsyncTask.execute {
                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 class RecoveryFlowActivity extends Activity {
        private Stripe stripe;
    
        public void startRecoveryFlow(@NonNull  String clientSecret) {
            stripe = new Stripe(
                getApplicationContext(),
                PaymentConfiguration.getInstance(this).getPublishableKey()
            );
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        final PaymentIntent paymentIntent =
                            stripe.retrievePaymentIntentSynchronous(clientSecret);
                        final PaymentIntent.Error lastPaymentError = paymentIntent != null ?
                                paymentIntent.getLastPaymentError() : null;
                        final String failureReason;
                        if (lastPaymentError != null &&
                                lastPaymentError.getType() == PaymentIntent.Error.Type.CardError) {
                            failureReason = lastPaymentError.getMessage();
                        } else {
                            // Default to a generic error message
                            failureReason = "Payment failed, try again";
                        }
                        // Display the failure reason to your customer
                        // ...continued in next step
                    } catch (Exception e) {
                        // Handle error
                    }
                }
            });
        }
    }
    

    Let your customer try again

    Give the customer the option to try payment again on-session and save the card if the customer enters a new payment method by following the steps here with two differences:

    • Reuse the existing PaymentIntent
    • If the payment failed because it requires authentication, try again with the existing PaymentMethod
    fun startRecoveryFlow(clientSecret: String) {
        // ...continued from previous step
        val lastPaymentError = paymentIntent.lastPaymentError
        val lastPaymentMethodId = lastPaymentError.paymentMethod?.id
        if (lastPaymentError?.code == "authentication_required" && lastPaymentMethodId != null) {
            // Payment failed because authentication is required, reuse the PaymentMethod
            val paymentIntentParams =
                ConfirmPaymentIntentParams.createWithPaymentMethodId(
                    lastPaymentMethodId,
                    clientSecret // Reuse the existing PaymentIntent
                )
            // Submit the payment...
            stripe.confirmPayment(this, paymentIntentParams)
        } else {
            // Collect a new PaymentMethod from the customer...
        }
    }
        private void startRecoveryFlow(@NonNull String clientSecret) {
            // ...continued from previous step
            final String lastPaymentMethodId;
            if (lastPaymentError != null && lastPaymentError.getPaymentMethod() != null) {
                lastPaymentMethodId = lastPaymentError.getPaymentMethod().id;
            } else {
                lastPaymentMethodId = null;
            }
    
            if (lastPaymentError != null &&
                    "authentication_required".equals(lastPaymentError.getCode()) &&
                    lastPaymentMethodId != null) {
                // Payment failed because authentication is required, reuse the PaymentMethod
                final ConfirmPaymentIntentParams params =
                        ConfirmPaymentIntentParams.createWithPaymentMethodId(
                                lastPaymentMethodId,
                                clientSecret // Reuse the existing PaymentIntent
                        );
                // Submit the payment...
                stripe.confirmPayment(this, params);
            } else {
                // Collect a new PaymentMethod from the customer...
            }
        }
    }
    
    

    6 Test the integration

    By this point you should have an integration that:

    1. Collects card details without charging the customer by using a SetupIntent
    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.

    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