vercel / nextjs-subscription-payments

Clone, deploy, and fully customize a SaaS subscription application with Next.js.
https://subscription-payments.vercel.app/
MIT License
5.83k stars 1.19k forks source link

No signatures found matching the expected signature for payload #228

Open abaybektursun opened 11 months ago

abaybektursun commented 11 months ago

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 commented 11 months ago

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.

commandcenterio commented 11 months ago

I'm facing the same issue

commandcenterio commented 11 months ago

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

commandcenterio commented 11 months ago

@thorwebdev @leerob any insight?

abaybektursun commented 11 months ago

@thorwebdev ? ^

commandcenterio commented 11 months ago

My issue was that i was using the incorrect secret

haramishra commented 10 months ago

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.

shsunmoonlee commented 10 months ago

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

ebowwa commented 4 months ago

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

Asrover commented 4 months ago

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;```