【问题标题】:StripeException: No Such PlanStripeException:没有这样的计划
【发布时间】:2019-06-02 09:32:20
【问题描述】:

我正在创建一个客户对象并将它们分配给 Stripe 中的一个计划,并收到错误消息“不存在这样的计划”。错误中给出的计划 ID 是正确的计划 ID:No such plan: prod_EIcYiWkVa7LF7T

值得注意的是,客户的 StripeCustomerId 也没有写入数据库,但这可能是因为代码稍后失败,因此没有进行任何更改。

 [HttpPost]
        [Authorize]
        public ActionResult Subscribe(SubscribeViewModel model)
        {

            string CurrentUserId = User.Identity.GetUserId();
            var CurrentUser = UserManager.FindById(CurrentUserId);


            StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripeSecretKey"]);

            var custoptions = new CustomerCreateOptions
            {
                Description = "Customer object for " + CurrentUser.Email,
                SourceToken = model.StripeToken
            };

            var custservice = new CustomerService();
            Customer customer = custservice.Create(custoptions);

            CurrentUser.StripeCustomerId = customer.Id;

            var items = new List<SubscriptionItemOption>
            {
                new SubscriptionItemOption
                {
                    PlanId = db.Plans.Where(a=>a.Id == CurrentUser.Plan).FirstOrDefault().StripePlanId
                }
            };
            var options = new SubscriptionCreateOptions
            {
                CustomerId = CurrentUser.StripeCustomerId,
                Items = items
            };

            var service = new SubscriptionService();
            Subscription subscription = service.Create(options);


            CurrentUser.PlanStatus = "TRIAL";
            CurrentUser.ExpirationDate = DateTime.Now.AddDays(model.Plan.TrialDays);
            var Plan = db.Plans.Where(a => a.Id == CurrentUser.Plan).FirstOrDefault();
            return RedirectToAction("Index", "Home");
        }

【问题讨论】:

  • prod_EIcYiWkVa7LF7T 是产品 ID。您需要该产品的定价计划的plan_xxx ID。 stripe.com/docs/billing/subscriptions/products-and-plans
  • 啊,完美。那解决了它。您知道为什么根据上面的代码,StripeCustomerId 不会被写入数据库吗?是否与 API 调用的时间有关?
  • 您是否保存或使用CurrentUser 对象?它似乎不是一个明确的 save 函数在任何地方调用它(虽然我不是 ASP 程序员,但希望在代码中看到类似的东西)

标签: c# asp.net-mvc stripe-payments


【解决方案1】:

这是作为评论发布在上面的,但我将其添加为向@karllekko 道歉的答案,因为我几乎没有阅读它就通过它。

您想使用计划 ID plan_xxxxx,而不是产品 ID prod_xxxxx。我犯了同样的错误,即创建多个产品而不是一个具有多个价格计划的产品。

创建一个产品,然后为每个产品设置多个计划,为您的“Gold”、“Silver”、“Bronze”等定价,并​​在您的代码中使用这些 plan_xxxxx ID 来创建订阅。

【讨论】:

    【解决方案2】:

    请使用价格 API Id 而不是上面的 prod_ID。

    如图所示,它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 2015-02-06
      相关资源
      最近更新 更多