LicenseRenewalForm.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Compression;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Policy;
  7. using System.Threading;
  8. using System.Windows;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using Microsoft.Exchange.WebServices.Data;
  15. using PRSServer.Forms.DatabaseLicense;
  16. using RestSharp;
  17. using Stripe;
  18. using DataFormat = RestSharp.DataFormat;
  19. using System.Net;
  20. using FastReport.DevComponents.DotNetBar;
  21. using System.Diagnostics.CodeAnalysis;
  22. using InABox.Database;
  23. using System.Runtime.CompilerServices;
  24. using System.Windows.Controls;
  25. using System.Drawing;
  26. using Image = System.Windows.Controls.Image;
  27. using System.Diagnostics;
  28. using Twilio.Rest.Api.V2010.Account;
  29. namespace PRSServer.Forms.DatabaseLicense
  30. {
  31. public class CreditCardGrid : DynamicGrid<CreditCardDetails>
  32. {
  33. protected override void Reload(Filters<CreditCardDetails> criteria, Columns<CreditCardDetails> columns, ref SortOrder<CreditCardDetails>? sort, Action<CoreTable, Exception> action)
  34. {
  35. }
  36. protected override CreditCardDetails LoadItem(CoreRow row)
  37. {
  38. return new CreditCardDetails();
  39. }
  40. public string TransactionID = "";
  41. protected override void DoValidate(CreditCardDetails[] items, List<string> errors)
  42. {
  43. base.DoValidate(items, errors);
  44. if (items.Any(x => string.IsNullOrWhiteSpace(x.CardNumber)))
  45. errors.Add("Please supply a card number.");
  46. if (items.Any(x => string.IsNullOrWhiteSpace(x.Month)))
  47. errors.Add("Please supply an expiration month.");
  48. if (items.Any(x => string.IsNullOrWhiteSpace(x.Year)))
  49. errors.Add("Please supply an expiration year.");
  50. if (items.Any(x => string.IsNullOrWhiteSpace(x.Cvv)))
  51. errors.Add("Please supply a CVV.");
  52. }
  53. protected override void SaveItem(CreditCardDetails item)
  54. {
  55. TransactionID = "";
  56. var error = "";
  57. Progress.ShowModal("Processing Payment", (progress) =>
  58. {
  59. try
  60. {
  61. StripeConfiguration.ApiKey = "sk_test_51Lc39sA3OFNLFxjFvH2PYwCpl9DNhkIoce3rskpTXOvoxiFQUIgtYg1MRWrPUIx14lcs9oG4pFVco9UvRSxen0mT00P8AisayJ";
  62. var cardoptions = new TokenCreateOptions()
  63. {
  64. Card = new TokenCardOptions()
  65. {
  66. Number = item.CardNumber,
  67. ExpMonth = item.Month,
  68. ExpYear = item.Year,
  69. Cvc = item.Cvv
  70. }
  71. };
  72. var service = new TokenService();
  73. var token = service.Create(cardoptions);
  74. var chargeoptions = new ChargeCreateOptions()
  75. {
  76. Amount = Convert.ToInt64(item.Amount * 100),
  77. Currency = "AUD",
  78. Description = "PRS Renewal",
  79. Source = token.Id
  80. };
  81. var chargeservice = new ChargeService();
  82. var charge = chargeservice.Create(chargeoptions);
  83. if (charge.Paid)
  84. TransactionID = charge.Id;
  85. else
  86. error = string.Format("{0}: {1}", charge.FailureCode, charge.FailureMessage);
  87. }
  88. catch (Exception ex)
  89. {
  90. error = $"{ex.Message}";
  91. }
  92. });
  93. //if (!String.IsNullOrWhiteSpace(error))
  94. // throw new Exception(error);
  95. }
  96. protected override void DeleteItems(params CoreRow[] rows)
  97. {
  98. }
  99. }
  100. /// <summary>
  101. /// Interaction logic for LicenseRenewal.xaml
  102. /// </summary>
  103. public partial class LicenseRenewalForm : ThemableWindow
  104. {
  105. private LicenseData? _currentLicense;
  106. private double _gross;
  107. private double _discount;
  108. private DateTime _renewalAvailable;
  109. public LicenseData? CurrentLicense
  110. {
  111. get => _currentLicense;
  112. set
  113. {
  114. if(value == null)
  115. {
  116. return;
  117. }
  118. PayWithStripe.IsEnabled = false;
  119. PayTooltip.Visibility = Visibility.Visible;
  120. PayTooltip.Content = new Label { Content = "Loading..." };
  121. _currentLicense = value;
  122. LastRenewal.Value = value.LastRenewal;
  123. CurrentExpiry.Value = value.Expiry;
  124. Modules.Renewed = value.LastRenewal;
  125. RenewalAvailableFrom = value.RenewalAvailable;
  126. Modules.Refresh(false, true);
  127. }
  128. }
  129. public int RenewalPeriod
  130. {
  131. get
  132. {
  133. if (int.TryParse(RenewalPeriodEditor.Value?.ToString(), out var period))
  134. {
  135. return period;
  136. }
  137. return 0;
  138. }
  139. set
  140. {
  141. RenewalPeriodEditor.SetValue(value);
  142. CalculateDiscounts();
  143. }
  144. }
  145. public DateTime RenewalDate
  146. {
  147. get
  148. {
  149. if(CurrentLicense == null)
  150. {
  151. return DateTime.MinValue;
  152. }
  153. if (CurrentLicense.Expiry > DateTime.Now)
  154. {
  155. return CurrentLicense.Expiry.Date;
  156. }
  157. return DateTime.Today;
  158. }
  159. }
  160. public DateTime RenewalAvailableFrom
  161. {
  162. get => _renewalAvailable;
  163. set
  164. {
  165. _renewalAvailable = value;
  166. if (!CanRenew)
  167. {
  168. RenewalAvailableLabel.Content = $"Renewal available from {value:dd MMM yyyy}";
  169. }
  170. }
  171. }
  172. public bool CanRenew => RenewalAvailableFrom.Date <= DateTime.Today || (CurrentLicense != null && CurrentLicense.Expiry <= DateTime.Now);
  173. public DateTime NewExpiration
  174. {
  175. get => RenewalDate.AddMonths(RenewalPeriod);
  176. }
  177. public double Gross { get => _gross; }
  178. public double Discount { get => _discount; }
  179. public double Net { get => _gross - _discount; }
  180. public LicenseRenewalForm()
  181. {
  182. InitializeComponent();
  183. LastRenewal.EditorDefinition = new DateEditor();
  184. LastRenewal.Configure();
  185. LastRenewal.IsEnabled = false;
  186. CurrentExpiry.EditorDefinition = new DateEditor();
  187. CurrentExpiry.Configure();
  188. CurrentExpiry.IsEnabled = false;
  189. RenewalPeriodEditor.EditorDefinition = new ComboLookupEditor(typeof(RenewalPeriodLookups));
  190. RenewalPeriodEditor.Configure();
  191. var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
  192. RenewalPeriodEditor.LoadLookups(lookups);
  193. NewExpiry.EditorDefinition = new DateEditor();
  194. NewExpiry.Configure();
  195. NewExpiry.IsEnabled = false;
  196. GrossLicenseFee.EditorDefinition = new CurrencyEditor();
  197. GrossLicenseFee.Configure();
  198. GrossLicenseFee.IsEnabled = false;
  199. DiscountEditor.EditorDefinition = new DoubleEditor();
  200. DiscountEditor.Configure();
  201. DiscountEditor.IsEnabled = false;
  202. NettLicenseFee.EditorDefinition = new CurrencyEditor();
  203. NettLicenseFee.Configure();
  204. NettLicenseFee.IsEnabled = false;
  205. Help.Content = new Image { Source = Properties.Resources.help.AsBitmapImage(Color.White) };
  206. Help.Click += Help_Click;
  207. Modules.OnAfterReload += Modules_OnAfterReload;
  208. //double months = Math.Max(0.0F,CalcMonths(CurrentExpiry.Value, NewExpiry.Value));
  209. //double total = 0.0F;
  210. //foreach (var row in Modules.Data.Rows)
  211. // total += row.Get<LicenseRenewal, double>(x => x.ExGST) * months;
  212. //GrossLicenseFee.Value = total;
  213. ////GrossLicenseFee.Width = 150;
  214. //Discount.Value = LicenseUtils.GetUserDiscount(Modules.Licenses);
  215. ////Discount.Width = 150;
  216. //NettLicenseFee.Value = total * ((100.0F - Discount.Value) / 100.0F);
  217. ////NettLicenseFee.Width = 150;
  218. }
  219. private void Help_Click(object sender, RoutedEventArgs e)
  220. {
  221. Process.Start(new ProcessStartInfo("https://prs-software.com.au/wiki/index.php/License_Renewal") { UseShellExecute = true });
  222. }
  223. private void Window_Loaded(object sender, RoutedEventArgs e)
  224. {
  225. Modules.Refresh(true, false);
  226. var license = LoadCurrentLicense();
  227. if(license != null)
  228. {
  229. CurrentLicense = license;
  230. }
  231. LastRenewal.Loaded = true;
  232. CurrentExpiry.Loaded = true;
  233. NewExpiry.Loaded = true;
  234. RenewalPeriodEditor.Loaded = true;
  235. }
  236. private void Modules_OnAfterReload(object sender)
  237. {
  238. if (CurrentLicense == null) return;
  239. RenewalPeriod = LicenseUtils.TimeDiscountLevels().OrderBy(x => x).First();
  240. PayWithStripe.IsEnabled = CanRenew;
  241. if (!PayWithStripe.IsEnabled)
  242. {
  243. PayTooltip.Visibility = Visibility.Visible;
  244. PayTooltip.Content = new Label { Content = $"Renewal available from {RenewalAvailableFrom:dd MMM yyyy}" };
  245. }
  246. else
  247. {
  248. PayTooltip.Visibility = Visibility.Collapsed;
  249. }
  250. OK.IsEnabled = true;
  251. }
  252. private static LicenseData? LoadCurrentLicense()
  253. {
  254. var license = new Client<License>().Query(new Filter<License>().All(), new Columns<License>(x => x.Data))
  255. .Rows.FirstOrDefault()?.ToObject<License>();
  256. if(license == null)
  257. {
  258. MessageBox.Show("No current license found. Please see the documentation on how to proceed.");
  259. return null;
  260. }
  261. if(!LicenseUtils.TryDecryptLicense(license.Data, out var data, out var error))
  262. {
  263. MessageBox.Show("Current license is corrupt. Please see the documentation on how to proceed.");
  264. return null;
  265. }
  266. return data;
  267. }
  268. private void RenewalPeriod_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  269. {
  270. CalculateDiscounts();
  271. }
  272. private void CalculateDiscounts()
  273. {
  274. var periodInMonths = RenewalPeriod;
  275. NewExpiry.Value = NewExpiration;
  276. double total = 0.0F;
  277. foreach (var row in Modules.Data.Rows)
  278. total += row.Get<LicenseTrackingItem, double>(x => x.ExGST) * periodInMonths;
  279. _gross = total;
  280. _discount = total * (LicenseUtils.GetUserDiscount(Modules.Licenses) / 100.0F) +
  281. total * (LicenseUtils.GetTimeDiscount(periodInMonths) / 100.0F);
  282. GrossLicenseFee.Value = Gross;
  283. DiscountEditor.Value = Discount;
  284. NettLicenseFee.Value = Net;
  285. }
  286. private class RenewalPeriodLookups : LookupGenerator<object>
  287. {
  288. public RenewalPeriodLookups(object[] items) : base(items)
  289. {
  290. }
  291. protected override void DoGenerateLookups()
  292. {
  293. foreach (var period in LicenseUtils.TimeDiscountLevels())
  294. AddValue(period, string.Format("{0} month{1}", period, period > 1 ? "s" : ""));
  295. }
  296. }
  297. private License GetNewLicense(LicenseRenewal licenseRenewal)
  298. {
  299. var license = LicenseTrackingItemGrid.LicenseClient.PostRequest<License>(licenseRenewal, "RenewLicense");
  300. if(license == null)
  301. {
  302. throw new Exception("The server did not return a valid license.");
  303. }
  304. return license;
  305. }
  306. private void SaveLicense(License license)
  307. {
  308. using var client = new Client<License>();
  309. var oldLicenses = client
  310. .Query(new Filter<License>().All(), new Columns<License>(x => x.ID))
  311. .Rows.Select(x => x.ToObject<License>()).ToList();
  312. client.Delete(oldLicenses, "");
  313. client.Save(license, "");
  314. }
  315. private void PayNowClick(object sender, RoutedEventArgs e)
  316. {
  317. if (!Client.Ping())
  318. {
  319. MessageBox.Show("The PRS server is not available right now. Please try again later.");
  320. return;
  321. }
  322. var companyInformation = new Client<CompanyInformation>().Load().FirstOrDefault();
  323. var isCompanyEmpty = string.IsNullOrWhiteSpace(companyInformation?.CompanyName);
  324. var isABNEmpty = string.IsNullOrWhiteSpace(companyInformation?.ABN);
  325. if (companyInformation == null || (isCompanyEmpty && isABNEmpty))
  326. {
  327. MessageBox.Show("The company information for this database has not been set. Please set company information before proceeding. The company name and ABN are required. This can be done through PRS Desktop, via System -> Company Information");
  328. return;
  329. }
  330. else if (isCompanyEmpty)
  331. {
  332. MessageBox.Show("Please set your company's name before proceeding. This can be done through PRS Desktop, via System -> Company Information");
  333. return;
  334. }
  335. else if (isABNEmpty)
  336. {
  337. MessageBox.Show("Please set your company's ABN before proceeding. This can be done through PRS Desktop, via System -> Company Information");
  338. return;
  339. }
  340. var ccdetails = new CreditCardDetails()
  341. {
  342. Amount = Net
  343. };
  344. var grid = new CreditCardGrid();
  345. if (grid.EditItems(new CreditCardDetails[] { ccdetails }))
  346. {
  347. var transactionID = grid.TransactionID;
  348. var licenseRenewal = new LicenseRenewal
  349. {
  350. Company = companyInformation,
  351. DateRenewed = RenewalDate,
  352. OldLicense = CurrentLicense,
  353. NewExpiry = NewExpiration,
  354. LicenseTracking = Modules.Data.Rows.Select(x => x.ToObject<LicenseTrackingItem>()).ToArray(),
  355. Gross = Gross,
  356. Discount = Discount,
  357. Net = Net,
  358. TransactionID = transactionID
  359. };
  360. string? error = null;
  361. License? license = null;
  362. Progress.ShowModal("Payment success, renewing license", progress =>
  363. {
  364. try
  365. {
  366. license = GetNewLicense(licenseRenewal);
  367. SaveLicense(license);
  368. }
  369. catch(Exception e)
  370. {
  371. error = $"There was an error retrieving your new license:\n\n{e.Message}\n\nPlease contact the PRS team, and keep your transaction ID for future reference. Your transaction ID is '{transactionID}'";
  372. }
  373. });
  374. if(error != null)
  375. {
  376. MessageBox.Show(error);
  377. }
  378. else
  379. {
  380. MessageBox.Show("License Renewed!");
  381. if(!LicenseUtils.TryDecryptLicense(license.Data, out var licenseData, out error))
  382. {
  383. MessageBox.Show(error);
  384. }
  385. CurrentLicense = licenseData ?? new LicenseData();
  386. }
  387. }
  388. }
  389. private void OK_Click(object sender, RoutedEventArgs e)
  390. {
  391. DialogResult = true;
  392. Close();
  393. }
  394. private void Cancel_Click(object sender, RoutedEventArgs e)
  395. {
  396. DialogResult = false;
  397. Close();
  398. }
  399. }
  400. }