#Yharoo
1 messages · Page 1 of 1 (latest)
hello! we don't have a demo with nextjs, but we do have one using nodejs : https://stripe.com/docs/webhooks/quickstart
ahh had this issue before. Do you use server.use(express.json()); in your project?
i'd suggest looking through the demo code
no, we don't use express.json iirc, cause that modifies the request body
@true elbow example:
make sure you set this up in express before you do server.use(express.json())
async function getStripe() {
const API_KEY = sk_test_myapikey
const stripe = new Stripe(API_KEY, {
apiVersion: "2022-11-15"
});
return stripe;
}
async function verifySubscriptionUpdateWebHook(request) {
const event = (await getStripe()).webhooks.constructEvent(
request.body,
request.headers["stripe-signature"],
"whsec_updateendpointsecret"
);
return event;
}
function stripeSubscriptionUpdateWebHook({ server }) {
server.post(
"/api/v1/public/stripe-subscription-updated",
bodyParser.raw({ type: "*/*" }),
async (req, res) => {
try {
const event: any = await verifySubscriptionUpdateWebHook(req);
if (event.type === "customer.subscription.updated" && event.data && event.data.object && event.data.object.customer) {
// Get customer id from session
const customerId = extractCustomerId(event);
// Update userbilling object with new object
// return true and not wait for our end to update
const service = new UserBillingService();
service.updateSubscriptionByCustomerId(customerId, event);
} else if (event.type === "customer.subscription.created" && event.data && event.data.object && event.data.object.customer) {
const customerId = extractCustomerId(event);
const customer: any = await getStripeCustomer(customerId);
if(customer) {
const email = customer.email;
if(email) {
const user = await User.findOne({ email });
updateCustomerPlan(req, user, event.data.object);
}
}
}
res.json({ received: true });
} catch (err) {
res.status(400).send(`Webhook Error: ${err.message}`);
} finally {
console.log(log);
}
}
);
@true elbow see bodyParser.raw({ type: "*/*" }) in the code above
Hey, thanks for reply
I am not using express, I'm using next js
it uses its built in server for backend
ahh okay my bad
hmmm, i just realized that we have a video tutorial using next.js : https://youtu.be/nVvDr6MyEXE?t=601
might be helpful