import Stripe from 'stripe';
const stripKey = process.env.STRIPE_PRIVATE_KEY;
const stripe = new Stripe(stripKey, { apiVersion: '2023-10-16'});
export async function webhook (event) {
console.log('Received Stripe Event:', event);
const stripeEndpointKey = process.env.STRIP_ENDPOINT_KEY;
let stripeEvent;
if (stripeEndpointKey) {
console.log("inside if stripeEndKey=======>", stripeEndpointKey)
// Get the signature sent by Stripe
const signature = event.headers['Stripe-Signature'];
console.log("signature=========>", signature)
try {
stripeEvent = stripe.webhooks.constructEvent(
event.rawBody,
signature,
stripeEndpointKey
);
console.log("stripeEvent-------->", stripeEvent);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
}
}
// Handle the specific event type
switch (stripeEvent.type) {
case 'checkout.session.completed':
const paymentIntent = stripeEvent.data.object;
// console.log('Received Stripe Event:', JSON.stringify(event, null, 2));
console.log("pamyentintend====>",paymentIntent);
console.log("testing the stripe event")
// await handlePaymentSucceeded(stripeEvent.data.object);
break;
// Add more cases for other event types as needed
default:
console.log(`Unhandled event type: ${stripeEvent.type}`);
}
return { statusCode: 200, body: 'Webhook handled successfully' };
}
above is my typescipt async function, I am using serverless framework.
I want to test the above endpoint using postman in the POST method I am passing raw body which is below.
{
"type": "checkout.session.completed",
"data": {
"object": {
"id": "session_id"
}
}
}
when I hit the API I am getting error in my console like :
Webhook signature verification failed. No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
I have tested the same endpoint using stripe CLI I followed below steps to test it from stripe CLI: stripe listen --forward-to http://localhost:3000/dev/v1/webhook --timeout 120 i added this endpoint to listen and after that in the another console I used below command: stripe trigger checkout.session.completed
after that i got response but when I am trying to test from postman I am getting signature error please help me to resolve this. I am trying to create webhook endpoint for the recurring payment.