0
votes

I am working with Stripe/Stripe.net Webhooks in my ASP.Net Core app. I am trying to map an object where I can use the properties returned from the Webhook JSON response and save various properties into my database. Using Postman, I am able to receive the sample JSON that I got from Stripe test webhooks in my Controller method and parse the JSON using

var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
Event stripeEvent = EventUtility.ParseEvent(json);

which returns:

stripeEvent = {<Stripe.Event@2269235 id=evt_00000000000000> JSON: {"id": "evt_00000000000000", "object": "event", "account": null, "api_version": "2020-03-02", "created": 1326853478, "data": {    "object": { "id": "ch_00000000000000", "...

I am needing to map the JSON from the RawObject ChildrenTokens into a model but I'm not able to access the ChildrenTokens through C#. Is there any way to be able to map these properties?

1
Can you check this thread to see if it's helpful: stackoverflow.com/questions/57831861/… In the answer, stripeEvent.Data.Object is accessed and through it, CustomerId is accessed.AntiqTech

1 Answers

2
votes

The trick here is to cast the stripeEvent.Data.Object as the specific class that the underlying event data should represent. Typically, you'll have a switch or if statement checking the stripeEvent.Type.

For instance, if the type of the event is payment_intent.succeeded, then the underlying data would be a PaymentIntent and you might have the following code to check the type, then cast to that type.

if (stripeEvent.Type == Events.PaymentIntentSucceeded)
{
  var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
}

From there, you can interact with the paymentIntent and it's related properties.