Exemple #1
0
        public void ProcessPayment(PaymentModel model)
        {
            var user    = currentUserService.CurrentUser();
            var userDto = Context.Users.FirstOrDefault(x => x.Id == user.Id);
            var plan    = stripeService.GetPlans().FirstOrDefault(x => x.Id == model.PlanId);


            if (userDto.StripeId == null)
            {
                var customer = stripeService.CreateCustomer(model.Token.email, model.Token.id);
                userDto.StripeId = customer.Id;
                Context.SaveChanges();
            }
            else
            {
                var currentSub = stripeService.CurrentSubForCustomer(userDto.StripeId);
                if (currentSub != null)
                {
                    stripeService.CancelSub(currentSub.Id);
                }
            }
            stripeService.CreateSubscription(plan.Id, userDto.StripeId);
        }
        public UserModel CurrentUser()
        {
            var userId  = UserId();
            var userDto = Context.Users
                          .FirstOrDefault(x => x.Id == userId);
            var user = userDto.ToModel();

            if (userDto.StripeId != null)
            {
                var sub = stripeService.CurrentSubForCustomer(userDto.StripeId);
                if (sub != null)
                {
                    user.PlanNickname = sub.Plan.Nickname;
                    user.PlanId       = sub.Plan.Id;
                }
            }


            return(user);
        }