#aswinpy
1 messages · Page 1 of 1 (latest)
Are those values already stored in the database before the webhook comes in? I wouldn't expect any of the IDs to update if that's the case
SELECT id,email,stripe_customer_id,stripe_subscription_id,stripe_price_id,stripe_current_period_end FROM `User`;"id email stripe_customer_id stripe_subscription_id stripe_price_id stripe_current_period_end
kp_3d2c4db32e51480ba563d5116eb7209f abc@gmail.com
kp_43dc139d1041437f9fc2f21f7c09753a def@gmail.com" when sign up id and email generated, others are empty
So those values are null in the database?
Can you add a log line console.log(); in your code to confirm that the object has the attributes you're looking for?
something like stripeCustomerId?
I would just log the whole object so you can look through it and manually confirm that each attribute is there
0 worked
import Stripe from 'stripe';
import { NextApiRequest, NextApiResponse } from 'next';
import { db } from '@/db';
const stripe = new Stripe(process.env.STRIPE_TEST_SECRET_KEY ?? '', {
apiVersion: '2023-08-16',
});
export const config = {
api: {
bodyParser: false,
},
};
const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const buf = await buffer(req);
const sig = req.headers['stripe-signature'] as string | string[];
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
buf.toString(),
sig,
process.env.STRIPE_WEBHOOK_SECRET ?? ''
);
} catch (err) {
console.log(`Webhook Error: ${err?.message}`);
return res.status(400).send(`Webhook Error: ${err?.message}`);
}
if (event.type === 'invoice.payment_succeeded') {
const invoice = event.data.object as Stripe.Invoice;
if (!invoice.customer || !invoice.subscription) {
return res.status(400).send('Invalid Stripe invoice event');
}
const subscription = await stripe.subscriptions.retrieve(invoice.subscription as string);
await db.user.update({
where: {
stripeCustomerId: invoice.customer as string,
},
data: {
stripeSubscriptionId: subscription.id,
stripePriceId: subscription.items.data[0].price.id,
stripeCurrentPeriodEnd: new Date(subscription.current_period_end * 1000),
},
});
}
console.log('success');
console.log("event", event);
console.log("event.type", event.type);
console.log("event.data.object", event.data.object);
console.log("event.data.object.customer", event.data.object.customer);
res.status(200).json({ received: true });
} else {
res.setHeader('Allow', 'POST');
res.status(405).send('Method Not Allowed');
}
};
export default webhookHandler;```
i console log but i do not see anything in the console
Ah so it sounds like your server is not receiving our webhook events
Can you send me your account ID (acct_1234)?
acct_1MxuYKGBNL1lkOS1
Are you running a CLI webhook session? I do not see any active webhook endpoints on your account for this event to be sentt o
i guess i am not doing that, i just pnpm dev my app, sign in and pay
Gotcha, if you are testing with a localhost server on your laptop or something you can use the Stripe CLI for forward events to it https://stripe.com/docs/webhooks#local-listener
And if you have a public URL you can set up a test endpoint in your dashboard https://stripe.com/docs/webhooks#register-webhook
Is /webhooks also the name of your webhook endpoint in your server's code?
You may need to change that CLI command to match the endpoint on your server's code