Payment Integration Guide
Learn how to integrate payment processing for handling payments and subscriptions in your SuperFast application.
About Payment Processing
Stripe Setup
Follow these steps to set up Stripe for your SuperFast application:
1. Create a Stripe Account
First, you need to create a Stripe account:
- Sign up for a Stripe account
- Complete your business profile
- Verify your email address
2. Get Your API Keys
Obtain your Stripe API keys:
- Go to the [Developers] section in your Stripe dashboard
- Click on [API keys]
- You'll find both [Publishable key] and [Secret key]
- [Important]: Keep your secret key secure and never expose it in client-side code
3. Configure Environment Variables
Add the following environment variables to your .env.local
file:
Important
Make sure to replace the placeholder values with your actual Stripe credentials:
# Stripe API KeysSTRIPE_SECRET_KEY=sk_test_your_secret_key_hereNEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here# Stripe Webhook Secret (for handling webhooks)STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
4. Implement Stripe Integration
Create the necessary files to handle Stripe payments and subscriptions:
- Stripe Utility: Create
lib/stripe.ts
to initialize Stripe and handle client-side operations - Checkout API: Create
app/api/stripe/create-checkout/route.ts
to generate checkout sessions - Success Page: Create
app/payment/success/page.tsx
to handle successful payments - Verification API: Create
app/api/stripe/verify-payment/route.ts
to verify payment status - Webhook Handler: Create
app/api/webhook/stripe/route.ts
to process Stripe events
These components work together to create a complete payment flow:
- User initiates payment → Checkout API creates session
- User completes payment on Stripe → Redirected to success page
- Success page verifies payment → Updates user subscription
- Webhook handler processes events → Updates database asynchronously
5. Configure Webhook Endpoint
Set up your webhook endpoint in the Stripe dashboard:
- Go to the [Developers] section in your Stripe dashboard
- Click on [Webhooks]
- Click [Add endpoint]
- Enter your webhook URL:
https://your-domain.com/api/webhook/stripe
- Select the following events to listen to:
checkout.session.completed
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
invoice.paid
invoice.payment_failed
- Copy the [Signing secret] and add it to your environment variables as
STRIPE_WEBHOOK_SECRET
6. Switch Payment Modes
Implement a feature to allow users to switch between one-time payment and subscription modes with a single click:
Payment Mode Switching
For detailed implementation of switching between one-time payment and subscription modes, refer to:
Stripe Integration Complete
- One-time payments and subscriptions
- Checkout session creation and management
- Payment verification and success handling
- Comprehensive webhook handling for subscription lifecycle events
- Error handling and retry logic