'Why does my stripe webhook in .NET Core 5 API get a 500 ERR response only In azure production?

I am trying to make a simple web-hook off of Stripe to update my database when my user has successfully subscribed.

I keep getting the 500 ERR response in the web-hooks part of the stripe developer dashboard.

image of 500 ERR in stripe dev dashboard

The strange thing is it only happens once I publish on Azure. I assume the connection strings are all good since all my other endpoints are working and update my database just fine in production. The only clue I have is in the image above unless there is some way to see more errors in Azure.

I tried setting up Azure to catch more errors but Azure is large and complex. Its been difficult to know where I should even start looking. I cant seem to see anything in the error logs if its even set up right.

This web-hook works on my local machine after setting up a forward in my PowerShell

stripe listen --forward-to https://localhost:44321/api/payments/webhook

Here is my .net webhook below.

[HttpPost("webhook")]
public async Task<IActionResult> WebHook([FromBody] HttpContext request)
{
    try
    {
        using (var stripStream = new StreamReader(HttpContext.Request.Body))
        {
            var json = (await stripStream.ReadToEndAsync()).Replace("\r", "");
            string header = Request.Headers["Stripe-Signature"];
            string signature = _stripeSettings.WHSecret.Trim(new char[] { ' ', '\n', '\r' });

            var stripeEvent = EventUtility.ConstructEvent(json, header, signature);

            if (stripeEvent.Type == Events.CustomerSubscriptionCreated)
            {
                var subscription = stripeEvent.Data.Object as Subscription;

                await addSubscriptionToDb(subscription);
            }
            if (stripeEvent.Type == Events.CustomerCreated)
            {
                var customer = stripeEvent.Data.Object as Customer;

                await addCustomerIdToUser(customer);
            }
            else
            {
                Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
            }

            return Ok();
        }
    }
    catch (StripeException e)
    {
        return BadRequest();
    }
}

I found it difficult to know where and how to start troubleshooting this since I am new to stripe and new to web-hooks.

Does anyone know if there is some special way I need to adjust settings in Azure to accept web-hooks differently or could this be my own code?

======================

I believe the issue is one of these lines of code... after taking these lines out and all the if else statements I get the 400/500 errors to go away

var json = (await stripStream.ReadToEndAsync()).Replace("\r", "");

string header = Request.Headers["Stripe-Signature"];

string signature = _stripeSettings.WHSecret.Trim(new char[] { ' ', '\n', '\r' });

var stripeEvent = EventUtility.ConstructEvent(json, header, signature);

When leaving these lines of code as the only lines in the try block I still get an error 500 showing in the response to the Stripe logs.

Here is a stack trace I found in azure

"stackTrace": " at Stripe.EventUtility.ValidateSignature(String json, String stripeSignatureHeader, String secret, Int64 tolerance, Int64 utcNow)\r\n at Stripe.EventUtility.ConstructEvent(String json, String stripeSignatureHeader, String secret, Int64 tolerance, Int64 utcNow, Boolean throwOnApiVersionMismatch)\r\n at Stripe.EventUtility.ConstructEvent(String json, String stripeSignatureHeader, String secret, Int64 tolerance, Boolean throwOnApiVersionMismatch)\r\n at MIP_Back.Controllers.PaymentsController.WebHook() in C:\Users\.....

and another error

And I also found the following message

Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.TargetSite.MethodHandle.Value. Serialization and deserialization of 'System.IntPtr' instances are not supported.

Any help or advice is appreciated. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source