我正在创建一个客户对象并将其分配给 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");
}
这是作为评论发布在上面的,但我将其添加为答案,并向@karllekko表示歉意,因为我几乎没有阅读它就跳过了它。
您想要使用计划 ID,plan_xxxxx
,而不是产品 ID,prod_xxxxx
。我犯了同样的错误,创建了多种产品,而不是一种具有多种价格计划的产品。
创建一个产品,然后为每个产品设置多个计划,为您的“金牌”、“银牌”、“铜牌”等定价并使用这些计划plan_xxxxx
代码中用于创建订阅的 ID。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)