StripeException:没有这样的计划

ext*_*elp 11 c# asp.net-mvc stripe-payments

我正在创建一个客户对象并将它们分配给 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");
        }
Run Code Online (Sandbox Code Playgroud)

use*_*289 5

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

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

创建单个产品,然后为每个产品为“黄金”、“白银”、“青铜”等定价设置多个计划,并plan_xxxxx在代码中使用这些ID 创建订阅。