Stripe v27中ChargeCreateOptions无SourceId定义的报错求助
解决Stripe v27中'ChargeCreateOptions'无'SourceId'定义的报错
这个问题是因为Stripe .NET SDK v27版本做了API适配更新——ChargeCreateOptions里的SourceId属性已经被移除了,取而代之的是更符合当前Stripe API规范的PaymentMethodId(或者兼容旧场景的Source属性)。
原因说明
Stripe已经逐步淘汰了旧的Source API,转而推广功能更全面的Payment Method API,所以SDK跟着移除了SourceId这个旧属性,引导开发者使用新的支付方式关联方式。
修改后的代码
如果你的前端传递的tokenId是Stripe生成的支付方式令牌(比如tok_xxx格式),直接替换成PaymentMethodId即可:
public IActionResult Processing(StripePaymentRequest paymentRequest) { Dictionary<string, string> Metadata = new Dictionary<string, string>(); Metadata.Add("Product", "RubberDuck"); Metadata.Add("Quantity", "10"); var options = new ChargeCreateOptions { Amount = 100, Currency = "USD", Description = "Buying 10 rubber ducks", // 替换原SourceId为PaymentMethodId PaymentMethodId = paymentRequest.tokenId, Metadata = Metadata }; var service = new ChargeService(); Charge charge = service.Create(options); return Json(charge); }
如果你的tokenId是旧的Source令牌(比如src_xxx格式),可以使用兼容的Source属性:
// 替换为以下代码 Source = paymentRequest.tokenId,
额外建议
如果你的前端还在使用旧版Stripe.js生成Source Token,建议尽快迁移到生成Payment Method,这样能更好地适配Stripe的最新功能,避免后续版本再出现类似的兼容性问题。
内容的提问来源于stack exchange,提问作者Truc
相关产品推荐
相关产品推荐

