#!/usr/bin/env node 'use strict'; /** * Manually trigger subscription_schedule.completed webhook event * This simulates what happens when a promo schedule completes * * Usage: * node tests/test_trigger_promo_webhook.js sub_sched_xxx */ const path = require('path'); // Load environment const envPath = path.resolve(process.cwd(), './environment.env'); require('dotenv').config({ path: envPath }); const axios = require('axios'); const scheduleId = process.argv[2]; if (!scheduleId || !scheduleId.startsWith('sub_sched_')) { console.error('Usage: node tests/test_trigger_promo_webhook.js sub_sched_xxx'); process.exit(1); } async function triggerWebhook() { console.log('\nšŸ”” Triggering webhook event for schedule:', scheduleId); console.log('════════════════════════════════════════════════════════\n'); try { // Use Stripe CLI to trigger the event const { spawn } = require('child_process'); console.log('Using Stripe CLI to trigger event...\n'); const stripe = spawn('stripe', [ 'trigger', 'subscription_schedule.completed', '--add', `subscription_schedule:id=${scheduleId}` ]); stripe.stdout.on('data', (data) => { console.log(data.toString()); }); stripe.stderr.on('data', (data) => { console.error(data.toString()); }); stripe.on('close', (code) => { if (code === 0) { console.log('\nāœ… Webhook event triggered successfully!'); console.log('\nšŸ“‹ Check your server logs for:'); console.log(' - "Subscription schedule completed: ..."'); console.log(' - "Promo expired email sent to ..."'); console.log('\nšŸ“§ Check email inbox or test-logs/promo-expired-preview.html\n'); } else { console.error(`\nāŒ Stripe CLI exited with code ${code}`); console.error('\nMake sure:'); console.error(' 1. Stripe CLI is installed: https://stripe.com/docs/stripe-cli'); console.error(' 2. You are logged in: stripe login'); console.error(' 3. Your webhook endpoint is listening\n'); } process.exit(code); }); } catch (error) { console.error('\nāŒ Error triggering webhook:', error.message); console.error('\nTrying alternative method: forwarding to local webhook endpoint...\n'); // Alternative: Send POST request to local webhook endpoint const webhookUrl = `http://localhost:${process.env.AGM_PORT || 4100}/api/subscription/webhooks`; console.log(`Sending to: ${webhookUrl}`); // This won't work without proper Stripe signature, but shows the concept console.log('\n⚠ This requires Stripe webhook signature verification.'); console.log('Use Stripe CLI instead:'); console.log(`\n stripe trigger subscription_schedule.completed --add subscription_schedule:id=${scheduleId}`); console.log('\nOr forward webhooks:'); console.log(` stripe listen --forward-to localhost:${process.env.AGM_PORT || 4100}/api/subscription/webhooks\n`); process.exit(1); } } triggerWebhook();