LicenseRenewalForm.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.Wpf;
  9. using InABox.WPF;
  10. using System.Windows.Controls;
  11. using System.Drawing;
  12. using Image = System.Windows.Controls.Image;
  13. using System.Diagnostics;
  14. using FluentResults;
  15. using InABox.Client.Remote.Json;
  16. using InABox.Configuration;
  17. using Microsoft.Win32;
  18. using PRS.Shared;
  19. using Stripe;
  20. namespace PRSServer.Forms.DatabaseLicense
  21. {
  22. /// <summary>
  23. /// Interaction logic for LicenseRenewal.xaml
  24. /// </summary>
  25. public partial class LicenseRenewalForm : ThemableWindow, IDynamicEditorHost
  26. {
  27. public static HttpJsonClient LicenseClient = new("https", "remote.prsdigital.com.au", 5000);
  28. //public static HttpJsonClient LicenseClient = new("http", "127.0.0.1", 8001);
  29. private static readonly EncryptedLocalConfiguration<LicenseRegistrationDetails> _config = new("g6+BoQpyti5bHsTZOY5Nbqq3Q3c90n0m3qZaQ3eAwkk=");
  30. private LicenseRegistrationDetails _licenseRegistrationDetails;
  31. private LicenseData? _currentLicense;
  32. public LicenseData? CurrentLicense
  33. {
  34. get => _currentLicense;
  35. set => _currentLicense = value;
  36. }
  37. public int RenewalPeriod
  38. {
  39. get
  40. {
  41. if (int.TryParse(RenewalPeriodEditor.Value?.ToString(), out var period))
  42. {
  43. return period;
  44. }
  45. return 0;
  46. }
  47. set
  48. {
  49. RenewalPeriodEditor.Value = value;
  50. CalculateDiscounts();
  51. }
  52. }
  53. public DateTime RenewalDate
  54. {
  55. get
  56. {
  57. if(CurrentLicense == null)
  58. {
  59. return DateTime.MinValue;
  60. }
  61. if (CurrentLicense.Expiry > DateTime.Now)
  62. {
  63. return CurrentLicense.Expiry.Date;
  64. }
  65. return DateTime.Today;
  66. }
  67. }
  68. private DateTime _renewalAvailable;
  69. public DateTime RenewalAvailableFrom
  70. {
  71. get => _renewalAvailable;
  72. set
  73. {
  74. _renewalAvailable = value;
  75. if (!CanRenew)
  76. {
  77. RenewalAvailableLabel.Content = $"Renewal available from {value:dd MMM yyyy}";
  78. }
  79. }
  80. }
  81. public bool CanRenew => RenewalAvailableFrom.Date <= DateTime.Today || (CurrentLicense != null && CurrentLicense.Expiry <= DateTime.Now);
  82. public DateTime NewExpiration
  83. {
  84. get => RenewalDate.AddMonths(RenewalPeriod);
  85. }
  86. public List<LicenseTrackingItem> LicenseItems { get; private set; }
  87. public int Licenses { get; private set; }
  88. public double Gross { get; private set; }
  89. public double Discount { get; private set; }
  90. public double Net => Gross - Discount;
  91. public LicenseRenewalForm()
  92. {
  93. InitializeComponent();
  94. Modules.OnCustomiseColumns += Modules_OnOnCustomiseColumns;
  95. // This should get us the RegistrationID, which we need in order
  96. // to get the License Pricing from the Server
  97. _licenseRegistrationDetails = _config.Load(false);
  98. LastRenewal.EditorDefinition = new DateEditor();
  99. LastRenewal.Configure();
  100. LastRenewal.IsEnabled = false;
  101. CurrentExpiry.EditorDefinition = new DateEditor();
  102. CurrentExpiry.Configure();
  103. CurrentExpiry.IsEnabled = false;
  104. RenewalPeriodEditor.EditorDefinition = new ComboLookupEditor(typeof(RenewalPeriodLookups));
  105. RenewalPeriodEditor.Host = this;
  106. RenewalPeriodEditor.Configure();
  107. var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
  108. RenewalPeriodEditor.LoadLookups(lookups);
  109. NewExpiry.EditorDefinition = new DateEditor();
  110. NewExpiry.Configure();
  111. NewExpiry.IsEnabled = false;
  112. GrossLicenseFee.EditorDefinition = new CurrencyEditor();
  113. GrossLicenseFee.Configure();
  114. GrossLicenseFee.IsEnabled = false;
  115. DiscountEditor.EditorDefinition = new DoubleEditor();
  116. DiscountEditor.Configure();
  117. DiscountEditor.IsEnabled = false;
  118. NettLicenseFee.EditorDefinition = new CurrencyEditor();
  119. NettLicenseFee.Configure();
  120. NettLicenseFee.IsEnabled = false;
  121. Help.Content = new Image { Source = Properties.Resources.help.AsBitmapImage(Color.White) };
  122. Help.Click += Help_Click;
  123. }
  124. private void Help_Click(object sender, RoutedEventArgs e)
  125. {
  126. Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/License_Renewal") { UseShellExecute = true });
  127. }
  128. private void LoadData()
  129. {
  130. Result<bool> result = Progress.ShowModal<bool>("Getting License", progress =>
  131. {
  132. try
  133. {
  134. progress.Report("Loading License");
  135. CurrentLicense = LoadCurrentLicense();
  136. progress.Report("Checking Server");
  137. if (!LicenseClient.Ping("ping"))
  138. return Result.Fail<bool>("Server Unavailable");
  139. progress.Report("Retrieving Data");
  140. RetrieveFees();
  141. progress.Report("Scanning Data");
  142. LoadUserTracking();
  143. foreach (var item in LicenseItems)
  144. item.Rate = LicenseUtils.GetLicenseFee(item.Type);
  145. return Result.Ok<bool>(true);
  146. }
  147. catch (Exception e)
  148. {
  149. return Result.Fail<bool>(e.Message);
  150. }
  151. });
  152. if (result.IsSuccess && result.Value)
  153. UpdateWindow();
  154. else
  155. {
  156. MessageWindow.ShowMessage(String.Join("\n",result.Errors.Select(x=>x.Message)), "Error");
  157. UpdateWindow();
  158. }
  159. }
  160. private void UpdateWindow()
  161. {
  162. // Always a minimum of one license required!
  163. Licenses = Math.Max(1, LicenseItems.OrderByDescending(x => x.Users).FirstOrDefault()?.Users ?? 0);
  164. Modules.Items = LicenseItems;
  165. Modules.Refresh(true, true);
  166. var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
  167. RenewalPeriodEditor.LoadLookups(lookups);
  168. RenewalPeriodEditor.Loaded = true;
  169. RenewalPeriod = LicenseUtils.TimeDiscountLevels().OrderBy(x => x).FirstOrDefault();
  170. PayWithStripe.IsEnabled = CanRenew;
  171. if (!PayWithStripe.IsEnabled)
  172. {
  173. PayTooltip.Visibility = Visibility.Visible;
  174. PayTooltip.Content = new Label { Content = $"Renewal available from {RenewalAvailableFrom:dd MMM yyyy}" };
  175. }
  176. else
  177. PayTooltip.Visibility = Visibility.Collapsed;
  178. LastRenewal.Loaded = true;
  179. CurrentExpiry.Loaded = true;
  180. NewExpiry.Loaded = true;
  181. //PayWithStripe.IsEnabled = false;
  182. PayTooltip.Visibility = Visibility.Visible;
  183. PayTooltip.Content = new Label { Content = "Loading..." };
  184. LastRenewal.Value = CurrentLicense?.LastRenewal ?? DateTime.MinValue;
  185. CurrentExpiry.Value = CurrentLicense?.Expiry ?? DateTime.MinValue;
  186. RenewalAvailableFrom = CurrentLicense?.RenewalAvailable ?? DateTime.MinValue;
  187. }
  188. private void Window_Loaded(object sender, RoutedEventArgs e)
  189. {
  190. LoadData();
  191. }
  192. private void RetrieveFees()
  193. {
  194. var summary = LicenseClient.PostRequest<LicenseFeeResponse>(
  195. new LicenseFeeRequest() { RegistrationID = CurrentLicense?.CustomerID ?? Guid.Empty },
  196. nameof(LicenseFeeRequest)
  197. );
  198. LicenseUtils.LoadSummary(summary);
  199. }
  200. private void LoadUserTracking()
  201. {
  202. var renewaldate = _currentLicense?.LastRenewal ?? DateTime.MinValue;
  203. var filter = !renewaldate.IsEmpty()
  204. ? new Filter<UserTracking>(x => x.Date).IsGreaterThanOrEqualTo(renewaldate).And(x => x.TotalWrite).IsNotEqualTo(0)
  205. : new Filter<UserTracking>(x => x.TotalWrite).IsNotEqualTo(0);
  206. var data = new Client<UserTracking>()
  207. .Query(
  208. filter,
  209. new Columns<UserTracking>(x => x.User.ID)
  210. .Add(x => x.Type));
  211. var typesummary = data.Rows.GroupBy(r => r.Get<UserTracking, String>(c => c.Type)).ToArray();
  212. //Licenses = data.ExtractValues<UserTracking, Guid>(x => x.User.ID).Distinct().Count();
  213. var licensemap = LicenseUtils.LicenseMap();
  214. var result = new List<LicenseTrackingItem>();
  215. foreach (var map in licensemap)
  216. {
  217. var type = map.Value.EntityName();
  218. var item = result.FirstOrDefault(x => Equals(x.Type, type));
  219. if (item == null)
  220. {
  221. item = new LicenseTrackingItem()
  222. {
  223. Type = type,
  224. Caption = map.Value.GetCaption(),
  225. };
  226. result.Add(item);
  227. }
  228. var users = typesummary
  229. .FirstOrDefault(x => Equals(x.Key, map.Key.EntityName().Split('.').Last()))?
  230. .Select(r=>r.Get<UserTracking,Guid>(x=>x.User.ID))
  231. .Distinct()
  232. .Where(x=>!item.UserIDs.Contains(x));
  233. if (users != null)
  234. item.UserIDs.AddRange(users);
  235. }
  236. LicenseItems = result
  237. .OrderBy(x=>x.Caption)
  238. .ToList();
  239. }
  240. private static LicenseData? LoadCurrentLicense()
  241. {
  242. var license = new Client<License>().Query(new Filter<License>().All(), new Columns<License>(x => x.Data))
  243. .Rows.FirstOrDefault()?.ToObject<License>();
  244. if(license == null)
  245. {
  246. MessageBox.Show("No current license found. Please see the documentation on how to proceed.");
  247. return null;
  248. }
  249. if(!LicenseUtils.TryDecryptLicense(license.Data, out var data, out var error))
  250. {
  251. MessageBox.Show("Current license is corrupt. Please see the documentation on how to proceed.");
  252. return null;
  253. }
  254. return data;
  255. }
  256. private void RenewalPeriod_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  257. {
  258. CalculateDiscounts();
  259. }
  260. private void CalculateDiscounts()
  261. {
  262. double CalcDiscount(double amount, int months)
  263. {
  264. return (amount * (LicenseUtils.GetUserDiscount(Licenses) / 100.0F)) +
  265. (amount * (LicenseUtils.GetTimeDiscount(months) / 100.0F));
  266. }
  267. var periodInMonths = RenewalPeriod;
  268. NewExpiry.Value = NewExpiration;
  269. double total = 0.0F;
  270. if (CurrentLicense?.IsDynamic == true)
  271. {
  272. foreach (var row in Modules.Data.Rows)
  273. total += row.Get<LicenseTrackingItem, double>(x => x.ExGST) * periodInMonths;
  274. Gross = total;
  275. Discount = CalcDiscount(total, periodInMonths);
  276. }
  277. else
  278. {
  279. foreach (var row in Modules.Data.Rows)
  280. total += Licenses * LicenseUtils.GetLicenseFee(row.Get<LicenseTrackingItem, String>(x => x.Type)) * periodInMonths;
  281. Gross = Math.Round(total) - 0.05;
  282. Gross = total;
  283. Discount = Math.Round(CalcDiscount(total, periodInMonths) * 20F) / 20F;
  284. }
  285. GrossLicenseFee.Value = Gross;
  286. DiscountEditor.Value = Discount;
  287. NettLicenseFee.Value = Net;
  288. PayWithStripe.IsEnabled = CanRenew && Net > 0;
  289. }
  290. private class RenewalPeriodLookups : LookupGenerator<object>
  291. {
  292. public RenewalPeriodLookups(object[] items) : base(items)
  293. {
  294. }
  295. protected override void DoGenerateLookups()
  296. {
  297. foreach (var period in LicenseUtils.TimeDiscountLevels())
  298. AddValue(period, string.Format("{0} month{1}", period, period > 1 ? "s" : ""));
  299. }
  300. }
  301. private void PayNowClick(object sender, RoutedEventArgs e)
  302. {
  303. if (!LicenseClient.Ping("ping"))
  304. {
  305. MessageBox.Show("The PRS server is not available right now. Please try again later.");
  306. return;
  307. }
  308. var grid = new LicenseRegistrationGrid();
  309. if (grid.EditItems(new LicenseRegistrationDetails[] { _licenseRegistrationDetails }))
  310. {
  311. _config.Save(_licenseRegistrationDetails);
  312. Result<String> result = Result.Fail("Incomplete");
  313. var renewalRequest = CreateRenewal();
  314. Progress.ShowModal("Processing", progress =>
  315. {
  316. if (renewalRequest.Net > 0.0F)
  317. {
  318. // Process the Stripe Payment
  319. progress.Report("Processing Payment");
  320. result = ProcessStripePayment();
  321. if (result.IsFailed)
  322. return;
  323. }
  324. else
  325. result = Result.Ok("no payment required");
  326. progress.Report("Creating Renewal");
  327. result = RenewLicense(renewalRequest, result.Value);
  328. if (result.IsFailed)
  329. return;
  330. progress.Report("Saving License");
  331. SaveLicense(result.Value);
  332. });
  333. if (result.IsFailed)
  334. MessageWindow.ShowMessage(String.Join("\n",result.Errors.Select(x=>x.Message)),"Error");
  335. else
  336. {
  337. MessageWindow.ShowMessage("License Updated Successfully!","Success");
  338. Close();
  339. }
  340. }
  341. }
  342. private Result<String> ProcessStripePayment()
  343. {
  344. var result = "";
  345. var error = "";
  346. try
  347. {
  348. StripeConfiguration.ApiKey = "sk_test_51MRSuPIyPMVqmkXN8TeGxDAGBeFx0pLzn3fsHBR8X1oMBKQVwGPEbuv6DNIu0qSmuflpmFfQ4N8c3vzdknKa7G0o00wTOXwCeW";
  349. var cardoptions = new TokenCreateOptions()
  350. {
  351. Card = new TokenCardOptions()
  352. {
  353. Number = _licenseRegistrationDetails.Card.CardNumber.Replace(" ", ""),
  354. ExpMonth = _licenseRegistrationDetails.Card.Month,
  355. ExpYear = _licenseRegistrationDetails.Card.Year,
  356. Cvc = _licenseRegistrationDetails.Card.Cvv
  357. }
  358. };
  359. var service = new TokenService();
  360. var token = service.Create(cardoptions);
  361. var chargeoptions = new ChargeCreateOptions()
  362. {
  363. Amount = Convert.ToInt64(Gross * 100),
  364. Currency = "AUD",
  365. Description = "PRS Renewal",
  366. Source = token.Id
  367. };
  368. var chargeservice = new ChargeService();
  369. var charge = chargeservice.Create(chargeoptions);
  370. if (charge.Paid)
  371. result = charge.Id;
  372. else
  373. error = string.Format("{0}: {1}", charge.FailureCode, charge.FailureMessage);
  374. }
  375. catch (Exception ex)
  376. {
  377. error = $"{ex.Message}";
  378. }
  379. if (!String.IsNullOrWhiteSpace(error))
  380. return Result.Fail(error);
  381. return Result.Ok(result);
  382. }
  383. private LicenseRenewalRequest CreateRenewal()
  384. {
  385. return new LicenseRenewalRequest
  386. {
  387. Company = _licenseRegistrationDetails.Company,
  388. DateRenewed = RenewalDate,
  389. OldLicense = CurrentLicense,
  390. NewExpiry = NewExpiration,
  391. LicenseTracking = LicenseItems.ToArray(),
  392. Gross = Gross,
  393. Discount = Discount,
  394. Net = Net,
  395. Addresses = LicenseUtils.GetMacAddresses(),
  396. };
  397. }
  398. private Result<string> RenewLicense(LicenseRenewalRequest renewalRequest, String transactionID)
  399. {
  400. renewalRequest.TransactionID = transactionID;
  401. try
  402. {
  403. var result = LicenseClient.PostRequest<LicenseRenewalResult>(renewalRequest, nameof(LicenseRenewalRequest));
  404. return Result.Ok(result.License);
  405. }
  406. catch (Exception e)
  407. {
  408. return Result.Fail(e.Message);
  409. }
  410. }
  411. private void SaveLicense(string license)
  412. {
  413. using var client = new Client<License>();
  414. var oldLicenses = client
  415. .Query(new Filter<License>().All(), new Columns<License>(x => x.ID))
  416. .Rows.Select(x => x.ToObject<License>()).ToList();
  417. client.Delete(oldLicenses, "");
  418. client.Save(new License() { Data = license }, "");
  419. }
  420. #region IDynamicEditorHost
  421. public IEnumerable<DynamicGridColumn> Columns => new DynamicGridColumn[] { };
  422. public void LoadColumns(string column, Dictionary<string, string> columns)
  423. {
  424. }
  425. public IFilter? DefineFilter(Type type)
  426. {
  427. return null;
  428. }
  429. public void LoadLookups(ILookupEditorControl editor)
  430. {
  431. }
  432. public object?[] GetItems()
  433. {
  434. return new object?[] { };
  435. }
  436. public BaseEditor? GetEditor(DynamicGridColumn column)
  437. {
  438. return new NullEditor();
  439. }
  440. #endregion
  441. private void EnterKey_Click(object sender, RoutedEventArgs e)
  442. {
  443. if (EnterKey?.ContextMenu != null)
  444. EnterKey.ContextMenu.IsOpen = true;
  445. }
  446. private void CreateManualRequest(object sender, RoutedEventArgs e)
  447. {
  448. var request = new LicenseRequest()
  449. {
  450. CustomerID = CurrentLicense?.CustomerID ?? Guid.Empty,
  451. Addresses = LicenseUtils.GetMacAddresses(),
  452. IsDynamic = CurrentLicense?.IsDynamic ?? false,
  453. };
  454. SaveFileDialog sfd = new SaveFileDialog()
  455. {
  456. FileName = "license.request"
  457. };
  458. if (sfd.ShowDialog() == true)
  459. {
  460. System.IO.File.WriteAllText(sfd.FileName, LicenseUtils.EncryptLicenseRequest(request));
  461. MessageWindow.ShowMessage("Please email this file to support@prsdigital.com.au!","Request Created");
  462. }
  463. }
  464. private void LoadManualResponse(object sender, RoutedEventArgs e)
  465. {
  466. var ofd = new OpenFileDialog()
  467. {
  468. FileName = "license.key",
  469. Filter = "License Files (*.key)|*.key"
  470. };
  471. if (ofd.ShowDialog() == true && System.IO.File.Exists(ofd.FileName))
  472. {
  473. var text = System.IO.File.ReadAllText(ofd.FileName);
  474. if (LicenseUtils.TryDecryptLicense(text, out var data, out var _))
  475. {
  476. if (LicenseUtils.ValidateMacAddresses(data.Addresses))
  477. {
  478. SaveLicense(text);
  479. MessageWindow.ShowMessage("License Updated", "Success");
  480. Close();
  481. }
  482. else
  483. MessageWindow.ShowMessage("License Key is not valid!","Invalid Key");
  484. }
  485. else
  486. MessageWindow.ShowMessage("License Key is not valid!","Bad Key");
  487. }
  488. }
  489. private void Modules_OnOnCustomiseColumns(object sender, DynamicGridColumns columns)
  490. {
  491. var userscol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.Users)));
  492. if (userscol != null)
  493. userscol.Alignment = Alignment.MiddleCenter;
  494. if (_currentLicense?.IsDynamic != true)
  495. {
  496. var ratecol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.Rate)));
  497. if (ratecol != null)
  498. columns.Remove(ratecol);
  499. var exgstcol = columns.FirstOrDefault(x => String.Equals(x.ColumnName, nameof(LicenseTrackingItem.ExGST)));
  500. if (exgstcol != null)
  501. columns.Remove(exgstcol);
  502. }
  503. }
  504. }
  505. }