-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-production-webhook.js
More file actions
147 lines (117 loc) · 4.54 KB
/
Copy pathtest-production-webhook.js
File metadata and controls
147 lines (117 loc) · 4.54 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
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];
}
}
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
async function testProductionWebhook() {
console.log("=== PRODUCTION WEBHOOK TEST ===");
try {
// Initialize Stripe to get a recent event
const Stripe = (await import("stripe")).default;
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2023-08-16",
});
console.log("\n=== FETCHING RECENT STRIPE EVENT ===");
// Get recent events from Stripe
const events = await stripe.events.list({
limit: 5,
types: ["checkout.session.completed"],
});
if (events.data.length === 0) {
console.log("❌ No recent checkout.session.completed events found");
return;
}
// Use the most recent event
const recentEvent = events.data[0];
console.log(`Using event: ${recentEvent.id}`);
const session = recentEvent.data.object;
console.log(`Metadata:`, session.metadata);
// Test if this event should be processed
const metadata = session.metadata || {};
const isVibeListPayment = metadata.source === "vibelist-app";
if (!isVibeListPayment) {
console.log("⚠️ This event would be skipped due to filtering");
return;
}
console.log("\n=== CREATING WEBHOOK PAYLOAD ===");
// Create the webhook payload exactly as Stripe would send it
const webhookPayload = JSON.stringify(recentEvent);
// Create a signature
const crypto = require("crypto");
const timestamp = Math.floor(Date.now() / 1000);
const payload = timestamp + "." + webhookPayload;
const signature = crypto
.createHmac("sha256", process.env.STRIPE_WEBHOOK_SECRET)
.update(payload, "utf8")
.digest("hex");
const stripeSignature = `t=${timestamp},v1=${signature}`;
console.log("\n=== CALLING PRODUCTION WEBHOOK ===");
// Call the production webhook endpoint
const webhookUrl = "https://www.vibe-list.com/api/webhook/stripe";
console.log(`Webhook URL: ${webhookUrl}`);
const response = await fetch(webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Stripe-Signature": stripeSignature,
"User-Agent": "Stripe/1.0 (+https://stripe.com/docs/webhooks)",
},
body: webhookPayload,
});
console.log(`Response status: ${response.status}`);
const responseText = await response.text();
console.log(`Response body: ${responseText}`);
if (response.ok) {
console.log("✅ Production webhook call successful!");
// Check if user was updated
console.log("\n=== VERIFYING DATABASE UPDATE ===");
// Get customer email from Stripe
const customer = await stripe.customers.retrieve(session.customer);
console.log(`Checking user: ${customer.email}`);
// Check database
const { createClient } = await import("@supabase/supabase-js");
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
const { data: profile, error } = await supabase
.from("profiles")
.select(
"has_access, customer_id, price_id, access_expires_at, updated_at"
)
.eq("email", customer.email)
.single();
if (error) {
console.log(`❌ Database check failed: ${error.message}`);
} else {
console.log(`Database status for ${customer.email}:`);
console.log(`- Has access: ${profile.has_access}`);
console.log(`- Customer ID: ${profile.customer_id || "none"}`);
console.log(`- Price ID: ${profile.price_id || "none"}`);
console.log(`- Last updated: ${profile.updated_at}`);
if (profile.has_access && profile.customer_id) {
console.log("🎉 SUCCESS! Production webhook working!");
} else {
console.log("❌ FAILURE! Production webhook not updating database");
}
}
} else {
console.log("❌ Production webhook call failed!");
console.log("Response:", responseText);
}
} catch (error) {
console.error("❌ Test failed:", error.message);
}
}
// Run the test
testProductionWebhook().catch(console.error);