#geffbluestar
1 messages · Page 1 of 1 (latest)
Can you share the evt_xx ID?
Also, your webhook code that is not returning the JSON you expect
gimme a sec to find the evt_xx ID.
As for your second question, yes. After assiging a variable to stripe.webhooks.constructEvent, the metadata cannot be read, but is still readable in the original request.body
Can you share the code?
how should I send it?
@Post('webhook')
async stripeWebhook(@Req() req: RawBodyRequest<Request>){
console.log('webhook received')
const metadata = req.body['data']['object']['metadata']
const raw = req.rawBody
const sig = req.headers['stripe-signature']
let event;
try {
//event = verified stripe hook
event = this.stripe.webhooks.constructEvent(raw,sig,endpointSecret)
} catch (error) {
throw new Error(error)
}
// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
// process into database
const exist = await this.paymentService.check(event.id)
//exits api if we processed event before
if (exist){
return //no error needed as we do not need to log repeated hooks
}
const addingData = await this.paymentService.create(Number(metadata.user_id),Number(metadata.product),event.id)
if (addingData){
console.log('successfully created new payment record')
return
}else{
throw new Error("error with creating new payment record")
}
}
return
}
^this is an API using nest.js. Currently testing at locahlost. It receives webhooks fine.
also, this doesn't have to be urgent, the code currently works for me, I'm just not sure if I'm doing sth wrong or this is a known bug
oh, and this is where I placed the metadata
@UseGuards(AuthGuard('jwt'))
@Post('web/session/:product')
async create(@Req() req, @Param('product', ParseIntPipe) product : number) {
const id = req.user.id
const subscriptionData = await this.subscriptionsService.findOne(product)
const subscribedProduced = {
currency:"hkd",
product_data:{
name: ${subscriptionData.name} plan,
description: subscriptionData.unlimited ? Unlimited credit plan for ${subscriptionData.duration} : Add ${subscriptionData.credits} credits and enjoy our services for another ${subscriptionData.duration} days
},
unit_amount_decimal: String(subscriptionData.fee*100)
}
const foundEmail = await this.usersService.returnEmail(id)
const session = await this.stripe.checkout.sessions.create({
customer_email: foundEmail ,
payment_method_types: ['card'],
mode: 'payment',
metadata: {
user_id: `${id}`,
product: `${product}`
},
line_items: [
{ price_data:subscribedProduced,
quantity:1,
}],
success_url: `${process.env.REACT_PUBLIC_HOSTNAME}/user-subscription`,
cancel_url: `${process.env.REACT_PUBLIC_HOSTNAME}/user-subscription`,
})
return {url:session.url}
}