I am using Stripe Checkout in an ASP.NET Web Forms app to let people pay for subscriptions, and that part of the code works fine. I created a webhook with the following code:
using Stripe;
using Stripe.Checkout;
using System.IO;
using System.Web;
using System;
namespace BNet {
public class spdata : IHttpHandler {
public void ProcessRequest ( HttpContext ctx ) {
try {
var epSecret = "whsec_u...";
var json = new StreamReader(ctx.Request.InputStream).ReadToEnd();
FileOps.WriteFile ("~/files/output.txt", "testing", out _, out _ );
var sig = ctx.Request.Headers["Stripe-Signature"];
try {
var se = EventUtility.ConstructEvent(
json,
sig,
epSecret
);
if ( se.Type == "checkout.session.completed" ) {
var session = se.Data.Object as Session;
ProcessSubscription ( session );
}
}
catch ( StripeException e ) {
FileOps.WriteFile ( "~/files/StripeLog.txt", e.Message, out _, out _ );
}
catch ( Exception ex ) {
FileOps.WriteFile ( "~/files/ErrorLog.txt", ex.Message, out _, out _ );
}
ctx.Response.Write ( "ok" );
ctx.Response.Flush ( );
}
catch (Exception exp) {
ctx.Response.Write ( exp.Message );
}
}
void ProcessSubscription (Session session) {
FileOps.WriteFile ( "StripeLog.txt", session.ToString ( ), out _, out _ );
}
public bool IsReusable {
get {
return false;
}
}
}
}
So, I created an endpoint in Stripe to use this webhook, and when I run the app, the Dashboard returns a server status of 200 OK, but none of the code in the webhook ever fires.
Then, I set up Stripe CLI to test the webhook locally. I use the following command to start CLI:
stripe listen --forward-to http://localhost:44357/spdata
CLI gives me a secret key, which I copied into the webhook. When I run the web app, it goes just fine. But in the CLI window, here's what I get for every event Stripe fires back to me:
2021-06-08 15:38:23 --> checkout.session.completed [evt_1J0CctBQEZK85JIBn76jElzT]
2021-06-08 15:38:23 [ERROR] Failed to POST: Post "http://localhost:44357/spdata": read tcp [::1]:54739->[::1]:44357: wsarecv: An existing connection was forcibly closed by the remote host.
I don't know what the source of the error is. I shut down Windows Firewall, and I don't have anything else running that could interfere. Any help out there?
[Route("[controller]")]
to indicate the URL/path to your code just above yourpublic class
declaration? If you add simple logging to the beginning of your function and try a simple POST request via curl, likecurl -v -X POST http://localhost:44357/spdata
does it log, or do you get an error/unexpected behavior/no log? – Inlaycurl
test, to see if that's also rejected, or if it's something specific to Stripe CLI. – Inlay