我在 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 给了我一个 key ,我将它复制到了 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 防火墙,并且没有运行任何其他可能干扰的程序。有什么帮助吗?
最佳答案
我可能会迟到,但是从这里改变这个
stripe listen --forward-to http://localhost:44357/spdata
到这里
stripe listen --forward-to https://localhost:44357/spdata
Stripe 只会在 https 上发布。
关于c# - 让 Stripe webhook 在 Stripe CLI 中工作时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67895011/