-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-live-webhook.js
More file actions
192 lines (167 loc) · 6.39 KB
/
Copy pathtest-live-webhook.js
File metadata and controls
192 lines (167 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const dotenv = require("dotenv");
const path = require("path");
const fs = require("fs");
// Load environment variables
dotenv.config();
// Also load .env.local if it exists
const envLocalPath = path.resolve(process.cwd(), ".env.local");
if (fs.existsSync(envLocalPath)) {
const envLocal = dotenv.parse(fs.readFileSync(envLocalPath));
for (const k in envLocal) {
process.env[k] = envLocal[k];
}
}
// Handle ESM modules in CommonJS
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
async function testLiveWebhookSetup() {
console.log("=== LIVE MODE WEBHOOK VERIFICATION ===");
try {
// Check if we're using live keys
const isLiveMode = process.env.STRIPE_SECRET_KEY?.startsWith("sk_live_");
const isLivePubKey =
process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY?.startsWith("pk_live_");
console.log(
`✅ Stripe Secret Key: ${isLiveMode ? "LIVE MODE" : "TEST MODE"}`
);
console.log(
`✅ Stripe Publishable Key: ${isLivePubKey ? "LIVE MODE" : "TEST MODE"}`
);
console.log(
`✅ Webhook Secret configured: ${!!process.env.STRIPE_WEBHOOK_SECRET}`
);
if (!isLiveMode || !isLivePubKey) {
console.warn("⚠️ WARNING: Not all keys are in live mode!");
return;
}
// Initialize Stripe
const Stripe = (await import("stripe")).default;
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2023-08-16",
});
// Check webhook endpoints in live mode
console.log("\n=== CHECKING LIVE WEBHOOK ENDPOINTS ===");
const webhookEndpoints = await stripe.webhookEndpoints.list();
console.log(`Found ${webhookEndpoints.data.length} webhook endpoint(s):`);
webhookEndpoints.data.forEach((endpoint, i) => {
console.log(` ${i + 1}. URL: ${endpoint.url}`);
console.log(` Status: ${endpoint.status}`);
console.log(` Events: ${endpoint.enabled_events.join(", ")}`);
console.log(
` Secret: ${endpoint.secret ? "whsec_..." + endpoint.secret.slice(-10) : "Not available"}`
);
});
// Check if our webhook URL is configured
const expectedUrl = "https://www.vibe-list.com/api/webhook/stripe";
const ourWebhook = webhookEndpoints.data.find(
(endpoint) => endpoint.url === expectedUrl
);
if (ourWebhook) {
console.log(`\n✅ Found our webhook endpoint: ${expectedUrl}`);
console.log(` Status: ${ourWebhook.status}`);
console.log(` Events: ${ourWebhook.enabled_events.length} configured`);
// Check if checkout.session.completed is enabled
const hasCheckoutCompleted = ourWebhook.enabled_events.includes(
"checkout.session.completed"
);
console.log(
` Checkout completed event: ${hasCheckoutCompleted ? "✅ Enabled" : "❌ Missing"}`
);
} else {
console.log(`\n❌ Our webhook endpoint not found: ${expectedUrl}`);
console.log(" Available endpoints:");
webhookEndpoints.data.forEach((endpoint) => {
console.log(` - ${endpoint.url}`);
});
}
// Test webhook endpoint accessibility
console.log("\n=== TESTING WEBHOOK ENDPOINT ACCESSIBILITY ===");
try {
const response = await fetch(expectedUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": "Stripe/1.0 (+https://stripe.com/docs/webhooks)",
},
body: JSON.stringify({ test: "ping" }),
});
console.log(
`Webhook endpoint response: ${response.status} ${response.statusText}`
);
if (response.status === 400) {
console.log(
"✅ Webhook endpoint is accessible (400 expected for invalid signature)"
);
} else {
console.log(`⚠️ Unexpected response status: ${response.status}`);
}
} catch (error) {
console.error(`❌ Failed to reach webhook endpoint: ${error.message}`);
}
// Check Supabase connection
console.log("\n=== TESTING SUPABASE CONNECTION ===");
const { createClient } = await import("@supabase/supabase-js");
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
// Test database connection
const { data: profiles, error } = await supabase
.from("profiles")
.select("id, email, has_access, customer_id, price_id")
.limit(3);
if (error) {
console.error(`❌ Supabase connection failed: ${error.message}`);
} else {
console.log(`✅ Supabase connected - Found ${profiles.length} profiles`);
profiles.forEach((profile) => {
console.log(
` - ${profile.email}: access=${profile.has_access}, customer=${profile.customer_id || "none"}`
);
});
}
// Check plan configuration
console.log("\n=== CHECKING PLAN CONFIGURATION ===");
const config = (await import("./config.js")).default;
console.log(`Found ${config.stripe.plans.length} plan(s):`);
config.stripe.plans.forEach((plan, i) => {
console.log(` ${i + 1}. ${plan.name}: ${plan.priceId}`);
console.log(` Price: $${plan.price}`);
console.log(` Features: ${plan.features.length} items`);
});
// Verify price IDs exist in Stripe
console.log("\n=== VERIFYING STRIPE PRICES ===");
for (const plan of config.stripe.plans) {
try {
const price = await stripe.prices.retrieve(plan.priceId);
console.log(
`✅ ${plan.name} price exists: ${price.id} ($${price.unit_amount / 100})`
);
} catch (error) {
console.error(`❌ ${plan.name} price not found: ${plan.priceId}`);
}
}
console.log("\n=== SUMMARY ===");
console.log("✅ Live mode keys configured");
console.log(
ourWebhook
? "✅ Webhook endpoint configured"
: "❌ Webhook endpoint missing"
);
console.log("✅ Supabase connection working");
console.log("✅ Plan configuration loaded");
if (ourWebhook && isLiveMode) {
console.log("\n🎉 READY FOR LIVE PAYMENTS!");
console.log(
"When customers complete payments, the webhook should now update their profiles."
);
} else {
console.log("\n⚠️ Setup incomplete - webhook may not work properly");
}
} catch (error) {
console.error("❌ Test failed:", error.message);
console.error(error.stack);
}
}
// Run the test
testLiveWebhookSetup().catch(console.error);