Example #1
0
        public ActionResult ChangeSubscription(ChangeSubscriptionModel model)
        {
            if (ModelState.IsValid)
            {
                ChangeSubscriptionRequest request = new ChangeSubscriptionRequest
                {
                    CustomerId = Customer.CustomerId,
                    ChangeSubscriptionModel = model,
                    PlanId = model.PlanId
                };
                ChangeSubscriptionResponse response = _subscriptionsService.ChangeSubscription(request);

                if (response.IsStatusActive)
                {
                    return(RedirectToAction("index", "subscriptions").AndAlert(AlertType.Danger, "Error.",
                                                                               "Previous subscription wasn't cancelled."));
                }

                if (response.ErrorCode == ErrorCode.IncorrectPlanIdentifier)
                {
                    throw new Exception(response.ErrorCode.ToString());
                }

                if (response.Result != "active")
                {
                    return(RedirectToAction("index", "subscriptions")
                           .AndAlert(AlertType.Danger, "Error.", response.Message));
                }

                return(RedirectToAction("index").AndAlert(AlertType.Success, "Changed.",
                                                          "The subscription was changed successfully."));
            }

            model.Plans = _planService.List();
            return(View("ChangeSubscription", model));
        }
Example #2
0
        public ChangeSubscriptionResponse ChangeSubscription(ChangeSubscriptionRequest request)
        {
            var response = new ChangeSubscriptionResponse();

            try
            {
                Customer customer = _customerRepository.Get(request.CustomerId);

                if (customer.Plan != null)
                {
                    var status = StripeFactory.GetStripeService().CloseCustomerPlan(customer.PaymentCustomerId);
                    if (status.ToLower() == "active")
                    {
                        response.IsStatusActive = true;
                        return(response);
                    }
                }

                Plan plan =
                    _planRepository.Query().SingleOrDefault(p => p.PlanCode.ToLower() == request.PlanId.ToLower());

                if (plan == null)
                {
                    response.ErrorCode = ErrorCode.IncorrectPlanIdentifier;
                    return(response);
                }

                string result = StripeFactory.GetStripeService().AssignCustomerPlan(customer.PaymentCustomerId,
                                                                                    request.ChangeSubscriptionModel.PlanId,
                                                                                    request.ChangeSubscriptionModel.CardNumber,
                                                                                    request.ChangeSubscriptionModel.SecurityCode,
                                                                                    request.ChangeSubscriptionModel.ExpirationMonth,
                                                                                    request.ChangeSubscriptionModel.ExpirationYear);


                if (result != "active")
                {
                    response.Result = result;
                    return(response);
                }

                customer.UpdatePlan(plan);

                _unitOfWork.Commit();
            }
            catch (StripeException ex)
            {
                LoggingFactory.GetLogger().LogError(ex);
                response.HasError  = true;
                response.ErrorCode = ErrorCode.StripeSetupError;
                response.Message   = ex.Message;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().LogError(ex);
                response.HasError  = true;
                response.ErrorCode = ErrorCode.StripeSetupError;
                response.Message   = ErrorCode.StripeSetupError.ToString();
            }


            return(response);
        }