Finalise payments on the server
Build an integration where you render the Payment Element before you create a PaymentIntent or SetupIntent, then confirm the Intent from your server.
Subscriptions is a pricing model where users make recurring payments to use a product. In this integration guide, build a custom payment flow where you render the Payment Element, create a Subscription, and confirm the Subscription from your server.
Set up StripeServer-side
First, create a Stripe account or sign in.
Use our official libraries to access the Stripe API from your application:
Enable payment methods
Caution
This integration path doesn’t support BLIK or pre-authorised debits that use the Automated Clearing Settlement System (ACSS).
View your payment methods settings and enable the payment methods you want to support. You need at least one payment method enabled to create a PaymentIntent.
By default, Stripe enables cards and other prevalent payment methods that can help you reach more customers, but we recommend turning on additional payment methods that are relevant for your business and customers. See Payment method support for product and payment method support, and our pricing page for fees.
For Subscriptions, configure your invoice settings and supported payment methods. To prevent mismatches and errors, your invoice settings must match your Payment Element settings.
Collect payment detailsClient-side
You’re ready to collect payment details on the client with the Payment Element. The Payment Element is a pre-built UI component that simplifies collecting payment details for a variety of payment methods.
The Payment Element contains an iframe that securely sends payment information to Stripe over an HTTPS connection. Avoid placing the Payment Element within another iframe because some payment methods require redirecting to another page for payment confirmation.
The checkout page address must start with https://
rather than http://
for your integration to work. You can test your integration without using HTTPS, but remember to enable it when you’re ready to accept live payments.
The Payment Element renders a dynamic form that allows your customer to pick a payment method. The form automatically collects all necessary payments details for the payment method selected by the customer.
You can customise the Payment Element to match the design of your site by passing the appearance object into options
when creating the Elements
provider.
Collect addresses
By default, the Payment Element only collects the necessary billing address details. To collect a customer’s full billing address (to calculate the tax for digital goods and services, for example) or shipping address, use the Address Element.
Create the pricing modelStripe CLI or Dashboard
Create your products and their prices in the Dashboard or with the Stripe CLI.
This example uses a fixed-price service with two different service-level options: Basic and Premium. For each service-level option, you need to create a product and a recurring price. (If you want to add a one-off charge for something like a setup fee, create a third product with a one-off price. To keep things simple, this example doesn’t include a one-off charge.)
In this example, each product bills at monthly intervals. The price for the Basic product is 5 USD. The price for the Premium product is 15 USD.
Create the customerClient and Server
Stripe needs a customer for each subscription. In your application’s frontend, collect any necessary user information and pass it to the back-end.
If you need to collect address details, the Address Element enables you to collect a shipping or billing address for your customers. To learn more, see Address Element.
<form id="signup-form"> <label> Email <input id="email" type="text" placeholder="Email address" value="test@example.com" required /> </label> <button type="submit"> Register </button> </form>
const emailInput = document.querySelector('#email'); fetch('/create-customer', { method: 'post', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: emailInput.value, }), }).then(r => r.json());
On the server, create the Stripe customer object.
Create the ConfirmationTokenClient-side
Use createPaymentMethod through a legacy implementation
If you’re using a legacy implementation, you might be using the information from stripe.
to finalise payments on the server. While we encourage you to follow this guide to Migrate to Confirmation Tokens you can still access our old documentation to Finalise payments on the server
When the customer submits your payment form, call stripe.createConfirmationToken to create a ConfirmationToken to send to your server for additional validation or business logic before payment confirmation.
Confirming the PaymentIntent generates a PaymentMethod. You can read the payment_method ID off the PaymentIntent confirmation response.
Caution
You must immediately use the created ConfirmationToken to confirm a PaymentIntent; if unused, it expires after 12 hours.
Create and submit the subscription to StripeServer-side
After the customer submits your payment form, use a Subscription to facilitate the confirmation and payment process. On the server, use the customer ID (from a cookie or request parameter), the price ID, and the payment method ID to create and confirm the subscription.
const stripe = require("stripe")(
); const express = require('express'); const cookieParser = require('cookieParser'); const app = express(); app.set('trust proxy', true); app.use(express.json()); app.use(express.static(".")); app.use(cookieParser()); app.post('/create-confirm-subscription', async (req, res) => { const customerId = req.cookies['customer']; const confirmationTokenId = req.body.confirmationTokenId; // The ID of the Price that you created previously const priceId = '{{ PRICE_ID }}'; try { const subscription = await stripe.subscriptions.create({ customer: customerId, items: [{ price: priceId, }], payment_behavior: 'default_incomplete', payment_settings: { save_default_payment_method: 'on_subscription' }, expand: ['latest_invoice.payments'] }); // Confirm intent with collected payment method const {status, clientSecret} = await stripe.paymentIntents.confirm( subscription.latest_invoice.payments.data[0].payment.payment_intent, { confirmation_token: confirmationTokenId, } ); res.json({status, clientSecret}); } catch (err) { res.json({ error: err }); } }); app.listen(3000, () => { console.log('Running on port 3000'); });"sk_test_BQokikJOvBiI2HlWgH4olfQ2"
Handle any next actionsClient-side
When the PaymentIntent requires additional action from the customer, such as authenticating with 3D Secure or redirecting to a different site, you need to trigger those actions. Use stripe.
to trigger the UI for handling customer action and completing the payment.
Manage the Subscription
To complete the integration, you might want to listen for webhooks, provision access to your service, and allow customers to cancel their subscriptions. For more details on how to do this, you can refer to the subscriptions integration guide.
Disclose Stripe to your customers
Stripe collects information on customer interactions with Elements to provide services to you, prevent fraud, and improve its services. This includes using cookies and IP addresses to identify which Elements a customer saw during a single checkout session. You’re responsible for disclosing and obtaining all rights and consents necessary for Stripe to use data in these ways. For more information, visit our privacy center.