stripe-no-webhooks 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +24 -16
- package/dist/index.mjs +24 -16
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -170,26 +170,34 @@ function createStripeHandler(config = {}) {
|
|
|
170
170
|
async function handleWebhook(request) {
|
|
171
171
|
try {
|
|
172
172
|
const body = await request.text();
|
|
173
|
+
const url = new URL(request.url);
|
|
174
|
+
const isLocalhost = url.hostname === "localhost" || url.hostname === "127.0.0.1";
|
|
173
175
|
const signature = request.headers.get("stripe-signature");
|
|
174
|
-
if (!signature) {
|
|
175
|
-
return new Response("Missing stripe-signature header", { status: 400 });
|
|
176
|
-
}
|
|
177
176
|
let event;
|
|
178
|
-
|
|
179
|
-
event =
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
177
|
+
if (isLocalhost) {
|
|
178
|
+
event = JSON.parse(body);
|
|
179
|
+
} else {
|
|
180
|
+
if (!signature) {
|
|
181
|
+
return new Response("Missing stripe-signature header", {
|
|
182
|
+
status: 400
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
event = stripe.webhooks.constructEvent(
|
|
187
|
+
body,
|
|
188
|
+
signature,
|
|
189
|
+
stripeWebhookSecret
|
|
190
|
+
);
|
|
191
|
+
} catch (err) {
|
|
192
|
+
const message = err instanceof Error ? err.message : "Unknown error";
|
|
193
|
+
return new Response(
|
|
194
|
+
`Webhook signature verification failed: ${message}`,
|
|
195
|
+
{ status: 400 }
|
|
196
|
+
);
|
|
197
|
+
}
|
|
190
198
|
}
|
|
191
199
|
if (sync) {
|
|
192
|
-
await sync.
|
|
200
|
+
await sync.processEvent(event);
|
|
193
201
|
}
|
|
194
202
|
if (callbacks) {
|
|
195
203
|
await handleCallbacks(event, callbacks);
|
package/dist/index.mjs
CHANGED
|
@@ -134,26 +134,34 @@ function createStripeHandler(config = {}) {
|
|
|
134
134
|
async function handleWebhook(request) {
|
|
135
135
|
try {
|
|
136
136
|
const body = await request.text();
|
|
137
|
+
const url = new URL(request.url);
|
|
138
|
+
const isLocalhost = url.hostname === "localhost" || url.hostname === "127.0.0.1";
|
|
137
139
|
const signature = request.headers.get("stripe-signature");
|
|
138
|
-
if (!signature) {
|
|
139
|
-
return new Response("Missing stripe-signature header", { status: 400 });
|
|
140
|
-
}
|
|
141
140
|
let event;
|
|
142
|
-
|
|
143
|
-
event =
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
141
|
+
if (isLocalhost) {
|
|
142
|
+
event = JSON.parse(body);
|
|
143
|
+
} else {
|
|
144
|
+
if (!signature) {
|
|
145
|
+
return new Response("Missing stripe-signature header", {
|
|
146
|
+
status: 400
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
event = stripe.webhooks.constructEvent(
|
|
151
|
+
body,
|
|
152
|
+
signature,
|
|
153
|
+
stripeWebhookSecret
|
|
154
|
+
);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
const message = err instanceof Error ? err.message : "Unknown error";
|
|
157
|
+
return new Response(
|
|
158
|
+
`Webhook signature verification failed: ${message}`,
|
|
159
|
+
{ status: 400 }
|
|
160
|
+
);
|
|
161
|
+
}
|
|
154
162
|
}
|
|
155
163
|
if (sync) {
|
|
156
|
-
await sync.
|
|
164
|
+
await sync.processEvent(event);
|
|
157
165
|
}
|
|
158
166
|
if (callbacks) {
|
|
159
167
|
await handleCallbacks(event, callbacks);
|