Best practices for Stripe API integrations
This skill provides guidance for building modern Stripe integrations, avoiding deprecated APIs, and following recommended patterns.
CheckoutSessions API (Recommended)
PaymentIntents API
Charges API - Do not use
Card Element - Legacy
Stripe-hosted Checkout (Simplest)
// Redirect to Stripe-hosted checkout
const session = await stripe.checkout.sessions.create({
mode: 'payment',
line_items: [{ price: 'price_xxx', quantity: 1 }],
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
});
// Redirect customer to session.url
Embedded Checkout (Branded)
// Embed checkout in your site
const checkout = await stripe.initEmbeddedCheckout({
clientSecret: session.client_secret,
});
checkout.mount('#checkout');
Payment Element (Custom UI)
// For advanced customization needs
const elements = stripe.elements({ clientSecret });
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
Enable payment methods through the Stripe Dashboard rather than hardcoding:
Use the SetupIntents API for saving payment methods:
const setupIntent = await stripe.setupIntents.create({
customer: 'cus_xxx',
payment_method_types: ['card'],
});
Use Stripe Billing with Checkout:
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
line_items: [{ price: 'price_monthly_xxx', quantity: 1 }],
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
});
For platforms connecting multiple parties:
Direct Charges
application_fee_amountDestination Charges
Always use on_behalf_of parameter for proper liability shift:
const paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: 'usd',
on_behalf_of: 'acct_connected_xxx',
transfer_data: { destination: 'acct_connected_xxx' },
});
Before going live: