← back to Lawyer Directory Builder

src/scripts/setup_stripe_tier_webhook.ts

58 lines

/**
 * One-shot setup: create the Stripe webhook endpoint for the Lawyer Pro
 * subscription tier (handled by src/server/billing.ts at /webhooks/stripe-tier)
 * and print the signing secret.
 *
 * This is a SEPARATE Stripe endpoint from the original $99 EZ Upgrade webhook
 * (which lives at /webhooks/stripe and is set up by setup_stripe_webhook.ts).
 * Each Stripe endpoint has its own signing secret — sharing one secret would
 * cause one of the two webhook handlers to silently reject every event.
 *
 * Idempotent: if a webhook for /webhooks/stripe-tier already exists, we delete
 * it first so the new endpoint comes back with a fresh signing secret.
 *
 *   npx tsx src/scripts/setup_stripe_tier_webhook.ts
 */
import 'dotenv/config';
import Stripe from 'stripe';

const URL = 'https://lawyers.agentabrams.com/webhooks/stripe-tier';
const EVENTS: Stripe.WebhookEndpointCreateParams.EnabledEvent[] = [
  'customer.subscription.created',
  'customer.subscription.updated',
  'customer.subscription.deleted',
  'invoice.payment_failed',
];

const SK = process.env.STRIPE_SECRET_KEY;
if (!SK || !SK.startsWith('sk_')) {
  console.error('STRIPE_SECRET_KEY missing or invalid in .env');
  process.exit(2);
}
const stripe = new Stripe(SK);

(async () => {
  // Find any existing endpoint at this URL — delete to force fresh secret
  const existing = await stripe.webhookEndpoints.list({ limit: 100 });
  for (const e of existing.data) {
    if (e.url === URL) {
      console.log(`found existing tier webhook ${e.id} — deleting to refresh signing secret`);
      await stripe.webhookEndpoints.del(e.id);
    }
  }

  const created = await stripe.webhookEndpoints.create({
    url: URL,
    enabled_events: EVENTS,
    description: 'Counsel & Bar — Lawyer Pro $29/mo subscription tier (lawyers.agentabrams.com/webhooks/stripe-tier)',
  });

  console.log('\n=== tier webhook endpoint created ===');
  console.log('id:           ', created.id);
  console.log('url:          ', created.url);
  console.log('events:       ', created.enabled_events.length, '·', created.enabled_events.join(', '));
  console.log('signing secret:', created.secret);
  console.log('\nNext step (run from your shell):');
  console.log(`  node ~/Projects/secrets-manager/cli.js add STRIPE_TIER_WEBHOOK_SECRET '${created.secret}'\n`);
})().catch(e => { console.error('setup failed:', e); process.exit(1); });