Hùng Lê
push data form webhook stripe
I have the following server/api/stripe/webhook code
import { eventHandler, readRawBody, getHeader, createError, sendError } from 'h3'
import Stripe from 'stripe'
// import { firestore } from '~/server/utils/firebaseAdmin'
// import test from '~/services/webhook/index'
export default eventHandler(async (event: any) => {
const stripe = new Stripe(${process.env.STRIPE_SECRET_KEY})
const endpointSecret = ${process.env.STRIPE_WEBHOOK_SECRET}
let body
try {
body = await readRawBody(event, false)
} catch (err) {
throw createError({
statusCode: 400,
statusMessage: Cannot read body: ${err},
})
}
const signature = getHeader(event, 'stripe-signature')
if (!body) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid request body',
})
}
if (!signature) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid stripe-signature',
})
}
let stripeEvent
try {
stripeEvent = stripe.webhooks.constructEvent(
body,
signature,
endpointSecret
)
} catch (err) {
console.error('❌ ERROR: Webhook verification failed:', err)
throw createError({
statusCode: 400,
statusMessage: Webhook error: ${err},
})
}
// Handle the event
if (stripeEvent.type == 'payment_intent.succeeded') {
const paymentIntent = stripeEvent.data.object
const s3Url = paymentIntent?.metadata?.s3Url || ''
if (!s3Url) {
return { received: true }
}
// io.emit('newS3Url', s3Url);
}
return { received: true }
})
how to push s3url in webhook to client by socket?
5 replies
push data form webhook stripe
I have the following server/api/stripe/webhook code
import { eventHandler, readRawBody, getHeader, createError, sendError } from 'h3'
import Stripe from 'stripe'
// import { firestore } from '~/server/utils/firebaseAdmin'
// import test from '~/services/webhook/index'
export default eventHandler(async (event: any) => {
const stripe = new Stripe(
${process.env.STRIPE_SECRET_KEY}
)
const endpointSecret = ${process.env.STRIPE_WEBHOOK_SECRET}
let body
try {
body = await readRawBody(event, false)
} catch (err) {
throw createError({
statusCode: 400,
statusMessage: Cannot read body: ${err}
,
})
}
const signature = getHeader(event, 'stripe-signature')
if (!body) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid request body',
})
}
if (!signature) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid stripe-signature',
})
}
let stripeEvent
try {
stripeEvent = stripe.webhooks.constructEvent(
body,
signature,
endpointSecret
)
} catch (err) {
console.error('❌ ERROR: Webhook verification failed:', err)
throw createError({
statusCode: 400,
statusMessage: Webhook error: ${err}
,
})
}
// Handle the event
if (stripeEvent.type == 'payment_intent.succeeded') {
const paymentIntent = stripeEvent.data.object
const s3Url = paymentIntent?.metadata?.s3Url || ''
if (!s3Url) {
return { received: true }
}
// io.emit('newS3Url', s3Url);
}
return { received: true }
})
how to push s3url in webhook to client?5 replies