#subliminalman - webhooks
1 messages · Page 1 of 1 (latest)
Hello!
@molten pollen this is something I'd recommend just testing in Test mode!
but 1/ that's up to you, depends how your application is built, but yes usually the Price id price_123 represents the service you're selling
2/ customer is a string it's just customer: 'cus_123'
ok cool
-
I have my local set up currently and it doesn't send out priceID when sending the checkout.session.completed even so I was confused. Is there a way to test locally with data closer to what I might expect?
-
Thank you for the clarification on that
the behaviour is the same really, we just don't include line items on Checkout Session by default. You want to read about expansion: https://stripe.com/docs/expand and more specifically https://stripe.com/docs/expand#includable-properties
ok so I'll add that to my request.
for other events like invoice paid and invoice failed those would auto have the priceID?
I'd recommend just testing again, you would get those answers in a few seconds by trying in the Dashboard and looking at the event! But yes they have the information
Apologies, I'm a little confused on this section here. https://stripe.com/docs/expand#with-webhooks
It says I would need to do another call like this in my webhook? Apologies on the basic questions, I make games usually instead of web dev so I'm bringing in a bunch of new info right now.
const session = await stripe.checkout.sessions.create({
when you get the Event in your webhook handler, then you have to make a call that will retrieve the Session and expand the line items to see what's inside of it (or cache this on creation in your database)
Ok so I'm understanding
- Make the Checkout Session call on the backend of the Subscription
- Grab the Webhook with the checkout.session.completed event
- Do another call to retrieve the Session.
What is the call I need to do for step 3? I'm using nodejs
thank you
Ok so I'm trying to do that call but I keep getting the session undefined.
Does this look correct? I'm basing it off of the webhooks example.
'use strict';
const stripe = require('stripe');
const { createCoreController } = require('@strapi/strapi').factories;
const endpointSecret = "";
module.exports = createCoreController('api::webhook.webhook', ({ strapi }) =>({
async create (ctx) {
const sig = ctx.request.header['stripe-signature'];
const unparsedBody = ctx.request.body;
let event;
try {
event = stripe.webhooks.constructEvent(unparsedBody, sig, endpointSecret);
} catch (err) {
ctx.response.status = 400;
console.log(`Webhook Error: ${err.message}`);
return;
}
const eventData = event.data.object;
switch (event.type) {
case 'checkout.session.completed':
// Payment is successful and the subscription is created.
// You should provision the subscription and save the customer ID to your database.
const session = await stripe.checkout.sessions.retrieve(
eventData.id,
{
expand: ['line_items'],
},
);
console.log(session);
break;
case 'invoice.paid':
break;
case 'invoice.payment_failed':
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
ctx.response.status = 200;
}
}));
everything up til this section works
const session = await stripe.checkout.sessions.retrieve(
eventData.id,
{
expand: ['line_items'],
},
);
console.log(session);
When doing that call session doesn't get populated.
I'm trying to get the priceID from session.
id: 'cs_test_a1N8tqveQvQVfNmMSqrnPFVQTDD9D3sEYi1snGeiMY8uv6nZ8FuxdvKbZ5'
Yeah you should be able to retrieve it. It's a valid Checkout Session Id
Yeah I just keep getting this in my error console.
TypeError: Cannot read properties of undefined (reading 'sessions')
sessions with "s"?
ah yes
by which line did it error?
await stripe.checkout.sessions.retrieve looks correct to me
const session = await stripe.checkout.sessions.retrieve(
ah
I think your Node server hasn't recognized stripe, then stripe.checkout is undefined
so I'm trying to get the productID from the line_items but it looks different than what I have on my dashboard. Where do I find that?
You see pr_1Ky1EcHN0DwtVLLV4Czk7Vgq, right?
I see li_1KyS9kHN0DwtVLLV1TWkKHQy