我正在创建客户对象并将其分配给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");
}