条纹;使用新订阅创建客户时如何获取订阅 ID

时间:2021-04-21 03:03:36

标签: c# .net stripe-payments

我正在创建一个新客户并将他们添加到一个订阅中,如下所示:

StripeConfiguration.SetApiKey(StripeData.ApiKey);
var customerService = new CustomerService();
var myCustomer = new CustomerCreateOptions
    {
         Email = stripeEmail,
         Source = stripeToken,
         Plan = StripeData.MonthlySubscriptionPlanId
     };
Customer stripeCustomer = customerService.Create(myCustomer);

然后我曾经能够做到这一点:

myLocalUser.StripeCustomerId = stripeCustomer.Id;
myLocalUser.StripeSubscriptionId = stripeCustomer.Subscriptions.Data[0]?.Id;

但现在 API 没有返回客户的订阅,所以第二行失败

2 个答案:

答案 0 :(得分:0)

我现在不得不用这段丑陋的代码再次调用 API 来获取客户的订阅 ID:

                    if (stripeCustomer.Subscriptions != null)
                    {
                        user.StripeSubscriptionId = stripeCustomer.Subscriptions.Data[0]?.Id;
                        
                    }
                    else
                    {
                        //get subscriptionId
                        var cust = customerService.Get(stripeCustomer.Id, new CustomerGetOptions
                        {
                            Expand = new System.Collections.Generic.List<string> { "subscriptions" }
                        });
                        if (cust.Subscriptions.Any())
                        {
                            stripeSubscriptionId = cust.Subscriptions.First().Id;
                        }
                    }

CustomerService.Create() 没有与 Get() 方法相同的 Expand 参数选项...

答案 1 :(得分:0)

这是意料之中的,因为订阅在客户对象上是 no longer included by default,除非您从 API version 2020-08-27 开始扩展它们。

创建具有来源和计划的客户仍然是可能的(尽管不再是推荐的集成路径,因为您可能会遇到 3DS 和税率问题),尽管由于您使用的是较新的 API 版本,因此您将无法获得subscriptions 列表回来。如果可以,您应该更新为创建订阅 via their own API

如果您仍然想使用这个旧的集成路径,您仍然可以在客户创建调用中取回订阅,您只需要在创建时扩展订阅:

var customerService = new CustomerService();
var myCustomer = new CustomerCreateOptions
{
  Email = stripeEmail,
  Source = stripeToken,
  Plan = StripeData.MonthlySubscriptionPlanId
};
myCustomer.AddExpand("subscriptions");
Customer stripeCustomer = customerService.Create(myCustomer);