Tam*_*ver 7 c# asp.net webhooks stripe-payments
我在 ASP.NET Web 表单应用程序中使用 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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我在 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.
Run Code Online (Sandbox Code Playgroud)
我不知道错误的根源是什么。我关闭了 Windows 防火墙,并且没有运行任何其他可能会干扰的程序。有什么帮助吗?
我可能会迟到,但是改变这个
stripe listen --forward-to http://localhost:44357/spdata
Run Code Online (Sandbox Code Playgroud)
对此
stripe listen --forward-to https://localhost:44357/spdata
Run Code Online (Sandbox Code Playgroud)
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 版本?)
| 归档时间: |
|
| 查看次数: |
6855 次 |
| 最近记录: |