nextjs-subscription-payments icon indicating copy to clipboard operation
nextjs-subscription-payments copied to clipboard

No signatures found matching the expected signature for payload

Open abaybektursun opened this issue 1 year ago • 11 comments

I followed the Readme step by step multiple times and tried test and production. It was never able to connect to Stripe.

No subscription pricing plans found.

Here is the error/warning in the Vercel Logs:

Function /api/webhooks

❌ Error message: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? 
Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

abaybektursun avatar Aug 01 '23 02:08 abaybektursun

I changed the code to const sig = req.headers.get('stripe-signature'); The warning message is gone, however, it still says No subscription pricing plans found.

abaybektursun avatar Aug 01 '23 02:08 abaybektursun

I'm facing the same issue

commandcenterio avatar Aug 02 '23 20:08 commandcenterio

Spent all day reading old threads and attempting the recommended fixes, no luck. Has anyone resolved this error?

commandcenterio avatar Aug 03 '23 11:08 commandcenterio

@thorwebdev @leerob any insight?

commandcenterio avatar Aug 03 '23 17:08 commandcenterio

@thorwebdev ? ^

abaybektursun avatar Aug 03 '23 17:08 abaybektursun

My issue was that i was using the incorrect secret

commandcenterio avatar Aug 04 '23 00:08 commandcenterio

For me, when I pasted the key on vercel, it added a new line, which caused the error. I removed the new line, and the error was fixed.

haramishra avatar Sep 10 '23 03:09 haramishra

This worked for me . change headers().get'Stripe-Signature') line to

const sig = req.headers.get('stripe-signature') as string;

I've opened a PR for it. Please approve @thorwebdev https://github.com/vercel/nextjs-subscription-payments/pull/242

shsunmoonlee avatar Sep 13 '23 01:09 shsunmoonlee

the pr seems to have been committed to main, and im updated to the most recent push; however, im still facing this exact issue, even with const sig = req.headers.get('stripe-signature') as string; in the app/webhooks/route.ts

ebowwa avatar Mar 07 '24 00:03 ebowwa

Stripe example how to use with NextJS: https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/nextjs/pages/api/webhooks.ts

import {NextApiRequest, NextApiResponse} from 'next';

const handler = async (
  req: NextApiRequest,
  res: NextApiResponse
): Promise<void> => {
  const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

  const webhookSecret: string = process.env.STRIPE_WEBHOOK_SECRET;

  if (req.method === 'POST') {
    const sig = req.headers['stripe-signature'];

    let event: Stripe.Event;

    try {
      const body = await buffer(req);
      event = stripe.webhooks.constructEvent(body, sig, webhookSecret);
    } catch (err) {
      // On error, log and return the error message
      console.log(`❌ Error message: ${err.message}`);
      res.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }

    // Successfully constructed event
    console.log('✅ Success:', event.id);

    // Cast event data to Stripe object
    if (event.type === 'payment_intent.succeeded') {
      const stripeObject: Stripe.PaymentIntent = event.data
        .object as Stripe.PaymentIntent;
      console.log(`💰 PaymentIntent status: ${stripeObject.status}`);
    } else if (event.type === 'charge.succeeded') {
      const charge = event.data.object as Stripe.Charge;
      console.log(`💵 Charge id: ${charge.id}`);
    } else {
      console.warn(`🤷‍♀️ Unhandled event type: ${event.type}`);
    }

    // Return a response to acknowledge receipt of the event
    res.json({received: true});
  } else {
    res.setHeader('Allow', 'POST');
    res.status(405).end('Method Not Allowed');
  }
};

export const config = {
  api: {
    bodyParser: false,
  },
};

const buffer = (req: NextApiRequest) => {
  return new Promise<Buffer>((resolve, reject) => {
    const chunks: Buffer[] = [];

    req.on('data', (chunk: Buffer) => {
      chunks.push(chunk);
    });

    req.on('end', () => {
      resolve(Buffer.concat(chunks));
    });

    req.on('error', reject);
  });
};

export default handler;```

Asrover avatar Mar 13 '24 06:03 Asrover

Double-check the secret in your env, it should be this one: image

serhii-kucherenko avatar Jul 20 '24 09:07 serhii-kucherenko