#wejh

1 messages · Page 1 of 1 (latest)

rare kettleBOT
left gale
#

Yes, our webhook deliveries are post requests

spare lark
#

`//init firebase
const stripe = new stripe("key")

const db = firestore();

const stripe = new Stripe(" });

const cors = Cors({
allowMethods: ['POST', 'HEAD'],
});

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

const webhookHandler = async (req: IncomingMessage, res: ServerResponse) => {
const nextReq = req as NextApiRequest;
const nextRes = res as NextApiResponse;

if (nextReq.method === 'POST') {
const buf = await buffer(nextReq);
const sig = nextReq.headers['stripe-signature'];

let event;
try {
  if (!sig) throw new Error('Missing Stripe Signature');
  event = stripe.webhooks.constructEvent(buf.toString(), sig, endpointSecret || '');
} catch (err) {
  nextRes.status(400).send(`Webhook error: ${(err as Error).message}`);
  return;
}

if (event.type === 'checkout.session.completed') {
  const session = event.data.object as any;

  const userId = session.customer ? session.customer : '';

  await db.collection('users').doc(userId).collection('accountinfo').doc('info').update({
    paymentTier: 'premium',
  });

  console.log("Payment was successful. ", session);
}

nextRes.json({ received: true });

} else {
nextRes.setHeader('Allow', 'POST');
nextRes.status(405).end('Method not allowed');
}
};

export default cors(webhookHandler);
`

left gale
#

but your server is what controls the response, so you'd need to determin why your code sends the 405 status

spare lark
#

According to my code, the last else statement means that it should be only throwing 405 if req != post, but I know this probably isnt what is happening. Any other reasons why it would be throwing this?

left gale
#

No, i'm not sure. WHats the value of method you're seeing in the requests?

#

Wouldn't make sense if it was a POST to hit that path i agree

rare kettleBOT
spare lark
#

will test rq. Just looked at dashboard for webhook and almost positive that stripe is sending post but going to test and make sure

left gale
#

Sure, yes, but look at the actual requests you receive

#

if (nextReq.method === 'POST') {