Stripe | Financial Infrastructure to Grow Your Revenue

Stripe | Financial Infrastructure to Grow Your Revenue

4466 articles

Save payment details during a TWINT payment


Save payment details during a the related setting payment

Checkout

Elements

Direct API

You can use Stripe-hosted Checkout to collect payment method details and charge the saved payment method immediately.

Alternatively, you can set up future the related setting payments to collect the related setting payment method details in advance, and determine the final amount or payment date later.

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:

Command Line

Select a language

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

# Available as a gem
sudo gem install stripe

Gemfile

Select a language

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

# If you use bundler, you can add this line to your Gemfile
gem 'stripe'

Create or retrieve a customer Server-side

To reuse a the related setting payment method for future payments, attach it to an object that represents your customer.

Use the Accounts v2 API to represent customers

The Accounts v2 API is generally available for Connect users, and in public preview for other Stripe users. If you’re part of the Accounts v2 preview, you need to specify a preview version in your code.

To join the Accounts v2 preview, go to Account previews and features in your Dashboard and enable Reusable payment methods for Global Payouts.

For most use cases, we recommend modeling your customers as customer-configured Account objects instead of using Customer objects.

Create a customer-configured Account or Customer when your customer creates an account with your business, or when saving a payment method. Associate the object’s ID with your own internal representation of a customer.

Create a new customer or retrieve an existing one to associate with this payment.

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

Create a Checkout Session Server-side

Your customer must authorize you to use their the related setting account for future payments through Stripe Checkout. This allows you to accept the related setting payments. Add a checkout button to your website that calls a server-side endpoint to create a Checkout Session.

index.html

<html>
 <head>
 <title>Checkout</title>
 </head>
 <body>
 <form action="/create-checkout-session" method="POST">
 <button type="submit">Checkout</button>
 </form>
 </body>
</html>

A Checkout Session is the programmatic representation of what your customer sees when they’re redirected to the payment form.

  1. Create a Checkout Session in payment mode .
  2. Enable the related setting in your Dashboard. Stripe automatically displays the related setting to eligible customers using dynamic payment methods . If you currently specify payment _ method _ types , see the migration guide .
  3. List line_items to charge. Make sure all line _ items use the chf currency.
  4. Specify the customer’s ID.
  5. Configure payment_intent_data.setup_future_usage to indicate that you want to set up the payment method for future usage.

After creating the Checkout Session, redirect your customer to the URL returned in the response.

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

When your customer provides their payment method details, Stripe redirects them to the success_url, a page on your website that informs them that their payment was successful. Make the Session ID available on your success page by including the {the related setting} template variable in the success_url as in the above example.

Caution

Don’t rely on the redirect to the success_url alone for detecting payment initiation, because:

  • Malicious users could directly access the success _ url without paying and gain access to your goods or services.
  • After a successful payment, customers might close their browser tab before they’re redirected to the success _ url .

Retrieve the payment method Server-side

After a customer submits their payment details, retrieve the PaymentMethod object. A PaymentMethod stores customer the related setting account information for future payments. You can retrieve the PaymentMethod synchronously using the success_url or asynchronously using webhooks.

Determine whether to retrieve the PaymentMethod synchronously or asynchronously, based on your dropff tolerance. Customers might not always reach the success_url after a successful payment, for example, if they close their browser tab before the redirect occurs. You can use webhooks to prevent your integration from experiencing this type of dropoff.

Handle checkout.session.completed webhooks, which contain a Session object. Learn more about setting up webhooks. The following example is a checkout.session.completed response.

{
 "id": "evt_1Ep24XHssDVaQm2PpwS19Yt0",
 "object": "event",
 "api_version": "2019-03-14",
 "created": 1561420781,
 "data": {
 "object": {
 "id": "cs_test_MlZAaTXUMHjWZ7DcXjusJnDU4MxPalbtL5eYrmS2GKxqscDtpJq8QM0k",
 "object": "checkout.session",
 "billing_address_collection": null,
 "client_reference_id": null,
 "customer": null,
 "customer_email": null,
 "display_items": [],
 "mode": "payment",
 "payment_intent": "pi_3MtwBwLkdIwHu7ix28a3tqPa",
 "submit_type": null,
 "subscription": null,
 "success_url": "https://example.com/success"
 }
 },
 "livemode": false,
 "pending_webhooks": 1,
 "request": {
 "id": null,
 "idempotency_key": null
 },
 "type": "checkout.session.completed"
}

Note the value of the payment_intent key, which is the ID for the PaymentIntent created with the Checkout Session. A PaymentIntent is an object that represents your intent to collect a payment from a customer and tracks the payment process. Retrieve the PaymentIntent object with the ID. The returned object contains a payment_method ID that you can charge future payments with.

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

Monitor webhooks Server-side

Use a method such as webhooks to confirm that the customer authorized the mandate. Don’t rely on your customer to return to the payment status page.

When a customer successfully authorizes the mandate, Stripe emits a payment_intent.succeeded webhook event and the PaymentIntent’s status transitions to succeeded. Store the resulting payment_method ID to make payments using the saved payment method later.

If a customer doesn’t successfully authorize the mandate, Stripe emits a payment_intent.payment_failed webhook event and the PaymentIntent’s status updates to requires_payment_method.

When you’re ready to charge your customer, use the customer’s ID and the PaymentMethod ID to create a PaymentIntent.

To find a twint instrument to charge, list the payment methods associated with your customer.

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

When you have the customer and PaymentMethod IDs, create and confirm a PaymentIntent with the amount and currency of the payment. To charge your customer off session, set off_session to true. That indicates that the customer isn’t in your checkout flow during this payment attempt.

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

Note

The return_url parameter isn’t required when using a the related setting payment method that was previously set up with a SetupIntent or a PaymentIntent with setup_future_usage. It’s required for all other cases.

Test your integration

When testing your payment integration with your test API keys, select the related setting as the payment method and click Pay. If you successfully complete authorization on the redirect page, the PaymentIntent transitions from requires_action to succeeded. If you decline the authorization on this page, the PaymentIntent transitions from requires_action to requires_payment_method.

See also

Last verified 2026-09-24

Is this helpful?