OK, had ‘strapi::body’ twice, so above fixed. But I still have error:
Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? If a webhook request is being forwarded by a third-party tool, ensure that the exact request body, including JSON formatting and new line style, is preserved. Learn more about webhook signing and explore webhook integration examples for various frameworks at GitHub - stripe/stripe-node: Node.js library for the Stripe API.
My code is
import Stripe from 'stripe'
import unparsed from "koa-body/unparsed.js"
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {apiVersion: '2023-10-16'})
export default {
receive: async (ctx, next) => {
let event
const signature = ctx.request.headers['stripe-signature']
console.log(process.env.STRIPE_WEBHOOK_SECRET)
try {
event = stripe.webhooks.constructEvent(
ctx.request.body[unparsed],
signature,
process.env.STRIPE_WEBHOOK_SECRET
)
} catch (err) {
console.error('Webhook Error1', err.message)
try {
event = stripe.webhooks.constructEvent(
ctx.request.body[Symbol.for('unparsedBody')],
signature,
process.env.STRIPE_WEBHOOK_SECRET
)
} catch (err) {
console.error('Webhook Error2', err.message)
ctx.response.status = 400
ctx.body = `Webhook Error: ${err.message}`
return
}
}
console.log('Received event:', event)
// Handle the event
switch (event.type) {
case 'charge.succeeded':
const chargeSucceeded = event.data.object;
console.log('chargeSucceeded', chargeSucceeded)
// Then define and call a function to handle the event charge.succeeded
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
ctx.response.status = 200
}
}
Can anyone help?