This guide explains how to set up and use the Stripe subscription integration in your Growdoro app.
- A Stripe account (create one at https://stripe.com)
- Your Stripe API keys (found in the Stripe Dashboard)
Add these to your .env.local file:
# Stripe API Keys
STRIPE_SECRET_KEY=sk_test_... # Your Stripe secret key
STRIPE_WEBHOOK_SECRET=whsec_... # Your webhook endpoint secret (see below)
# App URL (for redirects)
NEXT_PUBLIC_APP_URL=http://localhost:3000 # Change to your production URL- Go to the Stripe Dashboard → Developers → Webhooks
- Click "Add endpoint"
- Set the endpoint URL to:
https://yourdomain.com/api/stripe/webhook - Select the following events:
customer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_succeededinvoice.payment_failed
- Copy the signing secret and add it as
STRIPE_WEBHOOK_SECRETin your.env.local
- In Stripe Dashboard → Products, create a new product
- Add pricing (recurring for subscriptions)
- Copy the Price ID (starts with
price_)
This endpoint handles the return from Stripe Checkout:
- Retrieves the checkout session
- Creates/updates user profile with subscription details
- Redirects to success/error/pending pages
This endpoint handles Stripe events:
- Updates subscription status in real-time
- Handles cancellations and payment failures
- Maintains subscription state in your database
Use the example endpoint to create payment links:
// In your frontend code
const initiateSubscription = async () => {
const response = await fetch('/api/stripe/create-payment-link', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
priceId: 'price_xxxxxxxxxxxxx', // Your Stripe Price ID
email: '[email protected]' // Optional pre-fill
}),
});
const { url } = await response.json();
window.location.href = url; // Redirect to Stripe Checkout
};The integration uses these InstantDB entities:
$users: Stores user emailprofiles: Stores subscription detailssupporter: Boolean indicating active subscriptionsupporterSince: Timestamp when subscription startedsupporterUntil: Timestamp when current period endsstripeCustomerId: Stripe customer IDstripeDetails: JSON object with full subscription details
- Use Stripe test mode (keys starting with
sk_test_) - Test card numbers: https://stripe.com/docs/testing
- Common test card:
4242 4242 4242 4242
- Replace test API keys with live keys
- Update
NEXT_PUBLIC_APP_URLto production URL - Set up production webhook endpoint
- Test the full flow in production
- Monitor webhook logs in Stripe Dashboard
- Ensure
STRIPE_WEBHOOK_SECRETmatches the webhook endpoint secret - Check that the raw request body is being used for signature verification
- Check webhook logs in Stripe Dashboard
- Verify database queries are working correctly
- Ensure webhook events are being received
- Verify Price ID is correct
- Check that product is active in Stripe
- Ensure API keys have necessary permissions