Stripe-dotnet: QUESTION: Webhooks url

Created on 7 Apr 2017  ·  14Comments  ·  Source: stripe/stripe-dotnet

Hello,
I'm trying to set up the webhooks from Stripe. I created a handler and copied the same code you have shown in the documentation, but the next instruction is to go on Stripe and

setup the url that points to your StripeHandler.ashx

Simply, I don't know what that means. Please, if you could describe in Leyman's terms the steps I need to take to activate the webhooks, it would be of great help!

Matteo

Most helpful comment

The readme says to setup an IHttpHandler but I can't remember the last time I used one of those let alone how they work. I mostly use owin middleware/nancyfx but the concept is the same for asp.net mvc which is to setup a controller for them. In asp.net mvc that would look like so
```c#
[RoutePrefix("/callbacks/stripe)
public class StripeCallbackController : Controller
{
[HttpPost]
[Route]
public async Task() Index(CancellationToken ct)
{
var json = new StreamReader(Request.InputStream).ReadToEnd();

    var stripeEvent = StripeEventUtility.ParseEvent(json);

    switch (stripeEvent.Type)
    {
        case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
            var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
            break;
    }

    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

}
```

Given the route setup for that controller the url to use on Stripe's end would be https://yourdomain.com/callbacks/stripe

All 14 comments

Your webhook url gets added to your account at https://dashboard.stripe.com/account/webhooks

I know where to add the url, I don't know what the url is supposed to be.

Also, is there anything else I need to install/configure in order to get webhooks from Stripe? I've only installed the stripe.net library through NuGet and performed very basics operations with it (creating customers and subscriptions, charging cards...), so I don't know if anything else is required to make this work.

Thank you for all the help you can give me!
Matteo

the url is whatever path you decide to implement

When you set a webhook url in Stripe's interface, Stripe is going to POST to that address. You make that endpoint url whatever you want to create that is going to be listening for a POST.

Ok, so I guess I'm missing some steps! :)
It's not clear to me what I need to do to receive webhooks, do I have to implement some kind of controller and so define a url myself, or what?

Yep, exactly. It sounds like you may be using old(er) school ASP.Net so i forget how to do it there, so i cannot provide a decent example, but in NancyFx you might do some pseudo-code like

Post["/stripe/some/path/i/made"] = p => { /* implementation details here, converting the post body json to something you might consume */ };

The readme says to setup an IHttpHandler but I can't remember the last time I used one of those let alone how they work. I mostly use owin middleware/nancyfx but the concept is the same for asp.net mvc which is to setup a controller for them. In asp.net mvc that would look like so
```c#
[RoutePrefix("/callbacks/stripe)
public class StripeCallbackController : Controller
{
[HttpPost]
[Route]
public async Task() Index(CancellationToken ct)
{
var json = new StreamReader(Request.InputStream).ReadToEnd();

    var stripeEvent = StripeEventUtility.ParseEvent(json);

    switch (stripeEvent.Type)
    {
        case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
            var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
            break;
    }

    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

}
```

Given the route setup for that controller the url to use on Stripe's end would be https://yourdomain.com/callbacks/stripe

Thank you so much for the help guys!
I'm still doing some tests but I'm starting to see the light at the end of the tunnel!
Keep you posted :)

@xt0rted Very helfpul, thanks! A few typos though in your code, fixed it below to avoid confusion for those using it:

using System.Net;
using System.IO;
using Stripe;
using System.Web.Mvc;
using System.Threading;
using System.Threading.Tasks;

    namespace AwesomeApp.Controllers
{
    [RoutePrefix("/callbacks/stripe")]
    public class StripeCallbackApiController : Controller
    {
        [HttpPost]
        [Route]
        public async Task<ActionResult> Index(CancellationToken ct)
        {
            var json = new StreamReader(Request.InputStream).ReadToEnd();

            var stripeEvent = StripeEventUtility.ParseEvent(json);

            switch (stripeEvent.Type)
            {
                case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
                    var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
                    break;
            }

            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
    }
}

Hi ... I have an asp.net webforms based site using .netframework 4.5 and I have everything else configured using woocommerce via a wordpress site for the subscriptions signup. All I really need at this point is to create a listener for the webhooks in asp.net so I can write data to my local database regarding the user and their plan. But the only examples of listeners on the next I can find are for mvc implementations. I'm hoping I'm not mistaken in thinking that the framework doesn't actually require MVC, and that a listener can also be created using webforms asp.net. If so, I don't suppose anyone has a lead on documentation or examples for that? I'd be very appreciative. Or advice on how to proceed in my context would also be great. Thank you!

@vbwyrde Webhooks are simple POST requests to your server from ours (Stripe's). You should be able to handle this without having to implement MVC. The stripe-dotnet library can receive JSON and parse it as an Event so that you can handle this in your code. I don't have any specific example code to share at the moment though.

@remi-stripe ... thank you for your reply. Example code is often incredibly helpful for those of us who don't yet have experience with how to do certain things. I can certainly continue to poke around and try to find examples of stripe accepting json posts, even though the context is most often in MVC, which is confusing in the sense that I have to figure out how to tweeze out the MVC aspect from the JSON parsing. Thanks for your reply. At least I know now that I am heading in the right direction. I'll keep looking. Thanks again.

Since you're using webforms your best solution will probably be an IHttpHandler which won't require any additional dependencies. An example of one can be found here https://gist.github.com/jaime-fuhr/1752273.

Instead of the js serializer in that example you're better off using stripe.net as shown in earlier comments since that will do the deserialization for you.

When adding this to your web.config you'll be able to specify the path it should use such a /callbacks/stripe.

Thank you! This is very helpful! I will go ahead along these lines, Much appreciated!

Was this page helpful?
0 / 5 - 0 ratings