C#实现Stripe订阅结账时为商品添加客户名称描述的问题
实现方案
核心逻辑是将原代码中直接引用priceId的写法替换为使用PriceData参数内联定义价格信息,在ProductData的Name字段中填入你需要展示的客户名称+商品描述即可,该内容会同步显示在结账页面和后续的账单门户中。
修改后完整代码示例
var options = new Stripe.Checkout.SessionCreateOptions { LineItems = new List<SessionLineItemOptions> { new SessionLineItemOptions { // 替换原有直接引用PriceId的写法 PriceData = new SessionLineItemPriceDataOptions { Currency = "cny", // 替换为你实际使用的货币编码,如usd/eur等 ProductData = new SessionLineItemPriceDataProductDataOptions { // 此处自定义拼接要展示的描述,可直接填入客户名称 Name = $"企业订阅服务 - {客户名称变量}" }, // 替换为你实际的单价,单位为对应货币的最小单位,如人民币为分,199元即19900 UnitAmountDecimal = 19900, Recurring = new SessionLineItemPriceDataRecurringOptions { // 替换为你实际的订阅周期,可选值:day/week/month/year Interval = "month" } }, Quantity = 1 }, }, PaymentMethodTypes = new List<string> { "card", }, Mode = "subscription", SuccessUrl = string.Concat("https://", domain, "/success/", hhid, "?session_id={CHECKOUT_SESSION_ID}"), CancelUrl = string.Concat("https://", domain), Customer = customerId, SubscriptionData = new SessionSubscriptionDataOptions() { Metadata = new Dictionary<string, string>() { { "userId", userId.ToString() } } } }; if (!string.IsNullOrEmpty(user.PaymentCustomerId)) options.Customer = user.PaymentCustomerId; var service = new Stripe.Checkout.SessionService(); Stripe.Checkout.Session session = await service.CreateAsync(options); Response.Headers.Add("Location", session.Url);
注意事项
- 如果你不想硬编码价格、周期等配置,可以先调用
Stripe.PriceService.GetAsync(priceId)获取你原有Price的配置信息,直接复用返回结果中的UnitAmountDecimal、Currency、Recurring.Interval等字段,避免和原有价格配置出现不一致。 ProductData.Name支持任意自定义内容,你可以按照业务需求组合商品名、客户名称、订单编号等信息,所有内容都会完整展示在账单中。- 内联的
PriceData仅作用于当前会话生成的订阅和账单,不会在你的Stripe后台创建额外的Price/Product记录,也不会影响原有配置。
内容的提问来源于stack exchange,提问作者USMC6072
相关产品推荐
相关产品推荐

