【问题标题】:Stripe Webhook Controller Not Found未找到条纹 Webhook 控制器
【发布时间】:2017-09-25 23:37:22
【问题描述】:

我有以下:

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


}

并且来自条带的请求会产生错误:

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

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

【问题讨论】:

  • 不应该继承ApiController而不是Controller吗?
  • 那是什么意思?如果这是 WebAPI 2,它必须从 ApiController 继承:docs.microsoft.com/en-us/aspnet/web-api/overview/… 或者 COntroller 类是 Stripe 框架中的某个类?
  • @Gusman 你看到链接了吗?这就是作者建议实现 webhook 接收器的方式,这就是我上面的做法,但它不起作用。据我所知,WebApi 请求无论如何都无法访问 InputStream。
  • 是的,这对于 WebApi 2 是不正确的,作者甚至告诉他不记得怎么做,因为他很久以前不使用 asp .net,它的答案是 asp .net mvc ,不适用于 WebApi。尝试从 ApiController 继承并检查它。

标签: c# .net asp.net-mvc asp.net-web-api stripe-payments


【解决方案1】:

使用 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();
            }
        }
    }

如果您从 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();
            }
        }
    }

【讨论】:

  • 这是 WebApi2 的一个很好的答案,我唯一要提到的是使用 StripeEventUtility.ConstructEvent 而不是 ParseEvent 并根据官方 Stripe 文档验证 Stripe-Signature Header。 var stripeEvent = StripeEventUtility.ConstructEvent(json, HttpContext.Current.Request.Headers["Stripe-Signature"],ConfigurationManager.AppSettings["Stripe.WebhookSecret"]);stripe.com/docs/webhooks#verify-official-libraries
猜你喜欢
  • 2016-03-07
  • 2016-09-12
  • 1970-01-01
  • 2012-03-26
  • 2015-10-07
  • 2013-12-02
  • 2016-09-17
  • 2013-11-06
  • 1970-01-01
相关资源
最近更新 更多