#subliminalman - webhooks

1 messages · Page 1 of 1 (latest)

blissful wren
#

HI!

molten pollen
#

Hello!

proper ingot
#

@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'

molten pollen
#

ok cool

  1. 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?

  2. Thank you for the clarification on that

proper ingot
molten pollen
#

ok so I'll add that to my request.

for other events like invoice paid and invoice failed those would auto have the priceID?

proper ingot
#

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

molten pollen
#

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({
proper ingot
#

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)

molten pollen
#

Ok so I'm understanding

  1. Make the Checkout Session call on the backend of the Subscription
  2. Grab the Webhook with the checkout.session.completed event
  3. Do another call to retrieve the Session.

What is the call I need to do for step 3? I'm using nodejs

proper ingot
molten pollen
#

thank you

molten pollen
#

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;                
    }
}));
green field
#

👋 stepping in can catching up

#

You still have your endpointSecret as empty string

molten pollen
#

Oh sorry, I removed that because of the text limit

#

I have it in my code

green field
#

okie. Does it work?

#

I suggest to test it by sending some event from CLI

molten pollen
#

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.

green field
#

Can you give me its value?

molten pollen
#

id: 'cs_test_a1N8tqveQvQVfNmMSqrnPFVQTDD9D3sEYi1snGeiMY8uv6nZ8FuxdvKbZ5'

green field
#

Yeah you should be able to retrieve it. It's a valid Checkout Session Id

molten pollen
#

Yeah I just keep getting this in my error console.

TypeError: Cannot read properties of undefined (reading 'sessions')
green field
#

sessions with "s"?

molten pollen
#

ah yes

green field
#

by which line did it error?

#

await stripe.checkout.sessions.retrieve looks correct to me

molten pollen
#
const session = await stripe.checkout.sessions.retrieve(
green field
#

did you initialize stripe?

#

const stripe = require('stripe')('sk_test_xxx');

molten pollen
#

ah

green field
#

I think your Node server hasn't recognized stripe, then stripe.checkout is undefined

molten pollen
#

That works! you're a genius!

#

Thank you!

molten pollen
#

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?

green field
#

You see pr_1Ky1EcHN0DwtVLLV4Czk7Vgq, right?

molten pollen
#

I see li_1KyS9kHN0DwtVLLV1TWkKHQy

green field
#

That's the line item Id

#

Did it give you a full object under line_items? Like price or price_data

molten pollen
#

ah yeah ok sorry I just realized I wasn't accessing the array item

#

it's been a long day 🥱

#

ok finally got what I needed! Thank you so much!