private SelectList GetPlans() { var availablePlans = StripeFactory.GetStripeService().GetAvailablePlans().Select(p => new CompactPlan(p)).ToList(); return(new SelectList(availablePlans, "PlanId", "PlanName", 1)); }
// look up customer in Stripe to validate the request public bool ValidateCustomer(string customerId) { StripeCustomer stripeCustomer = StripeFactory.GetStripeService().GetCustomer(customerId); if (stripeCustomer != null) { return(true); } return(false); }
public string ClosePlan(Guid customerId) { Customer customer = _customerRepository.Get(customerId); var status = StripeFactory.GetStripeService().CloseCustomerPlan(customer.PaymentCustomerId); customer.RemovePlan(); _customerRepository.Update(customer); _unitOfWork.Commit(); return(status); }
public RefundModel GetRefundPayment(Guid customerId, string paymentId) { //Customer customer = _customerRepository.Get(customerId); var payment = StripeFactory.GetStripeService().GetCustomerPayment(paymentId); var viewModel = new RefundModel { CustomerId = customerId, PaymentId = paymentId, Amount = GetRemainingAmount( payment.Amount, payment.AmountRefunded), }; return(viewModel); }
public Guid Provision(CustomersProvisionModel model) { var fullName = model.FirstName + " " + model.LastName; //create customer in stripe StripeCustomer stripeCustomer = StripeFactory.GetStripeService().CreateCustomer(fullName, model.Email); // Customer Customer customer = new Customer(fullName, model.Domain, model.Company, stripeCustomer.Id) { PlanCreatedOn = stripeCustomer.Created }; _customerRepository.Add(customer); // roles var role = new Role(customer, "Administrator", true, UserType.SystemUser); _roleRepository.Add(role); // users var user = new User(customer, model.Email, model.FirstName, model.LastName, model.Password, true); _userRepository.Add(user); role.AddUser(user); customer.UpdateAdminUser(user); // security questions ReferenceList referenceList = _referenceListRepository.Query().SingleOrDefault(l => l.SystemName == "Security Questions"); foreach (var securityQuestion in SecurityQuestions.Questions) { referenceList.AddItem(customer, securityQuestion); } // user security questions user.AddSecurityQuestion("a", "a"); user.AddSecurityQuestion("b", "b"); user.AddSecurityQuestion("c", "c"); _unitOfWork.Commit(); return(customer.Id); }
public CustomerPaymentsModel Payments(Guid id) { Customer customer = _customerRepository.Get(id); var model = new CustomerPaymentsModel { CustomerId = customer.Id, CustomerName = customer.FullName, CustomerEmail = customer.AdminUser.Email, PaymentCustomerId = customer.PaymentCustomerId, CustomerPlan = (!string.IsNullOrEmpty(customer.PaymentCustomerId)) ? StripeFactory.GetStripeService().GetCustomerPlan(customer.PaymentCustomerId) : null, StripeChargesList = (!string.IsNullOrEmpty(customer.PaymentCustomerId)) ? StripeFactory.GetStripeService().GetCustomerPayments(10, customer.PaymentCustomerId) : null }; return(model); }
public CancelSubscriptionResponse CancelSubscription(Guid customerId) { var response = new CancelSubscriptionResponse(); var customer = _customerRepository.Get(customerId); if (customer.Plan != null) { var status = StripeFactory.GetStripeService().CloseCustomerPlan(customer.PaymentCustomerId); if (status.ToLower() == "active") { response.IsStatusActive = true; return(response); } customer.RemovePlan(); _customerRepository.Update(customer); _unitOfWork.Commit(); } return(response); }
public SetupStripeResponse SetupStripe(Guid customerId) { SetupStripeResponse response = new SetupStripeResponse(); Customer customerObj = _customerRepository.Get(customerId); try { var customer = StripeFactory.GetStripeService().CreateCustomer(customerObj.FullName, customerObj.AdminUser.Email); customerObj.UpdateStripe(customer.Id); _customerRepository.Update(customerObj); _unitOfWork.Commit(); } catch { response.HasError = true; response.ErrorCode = ErrorCode.StripeSetupError; } return(response); }
public static void ClassInitialize(TestContext context) { ClassInitialize(); var dbContext = Container.GetInstance <EFDbContext>(); _customer = new Customer("John", "john-domain", "Join-LTD"); // Create customer in stripe var stripeCustomer = StripeFactory.GetStripeService().CreateCustomer("John Smith", "*****@*****.**"); _customer.PaymentCustomerId = stripeCustomer.Id; dbContext.Customers.Add(_customer); dbContext.SaveChanges(); var user = new User(_customer, "*****@*****.**", "John", "Smith", "Qwerty123"); dbContext.Users.Add(user); _customer.AdminUser = user; dbContext.Customers.Attach(_customer); dbContext.SaveChanges(); }
public ActionResult RefundPayment(RefundModel refundModel) { if (ModelState.IsValid) { try { CustomersDetailsModel customer = _customerService.GetCustomerDetails(refundModel.CustomerId); if (customer == null) { return(HttpNotFound()); } var payment = StripeFactory.GetStripeService().GetCustomerPayment(refundModel.PaymentId); var remainingAmount = GetRemainingAmount( payment.Amount, payment.AmountRefunded); if (refundModel.Amount > remainingAmount) { return(RedirectToAction("payments", "customers", new { id = refundModel.CustomerId }) .AndAlert(AlertType.Danger, "Error.", "Provided data is incorrect.")); } StripeFactory.GetStripeService().Refund(refundModel.PaymentId, refundModel.AmountCents); } catch { return(RedirectToAction("payments", "customers", new { id = refundModel.CustomerId }) .AndAlert(AlertType.Danger, "Refund error.", "Error was thrown during refunding payment.")); } return(RedirectToAction("payments", "customers", new { id = refundModel.CustomerId }) .AndAlert(AlertType.Success, "Refund.", "Payment was refunded sucessfully.")); } return(RedirectToAction("payments", "customers", new { id = refundModel.CustomerId }) .AndAlert(AlertType.Danger, "Error.", "Provided data is incorrect.")); }
public SetupPaymentPlanReponse SetupPaymentPlan(PlanSetupModel planModel) { var response = new SetupPaymentPlanReponse(); try { Customer customer = _customerRepository.Get(Guid.Parse(planModel.CustomerId)); if (customer == null) { throw new Exception("Incorrect Customer identifier"); } if (customer.Plan != null) { response.HasPlan = true; var status = StripeFactory.GetStripeService().CloseCustomerPlan(customer.PaymentCustomerId); response.Status = status; //return response; } Plan dbPlan = _planRepository.Query().FirstOrDefault(p => p.PlanCode.ToLower() == planModel.StripePlanId.ToLower()); if (dbPlan == null) { throw new Exception("Incorrect plan identifier"); } var result = StripeFactory.GetStripeService().AssignCustomerPlan( planModel.PaymentCustomerId, planModel.StripePlanId, planModel.CreditCardNumber, planModel.Cvc, planModel.CardExpirationMonth, planModel.CardExpirationYear); if (result != "active") { response.IsStatusActive = false; return(response); } else { //dbPlan.Customers. response.IsStatusActive = true; } customer.UpdatePlan(dbPlan); _unitOfWork.Commit(); return(response); } 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); }
public void Delete(Guid id) { Customer customer = _customerRepository.Get(id); // Remove Customer from stripe service if (!string.IsNullOrEmpty(customer.PaymentCustomerId)) { try { if (StripeFactory.GetStripeService().GetCustomer(customer.PaymentCustomerId) != null) { StripeFactory.GetStripeService().DeleteCustomer(customer.PaymentCustomerId); } LoggingFactory.GetLogger().Log("Customer not found", EventLogSeverity.Fatal); }catch (Exception ex) { LoggingFactory.GetLogger().LogError(ex); } } _unitOfWork.Begin(); // Delete relations between users and roles foreach (var role in customer.Roles) { role.RoleUsers.Clear(); _roleRepository.Update(role); } // Delete Notes if (customer.Notes != null) { foreach (var note in customer.Notes.ToList()) { _noteRepository.Delete(note); } } // Delete API records if (customer.ApiTokens != null) { foreach (var apiToken in customer.ApiTokens.ToList()) { _apitokensRepository.Delete(apiToken); } } // Delete IPS records if (customer.IPSEntries != null) { foreach (var ipsEntry in customer.IPSEntries.ToList()) { _ipsEntryRepository.Delete(ipsEntry); } } //delete all reference list items if (customer.ReferenceListItems != null) { foreach (var item in customer.ReferenceListItems.ToList()) { _referenceListItemRepository.Delete(item); } } // delete all users if (customer.Users != null) { foreach (var user in customer.Users.ToList()) { _userRepository.Delete(user); } } //// delete all payments //foreach (var payment in _customerPaymentRepository.Query().Where(x => x.CustomerId == id).ToList()) //{ // _customerPaymentRepository.Delete(payment); //} //// delete all payment refunds //foreach (var paymentRefund in _customerPaymentRefundRepository.Query().Where(x => x.CustomerId == id).ToList()) //{ // _customerPaymentRefundRepository.Delete(paymentRefund); //} _customerRepository.Delete(customer); _unitOfWork.End(); }
/// <summary> /// Sign up a user to a plan /// </summary> /// <param name="request">Sign up request</param> public PlanSignUpResponse PlanSignUp(PlanSignUpRequest request) { try { // Begin transaction _unitOfWork.Begin("PlanSignUp"); var response = new PlanSignUpResponse(); var dbPlan = _planRepository.Query().FirstOrDefault(p => p.PlanCode.ToLower() == request.PlanName.ToLower()); if (dbPlan == null) { response.HasError = true; response.ErrorCode = ErrorCode.PlanNotFound; return(response); } if (request.SignUpModel.Domain != null) { if ( _customerRepository.Query().Any( t => t.Hostname.ToLower() == request.SignUpModel.Domain.ToLower())) { response.ErrorCode = ErrorCode.DomainAlreadyExists; response.HasError = true; return(response); } } // Customer var customer = new Customer(request.SignUpModel.FullName, request.SignUpModel.Domain, request.SignUpModel.Company); _customerRepository.Add(customer); // Role var role = new Role(customer, "Administrator", true, UserType.SystemUser); _roleRepository.Add(role); // Setup the User var user = new User(customer, request.SignUpModel.Email, request.SignUpModel.FirstName, request.SignUpModel.LastName, request.SignUpModel.Password); _userRepository.Add(user); role.AddUser(user); customer.UpdateAdminUser(user); // Security questions var referenceList = _referenceListRepository.Query().FirstOrDefault(l => l.SystemName == "Security Questions"); if (referenceList == null) { throw new NullReferenceException("Security questions reference list is null"); } foreach (var securityQuestion in SecurityQuestions.Questions) { referenceList.AddItem(customer, securityQuestion); } // User security questions user.AddSecurityQuestion("a", "a"); user.AddSecurityQuestion("b", "b"); user.AddSecurityQuestion("c", "c"); // Create customer in stripe var stripeCustomer = StripeFactory.GetStripeService().CreateCustomer(request.SignUpModel.FullName, request.SignUpModel.Email); customer.PaymentCustomerId = stripeCustomer.Id; // Associate plan var result = StripeFactory.GetStripeService().AssignCustomerPlan(customer.PaymentCustomerId, dbPlan.PlanCode, request.SignUpModel.CardNumber, request.SignUpModel.SecurityCode, int.Parse( request.SignUpModel.Expiration. Substring(0, 2)), int.Parse( request.SignUpModel.Expiration. Substring(3, 2))); if (result != "active") { throw new Exception($"Incorrect assigning plan result for Customer {customer.FullName}"); } customer.UpdatePlan(dbPlan); _customerRepository.Update(customer); // End transaction _unitOfWork.End(); // Send email var emailTemplate = _emailTemplatesRepository.GetTemplateByName( EmailTemplateCode.SignUpGreeting.ToDescriptionString()); emailTemplate = EmailTemplateFactory.ParseTemplate(emailTemplate, customer); var mailMessage = MailMessageMapper.ConvertToMailMessage(emailTemplate); var emailResult = EmailServiceFactory.GetEmailService().SendMail(mailMessage); if (!emailResult) { throw new Exception("Problem sending email"); } return(response); } catch (StripeException ex) { LoggingFactory.GetLogger().LogError(ex); throw new Exception("Assigning customer plan error", ex); } catch (Exception ex) { LoggingFactory.GetLogger().LogError(ex); throw; } }
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); }