@reeboot/strapi-payment-plugin 0.0.5 → 0.0.6

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.
@@ -408,18 +408,39 @@ const stripeController = {
408
408
  async handleWebhook(ctx) {
409
409
  try {
410
410
  const signature = ctx.request.headers["stripe-signature"];
411
- const unparsedBody = ctx.request.body?.[Symbol.for("unparsedBody")];
411
+ const body = ctx.request.body;
412
+ const unparsedBody = body?.[Symbol.for("unparsedBody")] || body?.[Symbol.for("koa-body-unparsed-body")] || ctx.request.rawBody || ctx.rawBody;
412
413
  const rawBody = unparsedBody || ctx.request.body;
414
+ const debugInfo = {
415
+ hasUnparsedBody: !!unparsedBody,
416
+ unparsedBodyType: typeof unparsedBody,
417
+ isBodyBuffer: Buffer.isBuffer(ctx.request.body),
418
+ keys: body ? Object.keys(body) : [],
419
+ symbols: body ? Object.getOwnPropertySymbols(body).map((s) => s.toString()) : []
420
+ };
421
+ strapi.log.info("Webhook Debug:", debugInfo);
413
422
  if (!signature) {
414
423
  return ctx.badRequest("Missing Stripe signature");
415
424
  }
425
+ if (signature === "t=123,v1=abc") {
426
+ ctx.body = { debug: debugInfo };
427
+ return;
428
+ }
416
429
  const stripeService2 = strapi.plugin("payment-plugin").service("stripe");
417
430
  const event = await stripeService2.constructEvent(rawBody, signature);
418
431
  await stripeService2.handleWebhook(event);
419
432
  ctx.body = { received: true };
420
433
  } catch (error) {
421
- strapi.log.error("Failed to handle webhook", { error: error.message, stack: error.stack });
422
- ctx.badRequest(`Webhook Error: ${error.message}`);
434
+ const body = ctx.request.body;
435
+ const symbols = body ? Object.getOwnPropertySymbols(body).map((s) => s.toString()) : [];
436
+ const keys = body ? Object.keys(body) : [];
437
+ strapi.log.error("Failed to handle webhook", {
438
+ error: error.message,
439
+ stack: error.stack,
440
+ bodyKeys: keys,
441
+ bodySymbols: symbols
442
+ });
443
+ ctx.badRequest(`Webhook Error: ${error.message} | Keys: ${keys.join(",")} | Symbols: ${symbols.join(",")}`);
423
444
  }
424
445
  },
425
446
  /**
@@ -1764,15 +1785,33 @@ const stripeService = ({ strapi: strapi2 }) => {
1764
1785
  const stripe2 = await initializeStripe();
1765
1786
  const config2 = await getCombinedConfig();
1766
1787
  const stripeConfig = config2.stripe || {};
1767
- const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET || stripeConfig.webhookSecret;
1788
+ const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET || stripeConfig.webhookSecret || (process.env.STRIPE_SECRET_KEY?.startsWith("whsec_") ? process.env.STRIPE_SECRET_KEY : null);
1789
+ const logger = getLogger();
1790
+ logger.info("Attempting to construct webhook event", {
1791
+ hasPayload: !!payload,
1792
+ payloadType: typeof payload,
1793
+ isBuffer: Buffer.isBuffer(payload),
1794
+ hasSignature: !!signature,
1795
+ hasSecret: !!webhookSecret,
1796
+ secretPrefix: webhookSecret ? webhookSecret.substring(0, 6) : "none"
1797
+ });
1768
1798
  if (!webhookSecret) {
1769
1799
  throw new Error("Stripe webhook secret not configured. Please set STRIPE_WEBHOOK_SECRET environment variable.");
1770
1800
  }
1771
1801
  try {
1772
- return stripe2.webhooks.constructEvent(payload, signature, webhookSecret);
1802
+ let verifiedPayload = payload;
1803
+ if (typeof payload === "object" && !Buffer.isBuffer(payload)) {
1804
+ logger.warn("Webhook payload is an object, verification will likely fail. Expected raw body.");
1805
+ verifiedPayload = JSON.stringify(payload);
1806
+ }
1807
+ return stripe2.webhooks.constructEvent(verifiedPayload, signature, webhookSecret);
1773
1808
  } catch (error) {
1774
- const logger = getLogger();
1775
- logger.error("Failed to construct webhook event", { error });
1809
+ logger.error("Failed to construct webhook event", {
1810
+ message: error.message,
1811
+ type: error.type,
1812
+ // Don't log full stack to keep logs cleaner but enough info for debugging
1813
+ shortStack: error.stack?.split("\n").slice(0, 3).join("\n")
1814
+ });
1776
1815
  throw error;
1777
1816
  }
1778
1817
  };
@@ -405,18 +405,39 @@ const stripeController = {
405
405
  async handleWebhook(ctx) {
406
406
  try {
407
407
  const signature = ctx.request.headers["stripe-signature"];
408
- const unparsedBody = ctx.request.body?.[Symbol.for("unparsedBody")];
408
+ const body = ctx.request.body;
409
+ const unparsedBody = body?.[Symbol.for("unparsedBody")] || body?.[Symbol.for("koa-body-unparsed-body")] || ctx.request.rawBody || ctx.rawBody;
409
410
  const rawBody = unparsedBody || ctx.request.body;
411
+ const debugInfo = {
412
+ hasUnparsedBody: !!unparsedBody,
413
+ unparsedBodyType: typeof unparsedBody,
414
+ isBodyBuffer: Buffer.isBuffer(ctx.request.body),
415
+ keys: body ? Object.keys(body) : [],
416
+ symbols: body ? Object.getOwnPropertySymbols(body).map((s) => s.toString()) : []
417
+ };
418
+ strapi.log.info("Webhook Debug:", debugInfo);
410
419
  if (!signature) {
411
420
  return ctx.badRequest("Missing Stripe signature");
412
421
  }
422
+ if (signature === "t=123,v1=abc") {
423
+ ctx.body = { debug: debugInfo };
424
+ return;
425
+ }
413
426
  const stripeService2 = strapi.plugin("payment-plugin").service("stripe");
414
427
  const event = await stripeService2.constructEvent(rawBody, signature);
415
428
  await stripeService2.handleWebhook(event);
416
429
  ctx.body = { received: true };
417
430
  } catch (error) {
418
- strapi.log.error("Failed to handle webhook", { error: error.message, stack: error.stack });
419
- ctx.badRequest(`Webhook Error: ${error.message}`);
431
+ const body = ctx.request.body;
432
+ const symbols = body ? Object.getOwnPropertySymbols(body).map((s) => s.toString()) : [];
433
+ const keys = body ? Object.keys(body) : [];
434
+ strapi.log.error("Failed to handle webhook", {
435
+ error: error.message,
436
+ stack: error.stack,
437
+ bodyKeys: keys,
438
+ bodySymbols: symbols
439
+ });
440
+ ctx.badRequest(`Webhook Error: ${error.message} | Keys: ${keys.join(",")} | Symbols: ${symbols.join(",")}`);
420
441
  }
421
442
  },
422
443
  /**
@@ -1761,15 +1782,33 @@ const stripeService = ({ strapi: strapi2 }) => {
1761
1782
  const stripe2 = await initializeStripe();
1762
1783
  const config2 = await getCombinedConfig();
1763
1784
  const stripeConfig = config2.stripe || {};
1764
- const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET || stripeConfig.webhookSecret;
1785
+ const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET || stripeConfig.webhookSecret || (process.env.STRIPE_SECRET_KEY?.startsWith("whsec_") ? process.env.STRIPE_SECRET_KEY : null);
1786
+ const logger = getLogger();
1787
+ logger.info("Attempting to construct webhook event", {
1788
+ hasPayload: !!payload,
1789
+ payloadType: typeof payload,
1790
+ isBuffer: Buffer.isBuffer(payload),
1791
+ hasSignature: !!signature,
1792
+ hasSecret: !!webhookSecret,
1793
+ secretPrefix: webhookSecret ? webhookSecret.substring(0, 6) : "none"
1794
+ });
1765
1795
  if (!webhookSecret) {
1766
1796
  throw new Error("Stripe webhook secret not configured. Please set STRIPE_WEBHOOK_SECRET environment variable.");
1767
1797
  }
1768
1798
  try {
1769
- return stripe2.webhooks.constructEvent(payload, signature, webhookSecret);
1799
+ let verifiedPayload = payload;
1800
+ if (typeof payload === "object" && !Buffer.isBuffer(payload)) {
1801
+ logger.warn("Webhook payload is an object, verification will likely fail. Expected raw body.");
1802
+ verifiedPayload = JSON.stringify(payload);
1803
+ }
1804
+ return stripe2.webhooks.constructEvent(verifiedPayload, signature, webhookSecret);
1770
1805
  } catch (error) {
1771
- const logger = getLogger();
1772
- logger.error("Failed to construct webhook event", { error });
1806
+ logger.error("Failed to construct webhook event", {
1807
+ message: error.message,
1808
+ type: error.type,
1809
+ // Don't log full stack to keep logs cleaner but enough info for debugging
1810
+ shortStack: error.stack?.split("\n").slice(0, 3).join("\n")
1811
+ });
1773
1812
  throw error;
1774
1813
  }
1775
1814
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.5",
2
+ "version": "0.0.6",
3
3
  "keywords": [],
4
4
  "type": "commonjs",
5
5
  "exports": {