Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Webhook Payload Parsing #55

Open
QuinnDamerell opened this issue Nov 25, 2022 · 6 comments
Open

Handle Webhook Payload Parsing #55

QuinnDamerell opened this issue Nov 25, 2022 · 6 comments

Comments

@QuinnDamerell
Copy link

Maybe I missed this, but I don't see any way of parsing and handling webhook body message payloads. It would be great if the SDK had some way of giving it the raw webhook incoming json body and allowing the SDK to validate it and parse it into the common model objects.

@johnbotris
Copy link

I would like to second this :)

@alexjarvie
Copy link

alexjarvie commented May 26, 2023

See readme
#Deserialize an object
ApiConfig.DeserializeObject(jsonString);

example webhook controller:

public async Task<IActionResult> Post(Object _object)    //ChargeBee.Models.Event 
        {
        ApiConfig.Configure(site, key);
        Event _event = ApiConfig.DeserializeObject<Event>(_object.ToString());
        
        //Do stuff   ie switch on _event.EventType or use things like  _event.Content.Customer.Id etc

@cb-alish
Copy link
Collaborator

Hi @QuinnDamerell , @alexjarvie solution is working for us, please let us know if this works for you ?

@cb-sriramthiagarajan
Copy link
Collaborator

Hi @QuinnDamerell @alexjarvie — can you please confirm if this solution is working for you?

@QuinnDamerell
Copy link
Author

QuinnDamerell commented May 15, 2024

Thanks for that. That does work, but it would still be nice if there was something a little more all-inclusive.

For example, Stripe has:

var signatureHeader = Request.Headers["Stripe-Signature"];
var stripeEvent = EventUtility.ConstructEvent(requestBodyJsonStr, signatureHeader, "private_webhook_key")

So you pass it to the webhook body, the signature header, and your private webhook key. It does all of the work to authenticate the call, parse the json, and return an object.

It's not too much work to do in chargebee, I do something like this:

if(!HttpContext.Request.Headers.ContainsKey("Authorization"))
{
    throw new Exception("No auth header found.");
}
var value = HttpContext.Request.Headers["Authorization"];
if(value != $"Basic {<chargebee webhook private key>}")
{
    throw new Exception($"Invalid auth header found. {value}");
}
using (var sr = new StreamReader(Request.Body))
{
     var root = new ChargeBee.Models.Event(await sr.ReadToEndAsync()));
     ... <handle webhook logic>
}

It would be nice if that logic above was wrapped into an SDK function and documented, so it's easier to find.

@QuinnDamerell
Copy link
Author

My biggest issue was it was hard to find that new ChargeBee.Models.Event(await sr.ReadToEndAsync())); was a good way of parsing the webhook body into an object. I don't think I could find that in any of the ChargeBee docs, I'm not sure how I figured it out.

Before that, I was doing it manually by making my own c# classes to mock the ChargeBee json objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants