-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSubscriptionController.cs
768 lines (633 loc) · 26.5 KB
/
SubscriptionController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Resgrid.Model;
using Resgrid.Model.Helpers;
using Resgrid.Model.Services;
using Resgrid.Providers.Claims;
using Resgrid.Web.Areas.User.Models.Subscription;
using Resgrid.Web.Options;
using Stripe;
using Microsoft.AspNetCore.Authorization;
using Resgrid.Framework;
using Resgrid.Model.Events;
using Resgrid.Model.Providers;
using Resgrid.Providers.Bus;
using Resgrid.Services;
using Resgrid.Web.Helpers;
namespace Resgrid.Web.Areas.User.Controllers
{
[Area("User")]
[ClaimsResource(ResgridClaimTypes.Resources.Department)]
public class SubscriptionController : SecureBaseController
{
#region Private Members and Constructors
private readonly IDepartmentsService _departmentsService;
private readonly IUsersService _usersService;
private readonly IDepartmentGroupsService _departmentGroupsService;
private readonly Model.Services.IAuthorizationService _authorizationService;
private readonly ISubscriptionsService _subscriptionsService;
private readonly IPersonnelRolesService _personnelRolesService;
private readonly IUnitsService _unitsService;
private readonly IDepartmentSettingsService _departmentSettingsService;
private readonly IEmailService _emailService;
private readonly IAffiliateService _affiliateService;
private readonly IUserProfileService _userProfileService;
private readonly IOptions<AppOptions> _appOptionsAccessor;
private readonly IEventAggregator _eventAggregator;
public SubscriptionController(IDepartmentsService departmentsService, IUsersService usersService, IDepartmentGroupsService departmentGroupsService,
Model.Services.IAuthorizationService authorizationService, ISubscriptionsService subscriptionsService, IPersonnelRolesService personnelRolesService, IUnitsService unitsService,
IDepartmentSettingsService departmentSettingsService, IEmailService emailService, IAffiliateService affiliateService,
IUserProfileService userProfileService, IOptions<AppOptions> appOptionsAccessor, IEventAggregator eventAggregator)
{
_departmentsService = departmentsService;
_usersService = usersService;
_departmentGroupsService = departmentGroupsService;
_authorizationService = authorizationService;
_subscriptionsService = subscriptionsService;
_personnelRolesService = personnelRolesService;
_unitsService = unitsService;
_departmentSettingsService = departmentSettingsService;
_emailService = emailService;
_affiliateService = affiliateService;
_userProfileService = userProfileService;
_appOptionsAccessor = appOptionsAccessor;
_eventAggregator = eventAggregator;
}
#endregion Private Members and Constructors
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> Index()
{
if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId))
Unauthorized();
var model = new SubscriptionView();
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
model.Plan = await _subscriptionsService.GetCurrentPlanForDepartmentAsync(DepartmentId);
model.Payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync(DepartmentId);
model.IsTestingDepartment = await _departmentSettingsService.IsTestingEnabledForDepartmentAsync(DepartmentId);
model.Department = department;
model.StripeKey = Config.PaymentProviderConfig.GetStripeClientKey();
model.StripeCustomer = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
if (model.Plan != null && model.Plan.PlanId != 1 && model.Plan.Cost == 0)
{
if (model.Payment != null)
{
model.Plan.Cost = model.Payment.Amount;
model.Plan.Quantity = model.Payment.Quantity;
}
}
var allPayments = await _subscriptionsService.GetAllPaymentsForDepartmentAsync(DepartmentId);
if (allPayments != null)
model.HadStripePaymentIn30Days = allPayments.Any(x => x.EndingOn >= DateTime.UtcNow.AddYears(-2) && x.Method == (int)PaymentMethods.Stripe);
else
model.HadStripePaymentIn30Days = false;
if (model.Payment != null)
{
if (model.Payment.EndingOn == DateTime.MaxValue)
model.Expires = "Never";
else
model.Expires = TimeConverterHelper.TimeConverter(model.Payment.EndingOn, department).ToString("D");
}
else
{
model.Expires = "Never";
}
if (model.Plan != null)
{
model.PossibleUpgrades = _subscriptionsService.GetPossibleUpgradesForPlan(model.Plan.PlanId);
model.PossibleDowngrades = _subscriptionsService.GetPossibleDowngradesForPlan(model.Plan.PlanId);
}
else
{
model.PossibleUpgrades = _subscriptionsService.GetPossibleUpgradesForPlan(1);
model.PossibleDowngrades = _subscriptionsService.GetPossibleUpgradesForPlan(1);
model.Plan = new Resgrid.Model.Plan() { PlanId = 1, Cost = 0, Name = "Forever Free" };
}
var personnelCount = (await _departmentsService.GetAllUsersForDepartmentUnlimitedMinusDisabledAsync(DepartmentId)).Count;
var unitsCount = (await _unitsService.GetUnitsForDepartmentUnlimitedAsync(DepartmentId)).Count;
if (model.Plan.PlanId >= 36)
{
model.PersonnelCount = personnelCount + unitsCount;
model.PersonnelLimit = model.Plan.GetLimitForType(PlanLimitTypes.Entities);
float personnelLimit;
if (float.TryParse(model.Plan.GetLimitForType(PlanLimitTypes.Entities), out personnelLimit))
{
float personLimit = (model.PersonnelCount / personnelLimit) * 100f;
model.PersonnelBarPrecent = personLimit.ToString();
if (personLimit >= 100)
{
ViewBag.PersonnelBarStyle = "progress-bar-danger";
SetSubscriptionErrorMessage();
}
else if (personLimit >= 75)
ViewBag.PersonnelBarStyle = "progress-bar-warning";
else
ViewBag.PersonnelBarStyle = "progress-bar-info";
}
else
{
model.PersonnelBarPrecent = "0.0";
}
}
else
{
model.PersonnelCount = personnelCount;
model.PersonnelLimit = model.Plan.GetLimitForType(PlanLimitTypes.Personnel);
float personnelLimit;
if (float.TryParse(model.Plan.GetLimitForType(PlanLimitTypes.Personnel), out personnelLimit))
{
float personLimit = (model.PersonnelCount / personnelLimit) * 100f;
model.PersonnelBarPrecent = personLimit.ToString();
if (personLimit >= 100)
{
ViewBag.PersonnelBarStyle = "progress-bar-danger";
SetSubscriptionErrorMessage();
}
else if (personLimit >= 75)
ViewBag.PersonnelBarStyle = "progress-bar-warning";
else
ViewBag.PersonnelBarStyle = "progress-bar-info";
}
else
{
model.PersonnelBarPrecent = "0.0";
}
}
var addon = await _subscriptionsService.GetPTTAddonPlanForDepartmentFromStripeAsync(DepartmentId);
model.HasActiveSubscription = await _subscriptionsService.HasActiveSubForDepartmentFromStripeAsync(DepartmentId);
model.HasActiveAddon = addon != null;
model.AddonFrequencyString = "month";
if (model.Plan != null)
{
if (model.Plan.Frequency == (int)PlanFrequency.Yearly)
model.AddonFrequencyString = "year";
else if (model.Plan.Frequency == (int)PlanFrequency.Monthly)
model.AddonFrequencyString = "month";
}
if (addon != null && addon.IsCancelled)
{
model.IsAddonCanceled = addon.IsCancelled;
model.AddonEndingOn = addon.EndingOn;
}
var addonPlan = await _subscriptionsService.GetPTTAddonForCurrentSubAsync(DepartmentId);
if (addonPlan != null)
{
model.AddonCost = addonPlan.Cost.ToString("C0", Cultures.UnitedStates);
model.AddonCost2 = (addonPlan.Cost / 2).ToString("C0", Cultures.UnitedStates);
model.AddonPlanIdToBuy = addonPlan.PlanAddonId;
}
else
model.AddonCost = "0";
var user = _usersService.GetUserById(UserId);
try
{
var session = await _subscriptionsService.CreateStripeSessionForCustomerPortal(DepartmentId, model.StripeCustomer, "", user.Email, department.Name);
if (session != null)
model.StripeCustomerPortalUrl = session.Url;
}
catch (Exception ex)
{
Logging.LogException(ex);
}
return View(model);
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> UpdateBillingInfo()
{
if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId))
Unauthorized();
var model = new BuyNowView();
if (Config.PaymentProviderConfig.IsTestMode)
model.StripeKey = Config.PaymentProviderConfig.TestClientKey;
else
model.StripeKey = Config.PaymentProviderConfig.ProductionClientKey;
return View(model);
}
[HttpPost]
[Authorize(Policy = ResgridResources.Department_Update)]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateBillingInfo(IFormCollection form, CancellationToken cancellationToken)
{
if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId))
Unauthorized();
try
{
var user = _usersService.GetUserById(UserId);
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
var cardToken = form["stripeToken"];
var cardService = new CardService();
var customerService = new CustomerService();
var updateCardOptions = new CardCreateOptions();
updateCardOptions.Source = new AnyOf<string, CardCreateNestedOptions>(cardToken);
Card stripeCard = await cardService.CreateAsync(stripeCustomerId, updateCardOptions, cancellationToken: cancellationToken);
var customerOptions = new CustomerUpdateOptions
{
Email = user.Email,
Description = department.Name,
DefaultSource = stripeCard.Id
};
Customer stripeCustomer = await customerService.UpdateAsync(stripeCustomerId, customerOptions, cancellationToken: cancellationToken);
var auditEvent = new AuditEvent();
auditEvent.Before = updateCardOptions.CloneJsonToString();
auditEvent.DepartmentId = DepartmentId;
auditEvent.UserId = UserId;
auditEvent.Type = AuditLogTypes.SubscriptionBillingInfoUpdated;
auditEvent.After = stripeCustomer.CloneJsonToString();
auditEvent.Successful = true;
auditEvent.IpAddress = IpAddressHelper.GetRequestIP(Request, true);
auditEvent.ServerName = Environment.MachineName;
auditEvent.UserAgent = $"{Request.Headers["User-Agent"]} {Request.Headers["Accept-Language"]}";
_eventAggregator.SendMessage<AuditEvent>(auditEvent);
return RedirectToAction("BillingInfoUpdateSuccess", "Subscription", new { Area = "User" });
}
catch (Exception ex)
{
Logging.SendExceptionEmail(ex, "UpdateBillingInfo", DepartmentId, UserName);
return RedirectToAction("PaymentFailed", "Subscription",
new { Area = "User", chargeId = "", errorMessage = ex.Message });
}
}
[HttpPost]
public async Task<IActionResult> LogStripeResponse(StripeResponseInput input, CancellationToken cancellationToken)
{
var providerEvent = new PaymentProviderEvent();
providerEvent.ProviderType = (int)PaymentMethods.Stripe;
providerEvent.RecievedOn = DateTime.UtcNow;
providerEvent.Data = $"Card Token Result: UserId:{UserId} DepartmentId:{DepartmentId} Status:{input.Status} Response:{input.Response}";
providerEvent.Processed = false;
providerEvent.CustomerId = "SYSTEM";
await _subscriptionsService.SavePaymentEventAsync(providerEvent, cancellationToken);
return new EmptyResult();
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> ValidateCoupon(string couponCode)
{
var service = new CouponService();
Coupon coupon = null;
try
{
if (!String.IsNullOrWhiteSpace(couponCode))
coupon = await service.GetAsync(couponCode.Trim().ToUpper());
}
catch
{
}
if (coupon == null || (coupon.RedeemBy.HasValue && coupon.RedeemBy.Value < DateTime.UtcNow))
return Content("Invalid");
return Content("Valid");
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> Cancel()
{
if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId))
Unauthorized();
CancelView model = new CancelView();
model.Payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync((await _departmentsService.GetDepartmentByUserIdAsync(UserId)).DepartmentId);
model.Plan = await _subscriptionsService.GetPlanByIdAsync(model.Payment.PlanId);
return View(model);
}
[HttpGet]
public async Task<IActionResult> BillingInfoUpdateSuccess()
{
return View();
}
[HttpGet]
public async Task<IActionResult> StripeBillingInfoUpdateSuccess(string sessionId)
{
var model = new PaymentCompleteView();
model.SessionId = sessionId;
return View(model);
}
[HttpPost]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> Cancel(CancelView model, CancellationToken cancellationToken)
{
if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId))
Unauthorized();
if (!model.Confirm)
ModelState.AddModelError("Confirm", "You must check the confirm box to cancel the subscription.");
if (ModelState.IsValid)
{
var payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync(DepartmentId);
if (payment == null)
return RedirectToAction("CancelFailure", "Subscription", new { Area = "User" });
if (payment.Method == (int)PaymentMethods.Stripe)
{
var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
if (String.IsNullOrWhiteSpace(stripeCustomerId))
{
var user = _usersService.GetUserById(UserId);
var cusService = new CustomerService();
var options = new CustomerListOptions
{
Email = user.Email
};
var customerList = await cusService.ListAsync(options, cancellationToken: cancellationToken);
if (customerList != null && customerList.Any())
stripeCustomerId = customerList.First().Id;
}
if (!String.IsNullOrWhiteSpace(stripeCustomerId))
{
var subscriptionService = new SubscriptionService();
var subs = await subscriptionService.ListAsync(new SubscriptionListOptions { Customer = stripeCustomerId }, cancellationToken: cancellationToken);
Subscription subscription = subs.First(sub => !sub.EndedAt.HasValue);
var cancelledSub = await subscriptionService.CancelAsync(subscription.Id, new SubscriptionCancelOptions { }, cancellationToken: cancellationToken);
var auditEvent = new AuditEvent();
auditEvent.Before = JsonConvert.SerializeObject(subscription);
auditEvent.DepartmentId = DepartmentId;
auditEvent.UserId = UserId;
auditEvent.Type = AuditLogTypes.SubscriptionCancelled;
auditEvent.After = JsonConvert.SerializeObject(cancelledSub);
auditEvent.Successful = true;
auditEvent.IpAddress = IpAddressHelper.GetRequestIP(Request, true);
auditEvent.ServerName = Environment.MachineName;
auditEvent.UserAgent = $"{Request.Headers["User-Agent"]} {Request.Headers["Accept-Language"]}";
_eventAggregator.SendMessage<AuditEvent>(auditEvent);
if (cancelledSub != null && cancelledSub.Status.Equals("canceled", StringComparison.InvariantCultureIgnoreCase))
{
return RedirectToAction("CancelSuccess", "Subscription", new { Area = "User" });
}
else
{
return RedirectToAction("CancelFailure", "Subscription", new { Area = "User" });
}
}
else
{
return RedirectToAction("CancelFailure", "Subscription", new { Area = "User" });
}
}
}
model.Payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync((await _departmentsService.GetDepartmentByUserIdAsync(UserId)).DepartmentId);
model.Plan = await _subscriptionsService.GetPlanByIdAsync(model.Payment.PlanId);
return View(model);
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> BuyAddon(string planAddonId)
{
var model = new BuyAddonView();
model.PlanAddon = await _subscriptionsService.GetPlanAddonByIdAsync(planAddonId);
model.PlanAddonId = model.PlanAddon.PlanAddonId;
model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
var addonTypes = await _subscriptionsService.GetAllAddonPlansAsync();
var addons = await _subscriptionsService.GetCurrentPaymentAddonsForDepartmentAsync(DepartmentId,
addonTypes.Where(x => x.AddonType == model.PlanAddon.AddonType).Select(y => y.PlanAddonId).ToList());
if (addons != null && addons.Count > 0)
model.CurrentPaymentAddon = addons.FirstOrDefault();
if (model.PlanAddon.PlanId.HasValue)
{
var plan = await _subscriptionsService.GetPlanByIdAsync(model.PlanAddon.PlanId.Value);
model.Frequency = ((PlanFrequency)plan.Frequency).ToString();
}
return View(model);
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> ManagePTTAddon()
{
var model = new BuyAddonView();
model.PlanAddon = await _subscriptionsService.GetPlanAddonByIdAsync("6f4c5f8b-584d-4291-8a7d-29bf97ae6aa9");
model.PlanAddonId = model.PlanAddon.PlanAddonId;
model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
//var addons = await _subscriptionsService.GetCurrentPaymentAddonsForDepartmentAsync(DepartmentId,
// new List<string>(){SubscriptionsService.PTT10UserAddonPackage});
var stripeCustomer = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
var addon = await _subscriptionsService.GetActivePTTStripeSubscriptionAsync(stripeCustomer);
if (addon != null)
{
model.Quantity = addon.TotalQuantity;
}
/*
if (addons != null && addons.Count > 0)
model.CurrentPaymentAddon = addons.FirstOrDefault();
var planAddons = await _subscriptionsService.GetCurrentPlanAddonsForDepartmentFromStripeAsync(DepartmentId);
if (planAddons != null && planAddons.Any())
{
foreach (var addon in planAddons)
{
if (!addon.IsCancelled)
model.Quantity += addon.Quantity;
}
}
if (model.PlanAddon.PlanId.HasValue)
{
var plan = await _subscriptionsService.GetPlanByIdAsync(model.PlanAddon.PlanId.Value);
model.Frequency = ((PlanFrequency)plan.Frequency).ToString();
}
*/
return View(model);
}
[HttpPost]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> ManagePTTAddon(BuyAddonView model)
{
try
{
var user = _usersService.GetUserById(UserId);
var addonPlan = await _subscriptionsService.GetPlanAddonByIdAsync(model.PlanAddonId);
var plan = await _subscriptionsService.GetPlanByIdAsync(addonPlan.PlanId.Value);
var result = await _subscriptionsService.AddAddonAddedToExistingSub(DepartmentId, plan, addonPlan);
return RedirectToAction("PaymentComplete", "Subscription", new { Area = "User", planId = plan.PlanId });
}
catch (Exception ex)
{
Logging.SendExceptionEmail(ex, "BuyNow", DepartmentId, UserName);
return RedirectToAction("PaymentFailed", "Subscription",
new { Area = "User", chargeId = "", errorMessage = ex.Message });
}
}
[HttpPost]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> BuyAddon(BuyAddonView model, CancellationToken cancellationToken)
{
try
{
var user = _usersService.GetUserById(UserId);
var addonPlan = await _subscriptionsService.GetPlanAddonByIdAsync(model.PlanAddonId);
var currentAddonPayments = await _subscriptionsService.GetCurrentPlanAddonsForDepartmentFromStripeAsync(DepartmentId);
if (addonPlan != null)
{
var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
var auditEvent = new AuditEvent();
auditEvent.Before = null;
auditEvent.DepartmentId = DepartmentId;
auditEvent.UserId = UserId;
auditEvent.Type = AuditLogTypes.AddonSubscriptionModified;
auditEvent.After = model.Quantity.ToString();
auditEvent.Successful = true;
auditEvent.IpAddress = IpAddressHelper.GetRequestIP(Request, true);
auditEvent.ServerName = Environment.MachineName;
auditEvent.UserAgent = $"{Request.Headers["User-Agent"]} {Request.Headers["Accept-Language"]}";
_eventAggregator.SendMessage<AuditEvent>(auditEvent);
var result = await _subscriptionsService.ModifyPTTAddonSubscriptionAsync(stripeCustomerId, model.Quantity, addonPlan);
if (result)
return RedirectToAction("PaymentComplete", "Subscription", new { Area = "User", planId = 0 });
else
return RedirectToAction("PaymentFailed", "Subscription", new { Area = "User", chargeId = "", errorMessage = "Unknown Error" });
}
else
{
return RedirectToAction("PaymentFailed", "Subscription", new { Area = "User", chargeId = "", errorMessage = "Unknown Addon Plan" });
}
}
catch (Exception ex)
{
Logging.SendExceptionEmail(ex, "BuyNow", DepartmentId, UserName);
return RedirectToAction("PaymentFailed", "Subscription",
new { Area = "User", chargeId = "", errorMessage = ex.Message });
}
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> CancelAddon(int addonTypeId)
{
switch ((PlanAddonTypes)addonTypeId)
{
case PlanAddonTypes.PTT:
var addonPttPlan = await _subscriptionsService.GetPTTAddonPlanForDepartmentFromStripeAsync(DepartmentId);
if (addonPttPlan != null)
{
var result = await _subscriptionsService.CancelPlanAddonByTypeFromStripeAsync(DepartmentId, addonTypeId);
}
break;
default:
break;
}
return RedirectToAction("Index", "Subscription");
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> GetStripeSession(int id, int count, CancellationToken cancellationToken)
{
var plan = await _subscriptionsService.GetPlanByIdAsync(id);
var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
var user = _usersService.GetUserById(UserId);
var session = await _subscriptionsService.CreateStripeSessionForSub(DepartmentId, stripeCustomerId, plan.GetExternalKey(), plan.PlanId, user.Email, department.Name, count);
var subscription = await _subscriptionsService.GetActiveStripeSubscriptionAsync(session.CustomerId);
bool hasActiveSub = false;
if (subscription != null)
hasActiveSub = true;
return Json(new
{
SessionId = session.SessionId,
HasActiveSub = hasActiveSub
});
}
[HttpGet]
[Authorize(Policy = ResgridResources.Department_Update)]
public async Task<IActionResult> GetStripeUpdate()
{
//var plan = await _subscriptionsService.GetPlanById(id);
var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId);
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
var user = _usersService.GetUserById(UserId);
var session = await _subscriptionsService.CreateStripeSessionForUpdate(DepartmentId, stripeCustomerId, user.Email, department.Name);
return Json(new
{
SessionId = session.SessionId
});
}
//[AuthorizeUpdate]
public async Task<IActionResult> CancelSuccess()
{
return View();
}
//[AuthorizeUpdate]
public async Task<IActionResult> CancelFailure()
{
return View();
}
//[AuthorizeUpdate]
public async Task<IActionResult> PaymentComplete(int paymentId)
{
PaymentCompleteView model = new PaymentCompleteView();
model.PaymentId = paymentId;
return View(model);
}
//[AuthorizeUpdate]
public async Task<IActionResult> UnableToPurchase()
{
UnableToPurchaseView model = new UnableToPurchaseView();
model.CurrentPayment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync(DepartmentId);
model.NextPayment = await _subscriptionsService.GetUpcomingPaymentForDepartmentAsync(DepartmentId);
return View(model);
}
//[AuthorizeUpdate]
public async Task<IActionResult> PaymentFailed(string chargeId, string errorMessage)
{
PaymentFailedView model = new PaymentFailedView();
model.ChargeId = chargeId;
model.ErrorMessage = errorMessage;
return View(model);
}
public async Task<IActionResult> PaymentPending()
{
PaymentFailedView model = new PaymentFailedView();
return View(model);
}
//[AuthorizeUpdate]
public async Task<IActionResult> PaymentHistory()
{
PaymentHistoryView model = new PaymentHistoryView();
model.Payments = await _subscriptionsService.GetAllPaymentsForDepartmentAsync(DepartmentId);
model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);
return View(model);
}
//[AuthorizeUpdate]
public async Task<IActionResult> ViewInvoice(int paymentId)
{
if (!await _authorizationService.CanUserViewPaymentAsync(UserId, paymentId))
Unauthorized();
ViewInvoiceView model = new ViewInvoiceView();
model.Payment = await _subscriptionsService.GetPaymentByIdAsync(paymentId);
if (!String.IsNullOrWhiteSpace(model.Payment.Data))
{
try
{
model.Charge = JsonConvert.DeserializeObject<Charge>(model.Payment.Data);
}
catch { }
}
return View(model);
}
public async Task<IActionResult> Processing(int planId)
{
ProcessingView model = new ProcessingView();
model.PlanId = planId;
return View(model);
}
public async Task<IActionResult> StripeProcessing(int planId, string sessionId)
{
ProcessingView model = new ProcessingView();
model.PlanId = planId;
model.SessionId = sessionId;
return View(model);
}
[HttpGet]
public async Task<IActionResult> CheckProcessingStatus(int planId)
{
var payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync(DepartmentId);
if (payment != null && payment.PlanId == planId && payment.PurchaseOn.ToShortDateString() == DateTime.UtcNow.ToShortDateString())
return Json("1");
return Json("0");
}
private void SetSubscriptionErrorMessage()
{
ViewBag.SubscriptionErrorMessage =
"It appears that you have more entities then your current plan allows. Don't worry they have not been deleted, but to re-enable access to them you need to purchase a higher plan. Note that users, groups or units that are the the ones past the limit (by date added) may not be visible or able to use the system.";
}
}
}