The recommended way to take subscription payments with Stripe in Spring Boot is Stripe Checkout in subscription mode. Your server creates a Checkout Session for a recurring Price, redirects the customer to Stripe's hosted page, and then updates your own records from webhook events such as checkout.session.completed and invoice.paid. The webhook, not the browser redirect, is the source of truth.
This approach keeps card data off your servers, which limits your PCI scope, and Stripe handles renewals, retries, taxes and receipts. The code below uses the official stripe-java library with Spring Boot 3 and Java 17 or newer.
Stripe splits a subscription into a few objects, and knowing them avoids most confusion.
| Object | Meaning |
|---|---|
| Product | What you sell, such as "Pro plan" |
| Price | An amount and billing interval attached to a product, for example monthly |
| Customer | Your user in Stripe |
| Checkout Session | A one-time hosted payment page that creates the subscription |
| Subscription | The recurring agreement, with a status such as active or past_due |
| Invoice | A bill generated for each billing period |
Create Products and Prices once in the Stripe Dashboard (or through the API) and store the Price ID, which looks like price_..., in configuration. Older Plan objects still work but Prices are the current model.
Add the Stripe Java library. Use the current release from Maven Central rather than a copied version number.
Keep secrets out of source control and out of application.properties. Read them from environment variables:
Use a test-mode key (starting with sk_test_) during development. Never commit a real key, and rotate any key that has been pasted into a repository or chat.
The endpoint below starts the flow. It creates a Checkout Session in subscription mode and returns the URL the browser should open.
setClientReferenceId ties the session to your own user so the webhook can find them. In real code, take the user from the authenticated principal instead of a request parameter, so a caller cannot start a checkout for someone else. Pass an existing Stripe customer with setCustomer when the user already has one, to avoid duplicates.
After payment, Stripe sends events to your endpoint. Verify the signature against the raw request body before trusting anything.
Take the body as a String so it stays byte-for-byte identical, because re-serialized JSON breaks the signature. Allow the /webhooks/stripe path without CSRF protection and without login, since Stripe calls it, and rely on the signature instead. Stripe can deliver an event more than once, so make handlers idempotent by storing processed event IDs.
For local testing, the Stripe CLI forwards events with stripe listen --forward-to localhost:8080/webhooks/stripe and prints the signing secret to use.
Do not build cancel and card-update screens yourself. The Customer Portal is a hosted page for these tasks.
Enable and configure the portal once in the Stripe Dashboard. Redirect the user to portalUrl; changes they make there arrive as webhook events.
No. A user can open that URL without paying. Grant access only after the verified webhook arrives, or after you retrieve the session from Stripe on your server.
Set trial days on the subscription data in the Checkout Session parameters, or configure the trial on the Price. Stripe sends customer.subscription.trial_will_end three days before it ends.
Stripe retries according to your Dashboard settings and emits invoice.payment_failed. The subscription becomes past_due and finally cancels or goes unpaid if all retries fail.
No. Stripe stores them. You keep only Stripe IDs for the customer and subscription plus your own plan status.
Create Products and Prices in Stripe, start a Checkout Session in subscription mode from Spring Boot, and drive your application state from signature-verified webhooks. Keep API keys in environment variables, make webhook handling idempotent, and send customers to the Customer Portal for self-service changes.