.NET Core中Stripe.net PaymentMethodGetOptions无成员及选项缺失问题
解决Stripe .NET库中PaymentMethodGetOptions无法添加参数的问题
问题背景
我在.NET Core环境中尝试通过以下代码获取支付方式:
var service = new PaymentMethodService(); return await service.GetAsync(paymentMethodId);
我希望在PaymentMethodGetOptions中添加Customer和Type参数,以适配service.Get()或service.GetAsync()方法的调用需求,但发现PaymentMethodGetOptions类是空的。另外,Stripe文档中提到的PaymentMethodRetreiveOptions在该库中并不存在。
解决方案
1. 更新Stripe.NET包版本
Stripe的.NET库会与官方API版本同步,旧版本可能未支持所需参数的传递。先更新到最新稳定版本:
dotnet add package Stripe.net --version <最新稳定版号>
2. 使用AddExtraParam传递额外参数
如果更新后PaymentMethodGetOptions仍未包含Customer和Type字段,可以利用库的通用扩展方法传递API支持的参数:
var options = new PaymentMethodGetOptions(); options.AddExtraParam("customer", "cus_你的客户ID"); options.AddExtraParam("type", "card"); // 或其他支付方式类型 var service = new PaymentMethodService(); return await service.GetAsync(paymentMethodId, options);
3. 直接构造API请求(备选)
若上述方法不适用,可通过基础客户端直接调用Stripe API:
var client = new StripeClient("你的Stripe API密钥"); var response = await client.GetAsync($"/v1/payment_methods/{paymentMethodId}", new Dictionary<string, string> { {"customer", "cus_你的客户ID"}, {"type", "card"} }); var paymentMethod = Newtonsoft.Json.JsonConvert.DeserializeObject<Stripe.PaymentMethod>(response.Content);
补充说明
Stripe文档中的参数类名称有时会与库内实际命名存在差异(比如文档中的PaymentMethodRetreiveOptions对应库内的PaymentMethodGetOptions)。当参数类未定义所需字段时,AddExtraParam是官方推荐的兼容方式,可传递所有API支持的额外参数。
内容的提问来源于stack exchange,提问作者Shant Hagopian
相关产品推荐
相关产品推荐

