【问题标题】:Stripe; how to get subscriptionId when creating a customer with a new subscription条纹;使用新订阅创建客户时如何获取订阅 ID
【发布时间】:2021-07-15 05:32:15
【问题描述】:

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

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 没有返回客户的订阅,所以第二行失败

【问题讨论】:

    标签: c# .net stripe-payments


    【解决方案1】:

    我现在不得不用这段丑陋的代码再次调用 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 参数选项...

    【讨论】:

      【解决方案2】:

      这是意料之中的,因为订阅是客户对象上的 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 2017-08-28
        • 1970-01-01
        • 2017-11-18
        • 2017-04-07
        • 2013-08-22
        相关资源
        最近更新 更多