【问题标题】:Stripe webhook test throws error in controllerStripe webhook 测试在控制器中引发错误
【发布时间】:2022-01-17 14:15:26
【问题描述】:

System.NullReferenceException:对象引用未设置为对象的实例

在 D:\Projects\xxxxxxx\Controllers\ApiController.cs:line 60 中的 xxxxx.Controllers.ApiController.Index() 处

我正在使用条带向端点发送测试 api 请求,结果为 500 任何帮助表示赞赏。

如果您有任何进一步的信息,请不要犹豫。

  using Microsoft.AspNetCore.Mvc;
  using System;
  using System.Threading.Tasks;
  using System.IO;
  using Stripe;
  using xxxxx.Services.Interfaces;
  using Microsoft.Extensions.Configuration;
  using System.Diagnostics;
  using xxxxxx.Models;

 namespace xxxxxxx.Controllers
{
[ApiController]
[Route("api/webhook")]    
public class ApiController : ControllerBase
{
    private readonly IConfiguration _config;
    private readonly IWebhookEventRepository _webhookEventRepository;

    public ApiController(IConfiguration config, 
        IWebhookEventRepository webhookEventRepository)
    {
        _config = config;
        _webhookEventRepository = webhookEventRepository;
    }

    [BindProperty]
    public WebhookEvent WebhookEvent { get; set; }

    [HttpPost]
    public async Task<IActionResult> Index()
    {
        var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

        Event stripeEvent;
        var KeyValue = _config.GetValue<string>("Stripe:WebhookSecret");            

        try
        {
            stripeEvent = EventUtility.ParseEvent(json);
            var signatureHeader = Request.Headers["Stripe-Signature"];
            stripeEvent = EventUtility.ConstructEvent(json,
            signatureHeader, KeyValue);

            Console.WriteLine($"Webhook notification with type: {stripeEvent.Type} found 
   for {stripeEvent.Id}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Something failed {e}");
            return BadRequest();
        }

        try
        {
            switch (stripeEvent.Type)
            {
                case Events.CustomerSubscriptionCreated:
                    var subscription = stripeEvent.Data.Object as Subscription;

           *LINE 60*        WebhookEvent = new WebhookEvent()
                    {
                        EventType = subscription.Object,
                        Customer = subscription.Customer.ToString(),
                        CustomerId = subscription.CustomerId,
                        Created = subscription.Created,
                        Cancelled = subscription.CancelAt
                    };

                    await _webhookEventRepository.Add(WebhookEvent);                          
                    break;

                case Events.CustomerSubscriptionUpdated:
                    var update = stripeEvent.Data.Object as Subscription;
                    break;

                case Events.CustomerCreated:
                    var customer = stripeEvent.Data.Object as Stripe.Customer;
                    break;
                default:
                    Debug.WriteLine("Default case");
                    break;
            }

            return Ok();

        }
        catch (StripeException e)
        {
            Console.WriteLine("Error: {0}", e.Message);
            return BadRequest();
        }
    }
}
}

【问题讨论】:

  • 这是第 60 行 WebhookEvent = new WebhookEvent()

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


【解决方案1】:

您确定这是您遇到错误的那一行吗?哪个对象意外为空?

在看到你的行标签之前,我怀疑这可能是与签名验证相关的问题的根源:

stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, KeyValue);

ConstructEvent 必须提供原始请求正文,并且调用 ParseEvent 的前一行可能会改变阅读器,因此您无法在此处使用它。

我建议:

  1. 尝试删除对ParseEvent 的多余调用——你不应该这样做 两者都需要
  2. 准确调试错误的来源并添加 如果这不允许您弄清楚,请详细说明您的问题 出去。

【讨论】:

  • 谢谢,我今天会检查一下,我认为您可能是正确的。抱歉,我看到的问题质量已被否决。未来我会努力做得更好。
猜你喜欢
  • 2020-11-18
  • 2021-04-03
  • 2023-03-08
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 2020-08-03
  • 2016-11-10
相关资源
最近更新 更多