未找到 Stripe Webhook 控制器

SB2*_*055 3 .net c# asp.net-mvc asp.net-web-api stripe-payments

我有以下内容:

public class StripeController : Controller
{

    private readonly UserService _userService;

    public StripeController(UserService userService)
    {
        _userService = userService;
    }

    [HttpPost]

    public ActionResult StripeWebook()
    {
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }


    [HttpPost]
    [Route("api/stripewebhook")]
    public async Task<ActionResult> Index(CancellationToken ct)
    {
        var json = new StreamReader(Request.InputStream).ReadToEnd();

        var stripeEvent = StripeEventUtility.ParseEvent(json);

        switch (stripeEvent.Type)
        {
            case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
                var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
                break;
        }

        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }


}
Run Code Online (Sandbox Code Playgroud)

来自 stripe 的请求会生成错误:

路径“/api/stripewebhook”的控制器未找到或未实现 IController

知道当我从条带门户进行测试时为什么会发生这种情况吗?

Gus*_*man 5

使用 WebApi 2 可以毫无问题地工作。

这是最小的 WebApi 控制器:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;

    namespace WebApplication1.Controllers
    {
        public class StripeController : ApiController
        {
            [HttpPost]
            [Route("api/stripewebhook")]
            public IHttpActionResult Index()
            {
                var json = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
                return Ok();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果你从 VS 执行它,你可以从http://localhost:(port)/api/stripewebhook访问它

现在您只需扩展它以包含条带代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;

    namespace WebApplication1.Controllers
    {
        public class StripeController : ApiController
        {
            [HttpPost]
            [Route("api/stripewebhook")]
            public IHttpActionResult Index()
            {
                var json = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();

                var stripeEvent = StripeEventUtility.ParseEvent(json);

                switch (stripeEvent.Type)
                {
                    case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
                        var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
                        break;
                }

                return Ok();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这是 WebApi2 的一个很好的答案,我唯一要提到的是使用 StripeEventUtility.ConstructEvent 而不是 ParseEvent 并根据官方 Stripe 文档验证 Stripe-Signature 标头。`var stripeEvent = StripeEventUtility.ConstructEvent(json, HttpContext.Current.Request.Headers["Stripe-Signature"],ConfigurationManager.AppSettings["Stripe.WebhookSecret"]);` https://stripe.com/docs/webhooks#验证官方图书馆 (2认同)