Webhook Suddenly Stopped Working

13 views
Skip to first unread message

Aditya Makharia

unread,
Oct 16, 2024, 4:25:44 AM10/16/24
to Stripe API Discussion
Hi. I have integrated Stripe payment gateway on my charitable hospital website.

Everything was working well.

Suddely about 15 days back my webhook started giving an error although there have been no changes in the code or the server.

The error is here:

public class StripeWebHookController : System.Web.Http.ApiController
{
    [HttpPost]
    public async Task<ActionResult> Index([System.Web.Http.FromBody] object json)
    {


        StripeManager.WriteLogs("Stripe Webhook Call");

        try
        {

            StripeManager.WriteLogs(json.ToString());

           

            var stripeEvent = EventUtility.ParseEvent(json.ToString(), true);

Error Message:

<Error><Message>An error has occurred.</Message><ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace /><InnerException><Message>An error has occurred.</Message><ExceptionMessage>Type 'System.Web.Mvc.HttpStatusCodeResult' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.</ExceptionMessage><ExceptionType>System.Runtime.Serialization.InvalidDataContractException</ExceptionType><StackTrace>   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.DataContractSerializer.GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---

Please help.

Karl Reid

unread,
Oct 16, 2024, 4:30:27 AM10/16/24
to Stripe API Discussion, adityam...@karmafoundation.ngo
Hi Aditya,

This error doesn't feel like it could be coming from the line you highlighted that calls our SDK function. Rather, from the message and stack trace it seems more likely this happening later in your code when you attempt to construct and return a response back for the incoming request. For instance it's a serialization error (code writing objects into the network stream) while what our SDK is doing here is deserialization (converting the network stream into objects); and the stack trace does not involve our library at all.

I'd suggest investigating this in more detail on your side and trying to determine what might have changed in your code or environment that's causing an error. If you determine this is something specific to Stripe or the stripe-dotnet library, I'd suggest opening a support ticket at https://support.stripe.com/?contact=true

All the best,
Karl

Aditya Makharia

unread,
Oct 16, 2024, 6:27:29 AM10/16/24
to Karl Reid, Stripe API Discussion
Hi Karl,

Thank you for your reply. I have simulated the code on postman and it catches an exception at var stripeEvent = EventUtility.ParseEvent(json.ToString(), true);

It is some Object Related Error, there has been no change in code or environment.

Please see below my complete Code

    [Route("api/[controller]")]

    public class StripeWebHookController : System.Web.Http.ApiController
    {
        [HttpPost]
        public async Task<ActionResult> Index([System.Web.Http.FromBody] object json)
        {


            StripeManager.WriteLogs("Stripe Webhook Call");

            try
            {

                StripeManager.WriteLogs(json.ToString());

               

                var stripeEvent = EventUtility.ParseEvent(json.ToString(), true);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentAmountCapturableUpdated)
                {
                    StripeManager.WriteLogs($"Stripe Webhook Call1::{stripeEvent.Type}");
                }
                else if (stripeEvent.Type == Events.PaymentIntentPaymentFailed)
                {
                    StripeManager.WriteLogs($"Stripe Webhook Call2::{stripeEvent.Type}");
                }
                else if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    StripeManager.WriteLogs($"Stripe Webhook Call3::{stripeEvent.Type}");
                }
                else if (stripeEvent.Type == Events.ChargeCaptured)
                {
                    StripeManager.WriteLogs($"Stripe Webhook Call4::{stripeEvent.Type}");
                }
                else if (stripeEvent.Type == Events.CheckoutSessionCompleted)
                {
                    StripeManager.WriteLogs($"Stripe Webhook Call4::{stripeEvent.Type}");
                    StripeManager.CreateDirectDonation(json.ToString());
                }
                // ... handle other event types
                else
                {
                    Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
                }
                return new HttpStatusCodeResult(200);
            }
            catch (StripeException e)
            {
                return new HttpStatusCodeResult(705);
            }
        }
    }
}

Please advise if you see something unusual?

Warm Regards
Aditya Makharia
Vice President
Karma Foundation
Website - www.karmafoundation.ngo
Twitter - @KarmaForAnimals

Karl Reid

unread,
Oct 16, 2024, 7:16:42 AM10/16/24
to Stripe API Discussion, adityam...@karmafoundation.ngo, Stripe API Discussion, Karl Reid
Hi Aditya,
I don't see anything immediately unusual except the use of a non-standard HTTP response code 705, and also I note that you don't have code in that branch to log any details of an exception which would limit your ability to debug problems. But I feel like that's probably not related to the actual error, it's just observations.
I'm still not seeing anything specific here that indicates an issue in our SDK and it still seems more like an issue in how your server writes the HTTP response. If you're finding more evidence of a Stripe-side problem I'd suggest opening a support ticket at https://support.stripe.com/?contact=true with a minimal reproduction project of the problem and exact details of your .NET runtime version, stripe-dotnet SDK version and other details of the environment where this happens.

All the best,
Karl
Reply all
Reply to author
Forward
0 new messages