← back to Agent Ad Network

scripts/stripe-test-setup.js

38 lines

#!/usr/bin/env node
// One-shot Stripe TEST-mode setup for agent-ad-network. Run AFTER pasting
// STRIPE_TEST_KEY=sk_test_… into the environment (or .env sourced by pm2).
//   node scripts/stripe-test-setup.js
// Creates: (1) the top-up product + a $25 reference price, (2) the webhook
// endpoint at $PUBLIC_URL/api/billing/webhook, then prints the whsec_ signing
// secret to add as STRIPE_WEBHOOK_SECRET. Refuses anything but sk_test_ keys.
'use strict';
const KEY = process.env.STRIPE_TEST_KEY || '';
const PUBLIC_URL = process.env.PUBLIC_URL || 'https://ads.agentabrams.com';
if (!KEY.startsWith('sk_test_')) { console.error('STRIPE_TEST_KEY must be a sk_test_ key — refusing.'); process.exit(1); }

const api = (method, path, params) => new Promise((resolve, reject) => {
  const body = params ? new URLSearchParams(params).toString() : '';
  const r = require('https').request({
    hostname: 'api.stripe.com', path, method,
    headers: { Authorization: `Bearer ${KEY}`, 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(body) }
  }, res => { let d = ''; res.on('data', c => d += c); res.on('end', () => {
    try { const j = JSON.parse(d); j.error ? reject(new Error(j.error.message)) : resolve(j); } catch (e) { reject(e); }
  }); });
  r.on('error', reject); r.end(body);
});

(async () => {
  const product = await api('POST', '/v1/products', { name: 'Agent Ad Network — budget top-up (TEST)' });
  console.log('product:', product.id);
  const price = await api('POST', '/v1/prices', {
    product: product.id, currency: 'usd', unit_amount: '2500', nickname: 'reference $25 top-up'
  });
  console.log('price:', price.id);
  const hook = await api('POST', '/v1/webhook_endpoints', {
    url: `${PUBLIC_URL}/api/billing/webhook`, 'enabled_events[0]': 'checkout.session.completed'
  });
  console.log('webhook endpoint:', hook.id);
  console.log('\nAdd to env and restart pm2 with --update-env:');
  console.log(`  STRIPE_WEBHOOK_SECRET=${hook.secret}`);
})().catch(e => { console.error('setup failed:', e.message); process.exit(1); });