Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract meta data from Stripe web hook .Net

I'm struggling to work with the data posted by Stripe to my web hook.

I'm using the basic checkout. I have everything working as intended. CLI is forwarding the web hooks after "checkout.session.completed". My hook is being hit as expected. However, I can't work out how to extract the meta data. I can see it in the object but I can't work out how. What is an "IhasObject"? I copied the code from the docs below.

 var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
        try
        {
            // check if was from Stripe
            var stripeEvent = EventUtility.ConstructEvent(json,Request.Headers["Stripe-Signature"], endpointSecret);

            // Handle the event
            if (stripeEvent.Type == Events.CheckoutSessionCompleted)
            {
                // TODO update the DB and set the booking to PAID based on the transaction Id 
                // in Metadata
                var session = stripeEvent.Data.Object;

               

I can see the correct data in the session object in VS debug and in the raw json. But how do I get it out of the session object or event? It's unclear and there are no examples in the documentation. I am tempted to read the json into a POCO but isn't this the point of the event object?

Can someone point me in the right direction? I just want to fulfill the order but the examples don't show how to work with the session object.

like image 552
Norbert Norbertson Avatar asked Oct 28 '25 20:10

Norbert Norbertson


1 Answers

You can cast the payload of the event to a CheckoutSession and read the metadata.

 var session = (Stripe.Checkout.Session) stripeEvent.Data.Object; 
 var metadata = session.Metadata;
like image 58
karllekko Avatar answered Oct 31 '25 11:10

karllekko