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 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
- try {
179
- event = stripe.webhooks.constructEvent(
180
- body,
181
- signature,
182
- stripeWebhookSecret
183
- );
184
- } catch (err) {
185
- const message = err instanceof Error ? err.message : "Unknown error";
186
- return new Response(
187
- `Webhook signature verification failed: ${message}`,
188
- { status: 400 }
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.processWebhook(body, signature);
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
- try {
143
- event = stripe.webhooks.constructEvent(
144
- body,
145
- signature,
146
- stripeWebhookSecret
147
- );
148
- } catch (err) {
149
- const message = err instanceof Error ? err.message : "Unknown error";
150
- return new Response(
151
- `Webhook signature verification failed: ${message}`,
152
- { status: 400 }
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.processWebhook(body, signature);
164
+ await sync.processEvent(event);
157
165
  }
158
166
  if (callbacks) {
159
167
  await handleCallbacks(event, callbacks);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stripe-no-webhooks",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "author": "Ramon Garate",
5
5
  "description": "Stripe integration without dealing with webhooks",
6
6
  "main": "./dist/index.js",