public ActionResult Cancel(int userId) { WebSite.Models.User user = _db.Users.Find(userId); DateTime subscriptionExpiryDate = DateTime.UtcNow; DateTime cancellationDate = DateTime.UtcNow; // TODO: Assert the return value if (user.Subscription != null) { // Cancel the subscription at Authorize.NET ISubscriptionGateway gateway = this.GetSubscriptionGateway(); try { gateway.CancelSubscription(user.Subscription.AuthorizeNETSubscriptionId); } catch { // The subscription may have expired or manually cancelled in which case an exception will be thrown here } // Determine the user's last day based on how much they have paid so far DateTime activationDate = user.Subscription.ActivationDate; int moduloAmount = 1; ViewBag.SubscriptionFrequency = user.Subscription.SubscriptionType.SubscriptionFrequency.Name.ToLower(); if (user.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Monthly) { moduloAmount = 30; } else if (user.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Quarterly) { moduloAmount = 365 / 4; } else if (user.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Yearly) { moduloAmount = 365; } else { // TODO: Log Error } subscriptionExpiryDate = cancellationDate.AddDays(moduloAmount - ((cancellationDate - activationDate).Days % moduloAmount)); // Mark the day the subscription is cancelled user.Subscription.CancellationDate = cancellationDate; } user.SubscriptionExpiryDate = subscriptionExpiryDate; user.Subscription = null; _db.SaveChanges(); return(this.RedirectToAction("CancelledMembers")); }
public ActionResult UpdateSubscription(CreditCard newCreditCard) { if (ModelState.IsValid) { User currentUser = Authentication.GetCurrentUserEagerlyLoaded(); // Create a new subscription with the old settings ISubscriptionRequest subscriptionRequest = CreateAuthorizeDotNetSubscriptionRequest(newCreditCard, currentUser.Subscription.SubscriptionType); ISubscriptionRequest subscriptionResponse = null; ISubscriptionGateway subscriptionGateway = GetSubscriptionGateway(); try { // Add the new subscription now subscriptionResponse = subscriptionGateway.CreateSubscription(subscriptionRequest); // Cancel the current subscription subscriptionGateway.CancelSubscription(currentUser.Subscription.AuthorizeNETSubscriptionId); // Subscription was updated successfully // Encrypt the card's number newCreditCard.Encrypt(); DatabaseContext db = _database; // Construct a subscription for the user Subscription userSubscription = new Subscription() { ActivationDate = DateTime.UtcNow, AuthorizeNETSubscriptionId = subscriptionResponse.SubscriptionID, CancellationDate = null, SubscriptionTypeId = currentUser.Subscription.SubscriptionTypeId, CreditCard = newCreditCard }; // Associate the subscription with the user currentUser.AddSubscription(userSubscription); db.SaveChanges(); return(this.RedirectToAction("Index")); } catch (Exception exception) { ModelState.AddModelError(string.Empty, exception.Message); } } ViewBag.Countries = _database.Countries; return(this.View()); }
public ActionResult CancelAutoTrading() { WebSite.Models.User currentUser = Authentication.GetCurrentUser(); Subscription autoTradingSubscription = currentUser.AutoTradingSubscription; ISubscriptionGateway gateway = GetSubscriptionGateway(); gateway.CancelSubscription(autoTradingSubscription.AuthorizeNETSubscriptionId); autoTradingSubscription.CancellationDate = DateTime.UtcNow; _database.SaveChanges(); return(View()); }
public ActionResult CancelSubscription(CancelSubscription model) { // If the user has not provided a reason for cancellation, then don't do anything if (!ModelState.IsValid) { return(this.View(model)); } WebSite.Models.User currentUser = Authentication.GetCurrentUser(); // Email the admins and let them know the reason this.EmailAdminsAboutCancellation(model.Reason, currentUser); // Cancel the subscription at Authorize.NET ISubscriptionGateway gateway = GetSubscriptionGateway(); // TODO: Assert the return value gateway.CancelSubscription(currentUser.Subscription.AuthorizeNETSubscriptionId); // Determine the user's last day based on how much they have paid so far DateTime subscriptionExpiryDate = DateTime.UtcNow; DateTime cancellationDate = DateTime.UtcNow; DateTime activationDate = currentUser.Subscription.ActivationDate; int moduloAmount = 1; ViewBag.SubscriptionFrequency = currentUser.Subscription.SubscriptionType.SubscriptionFrequency.Name.ToLower(); if (currentUser.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Monthly) { moduloAmount = 30; } else if (currentUser.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Quarterly) { moduloAmount = 365 / 4; } else if (currentUser.Subscription.SubscriptionType.SubscriptionFrequency.Name == PredefinedSubscriptionFrequencies.Yearly) { moduloAmount = 365; } else { // TODO: Log Error } subscriptionExpiryDate = cancellationDate.AddDays(moduloAmount - ((cancellationDate - activationDate).Days % moduloAmount)); // Mark the cancellation date of the user's subscription currentUser.Subscription.CancellationDate = DateTime.UtcNow; currentUser.SubscriptionExpiryDate = subscriptionExpiryDate; currentUser.Subscription = null; // Cancel any auto trading if the user has some Subscription autoTradingSubscription = currentUser.AutoTradingSubscription; if (autoTradingSubscription != null) { gateway.CancelSubscription(autoTradingSubscription.AuthorizeNETSubscriptionId); autoTradingSubscription.CancellationDate = DateTime.UtcNow; } _database.SaveChanges(); ViewBag.SubscriptionExpiryDate = subscriptionExpiryDate.ToLongDateString(); ViewBag.ActivationDate = activationDate.ToLongDateString(); // Mark the subscription as cancelled model.Cancelled = true; return(this.View(model)); }