Billing Worker
The billing worker handles Stripe subscription management for RedeCasas.
API Endpoints
| Method | Path | Auth | Description |
|---|---|---|---|
POST | /api/billing/checkout | ✅ | Create Stripe Checkout Session |
POST | /api/billing/portal | ✅ | Open Stripe Customer Portal |
GET | /api/billing/status | ✅ | Return subscription status |
POST | /api/billing/webhook | — | Stripe webhook handler |
All billing endpoints (except webhook) require auth via session cookie.
POST /api/billing/checkout
Create a Stripe checkout session for subscription.
Response 200 OK
{ "url": "https://checkout.stripe.com/..."}- Creates a Stripe customer if the user doesn’t have
stripe_customer_id - Stores
stripe_customer_idon the user record
POST /api/billing/portal
Open the Stripe customer portal.
Response 200 OK
{ "url": "https://billing.stripe.com/..."}- Returns
404if user has nostripe_customer_id
GET /api/billing/status
Check current subscription status.
Response 200 OK
{ "subscribed": true, "status": "active", "periodEnd": "2026-07-01T00:00:00.000Z"}POST /api/billing/webhook
Stripe webhook handler. No auth required (validated by Stripe signature).
Headers
Stripe-Signature— Stripe signature for verification
Handled Events
| Event | Action |
|---|---|
checkout.session.completed | Set subscription_status = 'active', store subscription_period_end |
invoice.payment_succeeded | Extend subscription_period_end |
invoice.payment_failed | Set subscription_status = 'past_due' |
customer.subscription.deleted | Set subscription_status = 'inactive' |
Response 200 OK
{ "received": true}Subscription Status Values
| Status | Meaning |
|---|---|
active | Subscription is current |
inactive | No active subscription |
past_due | Payment failed, grace period |
cancelled | User cancelled |
Environment Variables
Set via Wrangler secrets:
wrangler secret put STRIPE_SECRET_KEYwrangler secret put STRIPE_WEBHOOK_SECRETwrangler secret put STRIPE_PRICE_IDFor local development, add them to .dev.vars.
Flow
- User clicks “Subscribe” on the frontend
- Frontend calls
POST /api/billing/checkout - Worker creates a Stripe Checkout Session and returns the URL
- User completes payment on Stripe
- Stripe sends webhook to
POST /api/billing/webhook - Worker updates user’s subscription status in D1
- User can manage subscription via
POST /api/billing/portal
Development
cd billing-workernpm run devDeployment
cd billing-worker && npm run deploy