Fileoverview

Stripe Billing Edge Function

Handles all Stripe billing operations including checkout, plan changes, subscription sync, auto-upgrade settings, and billing portal access.

Endpoint

POST /stripe-billing

Auth

Required - JWT in Authorization header

Remarks

Supported actions (via action field in request body):

  • checkout: Create Stripe Checkout session for new subscription
  • change-plan: Upgrade/downgrade existing subscription
  • preview-change: Preview proration for plan change
  • update-auto-upgrade: Toggle auto-upgrade setting
  • sync-subscription: Sync subscription state from Stripe
  • portal: Create Stripe Billing Portal session

Tables touched:

  • sgtm_containers (read)
  • sgtm_container_subscriptions (read/write)
  • billing_plans (read)
  • clients (read/write)
  • client_memberships (read via RLS)

External services:

  • Stripe API for all payment operations

Returns

200 - Action-specific success response

Returns

400 - Invalid action or missing parameters

Returns

401 - Missing authorization header

Returns

404 - Container or client not found

Returns

500 - Stripe or database error