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();
}
}
}
}
Click on "Mark as Answer" if my answers help you along.
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.
}
}
}
}
Contributor
2611 Points
2710 Posts
Stripe WebHook implementation from MVC to Web Pages
Feb 22, 2020 05:51 PM|wavemaster|LINK
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?
All-Star
53021 Points
23607 Posts
Re: Stripe WebHook implementation from MVC to Web Pages
Feb 23, 2020 01:54 PM|mgebhard|LINK
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.