Save a card after a payment Payment Intents API

    Learn how to save card details after a payment.

    Overview

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

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

    1 Set up Stripe Server-side

    First, you need a Stripe account. Register now.

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

    # Available as a gem sudo gem install stripe
    # If you use bundler, you can add this line to your Gemfile gem 'stripe'
    # Install through pip pip install --upgrade stripe
    # Or find the Stripe package on http://pypi.python.org/pypi/stripe/
    # Install the PHP library via Composer composer require stripe/stripe-php
    # Or download the source directly: https://github.com/stripe/stripe-php/releases
    /* For Gradle, add the following dependency to your build.gradle and replace {VERSION} with the version number you want to use from - https://mvnrepository.com/artifact/com.stripe/stripe-java or - https://github.com/stripe/stripe-java/releases/latest */ implementation "com.stripe:stripe-java:{VERSION}"
    <!-- For Maven, add the following dependency to your POM and replace {VERSION} with the version number you want to use from - https://mvnrepository.com/artifact/com.stripe/stripe-java or - https://github.com/stripe/stripe-java/releases/latest --> <dependency> <groupId>com.stripe</groupId> <artifactId>stripe-java</artifactId> <version>{VERSION}</version> </dependency>
    # For other environments, manually install the following JARs: # - The Stripe JAR from https://github.com/stripe/stripe-java/releases/latest # - Google Gson from https://github.com/google/gson
    # Install via npm npm install --save 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 install via NuGet PM> Install-Package Stripe.net

    2 Create a PaymentIntent Server-side

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

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

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

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

    3 Collect card details Client-side

    You’re ready to collect card information on the client with Stripe Elements. Elements is a set of prebuilt UI components for collecting and validating card number, ZIP code, and expiration date.

    A Stripe Element contains an iframe that securely sends the payment information to Stripe over a HTTPS connection. The checkout page address must also start with https:// rather than http:// for your integration to work.

    You can test your integration without using HTTPS. Enable it when you’re ready to accept live payments.

    Set up Stripe Elements

    Stripe Elements is automatically available as a feature of Stripe.js. Include the Stripe.js script on your checkout page by adding it to the head of your HTML file. Always load Stripe.js directly from js.stripe.com to remain PCI compliant. Do not include the script in a bundle or host a copy of it yourself.

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

    Create an instance of Elements with the following JavaScript on your checkout page:

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

    Add Elements to your payment page

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

    <form id="payment-form"> <div id="card-element"> <!-- Elements will create input elements here --> </div> <!-- We'll put the error messages in this element --> <div id="card-errors" role="alert"></div> <button id="submit">Pay</button> </form>
    /** * Shows how you can use CSS to style your Element's container. * These classes are added to your Stripe Element by default. * You can override these classNames by using the options passed * to the CardElement component. * https://stripe.com/docs/js/elements_object/create_element?type=card#elements_create-options-classes */ .StripeElement { height: 40px; padding: 10px 12px; width: 100%; color: #32325d; background-color: white; border: 1px solid transparent; border-radius: 4px; box-shadow: 0 1px 3px 0 #e6ebf1; -webkit-transition: box-shadow 150ms ease; transition: box-shadow 150ms ease; } .StripeElement--focus { box-shadow: 0 1px 3px 0 #cfd7df; } .StripeElement--invalid { border-color: #fa755a; } .StripeElement--webkit-autofill { background-color: #fefde5 !important; }

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

    // Set up Stripe.js and Elements to use in checkout form var style = { base: { color: "#32325d", fontFamily: '"Helvetica Neue", Helvetica, sans-serif', fontSmoothing: "antialiased", fontSize: "16px", "::placeholder": { color: "#aab7c4", }, }, invalid: { color: "#fa755a", iconColor: "#fa755a", }, }; var card = elements.create("card", { style: style }); card.mount("#card-element");

    The card Element simplifies the form and minimizes the number of required fields by inserting a single, flexible input field that securely collects all necessary card and billing details. Otherwise, combine cardNumber, cardExpiry, and cardCvc Elements for a flexible, multi-input card form.

    For a full list of supported Element types, refer to our Stripe.js reference documentation.

    Elements validates user input as it is typed. To help your customers catch mistakes, listen to change events on the card Element and display any errors:

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

    Postal code validation depends on your customer’s billing country. Use our international test cards to experiment with other postal code formats.

    Install @stripe/react-stripe-js and @stripe/stripe-js:

    npm install --save @stripe/react-stripe-js @stripe/stripe-js

    Add Stripe.js and Elements to your page

    To use Element components, wrap the root of your React app in an Elements provider. Call loadStripe with your publishable key and pass the returned Promise to the Elements provider.

    import React from 'react'; import ReactDOM from 'react-dom'; import {Elements} from '@stripe/react-stripe-js'; import {loadStripe} from '@stripe/stripe-js'; import CheckoutForm from './CheckoutForm'; // Make sure to call `loadStripe` outside of a component’s render to avoid // recreating the `Stripe` object on every render. const stripePromise = loadStripe("pk_test_TYooMQauvdEDq54NiTphI7jx"); function App() { return ( <Elements stripe={stripePromise}> <CheckoutForm /> </Elements> ); }; ReactDOM.render(<App />, document.getElementById('root'));

    Add and configure a CardElement component

    Use individual Element components, such as CardElement, to build your form.

    /** * Use the CSS tab above to style your Element's container. */ import React from 'react'; import {CardElement} from '@stripe/react-stripe-js'; import './CardSectionStyles.css' const CARD_ELEMENT_OPTIONS = { style: { base: { color: "#32325d", fontFamily: '"Helvetica Neue", Helvetica, sans-serif', fontSmoothing: "antialiased", fontSize: "16px", "::placeholder": { color: "#aab7c4", }, }, invalid: { color: "#fa755a", iconColor: "#fa755a", }, }, }; function CardSection() { return ( <label> Card details <CardElement options={CARD_ELEMENT_OPTIONS} /> </label> ); }; export default CardSection;
    /** * Shows how you can use CSS to style your Element's container. * These classes are added to your Stripe Element by default. * You can override these classNames by using the options passed * to the CardElement component. * https://stripe.com/docs/js/elements_object/create_element?type=card#elements_create-options-classes */ .StripeElement { height: 40px; padding: 10px 12px; width: 100%; color: #32325d; background-color: white; border: 1px solid transparent; border-radius: 4px; box-shadow: 0 1px 3px 0 #e6ebf1; -webkit-transition: box-shadow 150ms ease; transition: box-shadow 150ms ease; } .StripeElement--focus { box-shadow: 0 1px 3px 0 #cfd7df; } .StripeElement--invalid { border-color: #fa755a; } .StripeElement--webkit-autofill { background-color: #fefde5 !important; }

    Elements are completely customizable. You can style Elements to match the look and feel of your site, providing a seamless checkout experience for your customers. It’s also possible to style various input states, for example when the Element has focus.

    The CardElement simplifies the form and minimizes the number of required fields by inserting a single, flexible input field that securely collects all necessary card and billing details. Otherwise, combine CardNumberElement, CardExpiryElement, and CardCvcElement elements for a flexible, multi-input card form.

    4 Submit the payment to Stripe Client-side

    Rather than sending the entire PaymentIntent object to the client, use its client secret from step 2. This is different from your API keys that authenticate Stripe API requests.

    The client secret should still be handled carefully because it can complete the charge. Do not log it, embed it in URLs, or expose it to anyone but the customer.

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

    Pass additional billing details, such as the cardholder name and address, to the billing_details hash. The card Element automatically sends the customer’s postal code information. However, combining cardNumber, cardCvc, and cardExpiry Elements requires you to pass the postal code to billing_details[address][postal_code].

    var form = document.getElementById('payment-form'); form.addEventListener('submit', function(event) { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); stripe.confirmCardPayment(clientSecret, { payment_method: { card: card, billing_details: { name: 'Jenny Rosen' } }, setup_future_usage: 'off_session' }).then(function(result) { if (result.error) { // Show error to your customer console.log(result.error.message); } else { if (result.paymentIntent.status === 'succeeded') { // Show a success message to your customer // There's a risk of the customer closing the window before callback execution // Set up a webhook or plugin to listen for the payment_intent.succeeded event // to save the card to a Customer // The PaymentMethod ID can be found on result.paymentIntent.payment_method } } }); });

    To complete the payment when the user clicks, retrieve the client secret from the PaymentIntent you created in step two and call stripe.confirmCardPayment with the client secret and the Element. Pass additional billing details, such as the cardholder name and address, to the billing_details hash.

    To call stripe.confirmCardPayment from your payment form component, use the useStripe and useElements hooks.

    If you prefer traditional class components over hooks, you can instead use an ElementsConsumer.

    import React from 'react'; import {useStripe, useElements, CardElement} from '@stripe/react-stripe-js'; import CardSection from './CardSection'; export default function CheckoutForm() { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not yet loaded. // Make sure to disable form submission until Stripe.js has loaded. return; } const result = await stripe.confirmCardPayment('{{CLIENT_SECRET}}', { payment_method: { card: elements.getElement(CardElement), billing_details: { name: 'Jenny Rosen', }, }, setup_future_usage: 'off_session', }); if (result.error) { // Show error to your customer console.log(result.error.message); } else { if (result.paymentIntent.status === 'succeeded') { // Show a success message to your customer // There's a risk of the customer closing the window // before callback execution. Set up a webhook or plugin // to listen for the payment_intent.succeeded event // to save the card to a Customer // The PaymentMethod ID can be found on result.paymentIntent.payment_method } } }; return ( <form onSubmit={handleSubmit}> <CardSection /> <button disabled={!stripe}>Confirm order</button> </form> ); }
    import React from 'react'; import {ElementsConsumer, CardElement} from '@stripe/react-stripe-js'; import CardSection from './CardSection'; class CheckoutForm extends React.Component { handleSubmit = async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); const {stripe, elements} = this.props if (!stripe || !elements) { // Stripe.js has not yet loaded. // Make sure to disable form submission until Stripe.js has loaded. return; } const result = await stripe.confirmCardPayment('{{CLIENT_SECRET}}', { payment_method: { card: elements.getElement(CardElement), billing_details: { name: 'Jenny Rosen', }, }, setup_future_usage: 'off_session', }); if (result.error) { // Show error to your customer console.log(result.error.message); } else { if (result.paymentIntent.status === 'succeeded') { // Show a success message to your customer // There's a risk of the customer closing the window // before callback execution. Set up a webhook or plugin // to listen for the payment_intent.succeeded event // to save the card to a Customer // The PaymentMethod ID can be found on result.paymentIntent.payment_method } } }; render() { return ( <form onSubmit={this.handleSubmit}> <CardSection /> <button disabled={!this.props.stripe}>Confirm order</button> </form> ); } } export default function InjectedCheckoutForm() { return ( <ElementsConsumer> {({stripe, elements}) => ( <CheckoutForm stripe={stripe} elements={elements} /> )} </ElementsConsumer> ); }

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

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

    5 Save the card Server-side

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

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

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

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

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

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

    6 Charge the saved card later Server-side

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

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

    curl https://api.stripe.com/v1/payment_methods \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer="{{CUSTOMER_ID}}" \ -d type=card \ -G
    # Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 paymentMethods = await stripe.paymentMethods.list({ customer: '{{CUSTOMER_ID}}', type: 'card', }); })();
    // Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 switch 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 switch 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); const paymentIntentRetrieved = await stripe.paymentIntents.retrieve(err.raw.payment_intent.id); console.log('PI retrieved: ', paymentIntentRetrieved.id); } })();
    // Set your secret key. Remember to switch 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: stripe.Bool(true), OffSession: stripe.Bool(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) paymentIntentID := stripeErr.PaymentIntent.ID paymentIntent, _ := paymentintent.Get(paymentIntentID, nil) fmt.Printf("PI: %v", paymentIntent.ID) } }
    // Set your secret key. Remember to switch 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", Customer = "{{CUSTOMER_ID}}", PaymentMethod = "{{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 service = new PaymentIntentService(); var paymentIntent = service.Get(paymentIntentId); Console.WriteLine(paymentIntent.Id); break; default: break; } }

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

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

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

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

    7 Test the integration

    By this point you should have an integration that:

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

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

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

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

    Overview

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

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

    1 Accept a payment Client-side Server-side

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

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

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

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

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

    2 Save the card Server-side

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

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

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

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

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

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

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

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

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

    3 Charge the card off-session Server-side

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

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

    curl https://api.stripe.com/v1/payment_methods \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer="{{CUSTOMER_ID}}" \ -d type=card \ -G
    # Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 paymentMethods = await stripe.paymentMethods.list({ customer: '{{CUSTOMER_ID}}', type: 'card', }); })();
    // Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 switch 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 switch 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); const paymentIntentRetrieved = await stripe.paymentIntents.retrieve(err.raw.payment_intent.id); console.log('PI retrieved: ', paymentIntentRetrieved.id); } })();
    // Set your secret key. Remember to switch 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: stripe.Bool(true), OffSession: stripe.Bool(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) paymentIntentID := stripeErr.PaymentIntent.ID paymentIntent, _ := paymentintent.Get(paymentIntentID, nil) fmt.Printf("PI: %v", paymentIntent.ID) } }
    // Set your secret key. Remember to switch 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", Customer = "{{CUSTOMER_ID}}", PaymentMethod = "{{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 service = new PaymentIntentService(); var paymentIntent = service.Get(paymentIntentId); Console.WriteLine(paymentIntent.Id); break; default: break; } }

    4 Handle declines or authentication requests

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

    Start a recovery flow

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

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

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

    Let your customer try again

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

    If the payment failed because it requires authentication, try again with the existing PaymentMethod instead of creating a new one.

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

    5 Test the integration

    By this point you should have an integration that:

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

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

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

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

    Overview

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

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

    1 Accept a Payment Client-side Server-side

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

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

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

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

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

    2 Save the card Server-side

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

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

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

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

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

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

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

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

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

    3 Charge the card off-session Server-side

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

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

    curl https://api.stripe.com/v1/payment_methods \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer="{{CUSTOMER_ID}}" \ -d type=card \ -G
    # Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 paymentMethods = await stripe.paymentMethods.list({ customer: '{{CUSTOMER_ID}}', type: 'card', }); })();
    // Set your secret key. Remember to switch 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 switch 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 switch 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 switch 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 switch 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 switch 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 switch 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); const paymentIntentRetrieved = await stripe.paymentIntents.retrieve(err.raw.payment_intent.id); console.log('PI retrieved: ', paymentIntentRetrieved.id); } })();
    // Set your secret key. Remember to switch 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: stripe.Bool(true), OffSession: stripe.Bool(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) paymentIntentID := stripeErr.PaymentIntent.ID paymentIntent, _ := paymentintent.Get(paymentIntentID, nil) fmt.Printf("PI: %v", paymentIntent.ID) } }
    // Set your secret key. Remember to switch 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", Customer = "{{CUSTOMER_ID}}", PaymentMethod = "{{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 service = new PaymentIntentService(); var paymentIntent = service.Get(paymentIntentId); Console.WriteLine(paymentIntent.Id); break; default: break; } }

    4 Handle declines or authentication requests

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

    Start a recovery flow

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

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

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

    Let your customer try again

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

    If the payment failed because it requires authentication, try again with the existing PaymentMethod instead of creating a new one.

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

    5 Test the integration

    By this point you should have an integration that:

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

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

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

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

    See also

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

    Was this page helpful?

    Feedback about this page?

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

    On this page