locked
Stripe WebHook implementation from MVC to Web Pages RRS feed

  • Question

  • User379720387 posted

    As per Stripe:

    Webhook data is sent as JSON in the POST request body. The full event details are included and can be used directly, after parsing the JSON into an Event object.

    the code sample is for AspNetCore.Mvc, which I am not using, nor do I understand.

    I am not making a request, just reading the response.

    What would this look like in Web Pages?

    using System;
    using System.IO;
    using Microsoft.AspNetCore.Mvc;
    using Stripe;
    
    namespace workspace.Controllers
    {
        [Route("api/[controller]")]
        public class StripeWebHook : Controller
        {
            [HttpPost]
            public async Task<IActionResult> Index()
            {
                var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
    
                try
                {
                    var stripeEvent = EventUtility.ParseEvent(json);
    
                    // Handle the event
                    if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                    {
                        var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                        // Then define and call a method to handle the successful payment intent.
                        // handlePaymentIntentSucceeded(paymentIntent);
                    }
                    else if (stripeEvent.Type == Events.PaymentMethodAttached)
                    {
                        var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                        // Then define and call a method to handle the successful attachment of a PaymentMethod.
                        // handlePaymentMethodAttached(paymentMethod);
                    }
                    // ... handle other event types
                    else
                    {
                        // Unexpected event type
                        return BadRequest();
                    }
                    return Ok();
                }
                catch (StripeException e)
                {
                    return BadRequest();
                }
            }
        }
    }
    

    Saturday, February 22, 2020 5:51 PM

Answers

  • User475983607 posted

    What would this look like in Web Pages?

    Your code goes in the Page_Load event for whatever page you are using to handle the POST.  You should consider using an HTTP Handler since there's no UI.

        public partial class PostExample : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.HttpMethod.ToUpper() == "POST")
                {
                    using (var reader = new StreamReader(Request.InputStream))
                    {
                        //deserialize the input stream and write business logic.
                    }
                }
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 23, 2020 1:54 PM