You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET WebForms中无法从Stripe Checkout Session获取Customer求助

解决Stripe Checkout.Session.Completed事件无法获取Customer的问题

1. 用Stripe官方方法解析Webhook事件

别手动反序列化JSON,必须用Stripe.NET提供的EventUtility.ParseEvent方法解析请求体,不然会因为Stripe对象的嵌套结构、属性映射规则问题导致字段丢失。

示例正确解析代码:

using Stripe;

public void ProcessStripeWebhook()
{
    var json = new StreamReader(Request.InputStream).ReadToEnd();
    var stripeEvent = EventUtility.ParseEvent(json);

    if (stripeEvent.Type == Events.CheckoutSessionCompleted)
    {
        var session = stripeEvent.Data.Object as CheckoutSession;
        // 按Stripe.NET版本选择属性:v40+用CustomerId,旧版本用Customer.Id
        var customerId = session.CustomerId; 
        if (!string.IsNullOrEmpty(customerId))
        {
            // 如需完整Customer对象,调用API获取
            var customerService = new CustomerService();
            var customer = customerService.Get(customerId);
        }
    }
}

2. 匹配Stripe.NET版本的属性规则

  • 若是Stripe.NET v40及以上,CheckoutSession的Customer ID直接通过CustomerId属性获取(字符串类型,返回cus_xxx格式);
  • 若是v39及以下版本,Customer属性默认不会自动展开,需要在创建Checkout Session时指定Expand参数,让Stripe返回完整Customer数据:
var options = new SessionCreateOptions
{
    PaymentMethodTypes = new List<string> { "card" },
    LineItems = new List<SessionLineItemOptions> { /* 你的商品配置 */ },
    Mode = "payment",
    SuccessUrl = "https://yourdomain.com/success",
    CancelUrl = "https://yourdomain.com/cancel",
    Expand = new List<string> { "customer" } // 新增此行展开Customer字段
};

var service = new SessionService();
Session session = service.Create(options);

3. 验证Webhook事件的完整结构

去Stripe Dashboard的Webhook日志里查看事件的完整payload,确认data.object.customer字段确实存在,排除因为Webhook配置过滤字段的问题。

4. 手动解析JSON的正确姿势(不推荐)

如果非要手动处理JSON,要确保正确嵌套访问字段:

using Newtonsoft.Json.Linq;

var jsonObj = JObject.Parse(json);
var customerId = jsonObj["data"]["object"]["customer"]?.ToString();

内容的提问来源于stack exchange,提问作者TC_Guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:33:18