Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem getting Stripe webhook to work in Stripe CLI

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?

like image 777
TampaSportsLover Avatar asked Mar 08 '26 07:03

TampaSportsLover


2 Answers

I might be late, but change this from this

stripe listen --forward-to http://localhost:44357/spdata

to this

stripe listen --forward-to https://localhost:44357/spdata

Stripe will only post on https.

Note: if you don't specify the protocol, like this stripe listen --forward-to localhost:44357/spdata (which is how it is done in the docs) it will default to http and will not work with just a generic error of FAILED TO POST

like image 185
user5826312 Avatar answered Mar 10 '26 20:03

user5826312


I couldn't figure out why I was not getting webhooks through the CLI either, until I removed the http:// from the '--forward-to' parameter. Then I suddenly started getting them.

Try this: stripe listen --forward-to localhost:44357/spdata

(Note: I never even got the errors like you did - maybe I'm on an earlier CLI version?)

like image 29
JonasM Avatar answered Mar 10 '26 21:03

JonasM