在 Stripe CLI 中使用 Stripe webhook 时出现问题

问题描述 投票:0回答:5

我在 ASP.NET Web 表单应用程序中使用 Stripe Checkout 来让人们支付订阅费用,并且这部分代码工作正常。我使用以下代码创建了一个网络钩子:

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;
            }
        }
    }
}

因此,我在 Stripe 中创建了一个端点来使用此 Webhook,当我运行该应用程序时,仪表板返回服务器状态 200 OK,但 Webhook 中的任何代码都不会触发。

然后,我设置了 Stripe CLI 在本地测试 Webhook。我使用以下命令启动 CLI:

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

CLI 为我提供了一个密钥,我将其复制到 webhook 中。当我运行网络应用程序时,一切顺利。但在 CLI 窗口中,以下是 Stripe 向我返回的每个事件的结果:

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.

我不知道错误的根源是什么。我关闭了 Windows 防火墙,并且没有运行任何其他可能会干扰的程序。有什么帮助吗?

c# asp.net stripe-payments webhooks
5个回答
7
投票

我可能会迟到,但请改变这个

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

到此

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

Stripe 只会在 https 上发布。

注意:如果您不指定协议,就像这样

stripe listen --forward-to localhost:44357/spdata
(这就是文档中的操作方式),它将默认为 http,并且不会仅处理
FAILED TO POST

的一般错误

2
投票

我也不明白为什么我没有通过 CLI 获取 webhook,直到我从“--forward-to”参数中删除了 http://。然后我突然开始得到它们。

试试这个:

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

(注意:我从来没有像你一样遇到过错误 - 也许我使用的是早期的 CLI 版本?)


1
投票

除了将 stripe 指向 https 端口之外,还可以使用

--skip-verify
来使其正常工作。
stripe listen
不会 POST 到 http 端口,正如一个答案正确所述。

但是,如果 stripe 不信任您的本地 TLS 证书(默认情况下),则在尝试建立 TLS 握手时调用 Webhook 将失败。


0
投票

对于我们大多数人来说这可能是显而易见的,但我被它欺骗了:

确保您的控制器操作不受某些授权的限制。

[AllowAnonymous]

症状:您看到状态代码

302
,这是重定向到登录页面

话虽如此,普通 HTTP(不含 HTTPS)现在似乎可以在测试环境中工作。


0
投票

仅适用于仍在努力执行命令的人

条带监听 --forward-to localhost:4242/webhook

执行时必须正在运行

条纹触发[事件]

不然不行。

© www.soinside.com 2019 - 2024. All rights reserved.