Cursor prompt

Add Stripe Payment Integration

Complete Stripe checkout, webhooks, subscription management, and billing portal with idempotent webhook handling.

Integrate Stripe payments into {{project_name}}.

## Billing Model: {{billing_model}}
## Price IDs: {{price_ids}}

### Checkout Flow
1. Create Stripe customer on user signup → store stripeCustomerId in DB
2. POST /api/stripe/checkout → create Checkout Session with metadata: { userId, planId }
3. Set success_url and cancel_url with session_id for verification
4. Success handler: verify session, activate subscription in DB

### Webhook Handler: POST /api/stripe/webhook
Verify HMAC signature with stripe.webhooks.constructEvent() first.
Handle these events idempotently (check processed event ID before handling):
- checkout.session.completed → activate subscription, store subscriptionId
- customer.subscription.updated → sync plan tier and period end
- customer.subscription.deleted → downgrade user to free tier
- invoice.payment_failed → mark past_due, trigger dunning email sequence
- invoice.payment_succeeded → extend access period, clear past_due flag
Return 200 for ALL events including unhandled ones.

### Billing Portal
POST /api/stripe/portal → create portal session → redirect to Stripe-hosted portal.
Users can manage their own plan changes and cancellations.

### Database Fields to Add
stripeCustomerId, subscriptionId, subscriptionStatus, currentPeriodEnd, planId

### Security
- Webhook signature verification is mandatory — reject unsigned requests
- Never log full card data — Stripe handles PCI compliance
- Store only Stripe IDs, never raw payment method data

### Output
All API routes, webhook handler, DB migration, and environment variable list.

Fill in: {{project_name}}, {{billing_model}}, {{price_ids}}. In Loopy you fill these in a form and copy the finished prompt in one click.