【发布时间】:2021-08-25 22:02:44
【问题描述】:
我在 ASP.NET Web Forms 应用程序中使用 Stripe Checkout 让人们为订阅付费,这部分代码运行良好。我使用以下代码创建了一个 webhook:
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 中。当我运行 Web 应用程序时,一切正常。但是在 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 防火墙,并且没有运行任何其他可能干扰的程序。有什么帮助吗?
【问题讨论】:
-
我不太了解 .NET 路由,但您是否不需要在
public class声明上方添加类似[Route("[controller]")]的内容来指示代码的 URL/路径?如果您将简单的日志记录添加到函数的开头并通过 curl 尝试一个简单的 POST 请求,例如curl -v -X POST http://localhost:44357/spdata它会记录,还是会出现错误/意外行为/没有记录? -
这就是你在做 MVC 时使用的东西,但事实并非如此。 (微笑)代码在几个地方有一个“WriteFile”调用,它使用自定义函数写入文本文件,它甚至没有命中该代码。几乎就像它默默地失败了,直到或除非我能让 Stripe CLI 工作,我无法调试它。最大的问题是,为什么 Stripe CLI 不断抛出所有“未能发布”消息?这就是我希望知道 Stripe 的人进来的地方!
-
通常你从 Stripe CLI 得到的错误表明你的服务器代码拒绝连接。这就是为什么我建议使用
curl测试,看看它是否也被拒绝,或者它是否是 Stripe CLI 特有的。
标签: c# asp.net stripe-payments webhooks