| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- using System;
- using System.Collections.Generic;
- using System.IO.Compression;
- using System.IO;
- using System.Linq;
- using System.Security.Policy;
- using System.Threading;
- using System.Windows;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.Exchange.WebServices.Data;
- using PRSServer.Forms.DatabaseLicense;
- using RestSharp;
- using Stripe;
- using DataFormat = RestSharp.DataFormat;
- using System.Net;
- using FastReport.DevComponents.DotNetBar;
- using System.Diagnostics.CodeAnalysis;
- using InABox.Database;
- using System.Runtime.CompilerServices;
- using System.Windows.Controls;
- using System.Drawing;
- using Image = System.Windows.Controls.Image;
- using System.Diagnostics;
- using Twilio.Rest.Api.V2010.Account;
- namespace PRSServer.Forms.DatabaseLicense
- {
- public class CreditCardGrid : DynamicGrid<CreditCardDetails>
- {
-
- protected override void Reload(Filters<CreditCardDetails> criteria, Columns<CreditCardDetails> columns, ref SortOrder<CreditCardDetails>? sort, Action<CoreTable, Exception> action)
- {
-
- }
- protected override CreditCardDetails LoadItem(CoreRow row)
- {
- return new CreditCardDetails();
- }
- public string TransactionID = "";
- protected override void DoValidate(CreditCardDetails[] items, List<string> errors)
- {
- base.DoValidate(items, errors);
- if (items.Any(x => string.IsNullOrWhiteSpace(x.CardNumber)))
- errors.Add("Please supply a card number.");
- if (items.Any(x => string.IsNullOrWhiteSpace(x.Month)))
- errors.Add("Please supply an expiration month.");
- if (items.Any(x => string.IsNullOrWhiteSpace(x.Year)))
- errors.Add("Please supply an expiration year.");
- if (items.Any(x => string.IsNullOrWhiteSpace(x.Cvv)))
- errors.Add("Please supply a CVV.");
- }
- protected override void SaveItem(CreditCardDetails item)
- {
- TransactionID = "";
- var error = "";
- Progress.ShowModal("Processing Payment", (progress) =>
- {
- try
- {
- StripeConfiguration.ApiKey = "sk_test_51Lc39sA3OFNLFxjFvH2PYwCpl9DNhkIoce3rskpTXOvoxiFQUIgtYg1MRWrPUIx14lcs9oG4pFVco9UvRSxen0mT00P8AisayJ";
- var cardoptions = new TokenCreateOptions()
- {
- Card = new TokenCardOptions()
- {
- Number = item.CardNumber,
- ExpMonth = item.Month,
- ExpYear = item.Year,
- Cvc = item.Cvv
- }
- };
- var service = new TokenService();
- var token = service.Create(cardoptions);
- var chargeoptions = new ChargeCreateOptions()
- {
- Amount = Convert.ToInt64(item.Amount * 100),
- Currency = "AUD",
- Description = "PRS Renewal",
- Source = token.Id
- };
- var chargeservice = new ChargeService();
- var charge = chargeservice.Create(chargeoptions);
- if (charge.Paid)
- TransactionID = charge.Id;
- else
- error = string.Format("{0}: {1}", charge.FailureCode, charge.FailureMessage);
- }
- catch (Exception ex)
- {
- error = $"{ex.Message}";
- }
- });
- //if (!String.IsNullOrWhiteSpace(error))
- // throw new Exception(error);
- }
- protected override void DeleteItems(params CoreRow[] rows)
- {
-
- }
- }
-
- /// <summary>
- /// Interaction logic for LicenseRenewal.xaml
- /// </summary>
- public partial class LicenseRenewalForm : ThemableWindow
- {
- private LicenseData? _currentLicense;
- private double _gross;
- private double _discount;
- private DateTime _renewalAvailable;
- public LicenseData? CurrentLicense
- {
- get => _currentLicense;
- set
- {
- if(value == null)
- {
- return;
- }
- PayWithStripe.IsEnabled = false;
- PayTooltip.Visibility = Visibility.Visible;
- PayTooltip.Content = new Label { Content = "Loading..." };
- _currentLicense = value;
- LastRenewal.Value = value.LastRenewal;
- CurrentExpiry.Value = value.Expiry;
- Modules.Renewed = value.LastRenewal;
- RenewalAvailableFrom = value.RenewalAvailable;
- Modules.Refresh(false, true);
- }
- }
- public int RenewalPeriod
- {
- get
- {
- if (int.TryParse(RenewalPeriodEditor.Value?.ToString(), out var period))
- {
- return period;
- }
- return 0;
- }
- set
- {
- RenewalPeriodEditor.SetValue(value);
- CalculateDiscounts();
- }
- }
- public DateTime RenewalDate
- {
- get
- {
- if(CurrentLicense == null)
- {
- return DateTime.MinValue;
- }
- if (CurrentLicense.Expiry > DateTime.Now)
- {
- return CurrentLicense.Expiry.Date;
- }
- return DateTime.Today;
- }
- }
- public DateTime RenewalAvailableFrom
- {
- get => _renewalAvailable;
- set
- {
- _renewalAvailable = value;
- if (!CanRenew)
- {
- RenewalAvailableLabel.Content = $"Renewal available from {value:dd MMM yyyy}";
- }
- }
- }
- public bool CanRenew => RenewalAvailableFrom.Date <= DateTime.Today || (CurrentLicense != null && CurrentLicense.Expiry <= DateTime.Now);
- public DateTime NewExpiration
- {
- get => RenewalDate.AddMonths(RenewalPeriod);
- }
- public double Gross { get => _gross; }
- public double Discount { get => _discount; }
- public double Net { get => _gross - _discount; }
- public LicenseRenewalForm()
- {
- InitializeComponent();
- LastRenewal.EditorDefinition = new DateEditor();
- LastRenewal.Configure();
- LastRenewal.IsEnabled = false;
- CurrentExpiry.EditorDefinition = new DateEditor();
- CurrentExpiry.Configure();
- CurrentExpiry.IsEnabled = false;
- RenewalPeriodEditor.EditorDefinition = new ComboLookupEditor(typeof(RenewalPeriodLookups));
- RenewalPeriodEditor.Configure();
- var lookups = new RenewalPeriodLookups(null).AsTable("RenewalPeriod");
- RenewalPeriodEditor.LoadLookups(lookups);
- NewExpiry.EditorDefinition = new DateEditor();
- NewExpiry.Configure();
- NewExpiry.IsEnabled = false;
- GrossLicenseFee.EditorDefinition = new CurrencyEditor();
- GrossLicenseFee.Configure();
- GrossLicenseFee.IsEnabled = false;
- DiscountEditor.EditorDefinition = new DoubleEditor();
- DiscountEditor.Configure();
- DiscountEditor.IsEnabled = false;
- NettLicenseFee.EditorDefinition = new CurrencyEditor();
- NettLicenseFee.Configure();
- NettLicenseFee.IsEnabled = false;
- Help.Content = new Image { Source = Properties.Resources.help.AsBitmapImage(Color.White) };
- Help.Click += Help_Click;
- Modules.OnAfterReload += Modules_OnAfterReload;
- //double months = Math.Max(0.0F,CalcMonths(CurrentExpiry.Value, NewExpiry.Value));
- //double total = 0.0F;
- //foreach (var row in Modules.Data.Rows)
- // total += row.Get<LicenseRenewal, double>(x => x.ExGST) * months;
- //GrossLicenseFee.Value = total;
- ////GrossLicenseFee.Width = 150;
- //Discount.Value = LicenseUtils.GetUserDiscount(Modules.Licenses);
- ////Discount.Width = 150;
- //NettLicenseFee.Value = total * ((100.0F - Discount.Value) / 100.0F);
- ////NettLicenseFee.Width = 150;
- }
- private void Help_Click(object sender, RoutedEventArgs e)
- {
- Process.Start(new ProcessStartInfo("https://prs-software.com.au/wiki/index.php/License_Renewal") { UseShellExecute = true });
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- Modules.Refresh(true, false);
- var license = LoadCurrentLicense();
- if(license != null)
- {
- CurrentLicense = license;
- }
- LastRenewal.Loaded = true;
- CurrentExpiry.Loaded = true;
- NewExpiry.Loaded = true;
- RenewalPeriodEditor.Loaded = true;
- }
- private void Modules_OnAfterReload(object sender)
- {
- if (CurrentLicense == null) return;
- RenewalPeriod = LicenseUtils.TimeDiscountLevels().OrderBy(x => x).First();
- PayWithStripe.IsEnabled = CanRenew;
- if (!PayWithStripe.IsEnabled)
- {
- PayTooltip.Visibility = Visibility.Visible;
- PayTooltip.Content = new Label { Content = $"Renewal available from {RenewalAvailableFrom:dd MMM yyyy}" };
- }
- else
- {
- PayTooltip.Visibility = Visibility.Collapsed;
- }
- OK.IsEnabled = true;
- }
- private static LicenseData? LoadCurrentLicense()
- {
- var license = new Client<License>().Query(new Filter<License>().All(), new Columns<License>(x => x.Data))
- .Rows.FirstOrDefault()?.ToObject<License>();
- if(license == null)
- {
- MessageBox.Show("No current license found. Please see the documentation on how to proceed.");
- return null;
- }
- if(!LicenseUtils.TryDecryptLicense(license.Data, out var data, out var error))
- {
- MessageBox.Show("Current license is corrupt. Please see the documentation on how to proceed.");
- return null;
- }
- return data;
- }
- private void RenewalPeriod_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
- {
- CalculateDiscounts();
- }
-
- private void CalculateDiscounts()
- {
- var periodInMonths = RenewalPeriod;
-
- NewExpiry.Value = NewExpiration;
- double total = 0.0F;
- foreach (var row in Modules.Data.Rows)
- total += row.Get<LicenseTrackingItem, double>(x => x.ExGST) * periodInMonths;
- _gross = total;
- _discount = total * (LicenseUtils.GetUserDiscount(Modules.Licenses) / 100.0F) +
- total * (LicenseUtils.GetTimeDiscount(periodInMonths) / 100.0F);
- GrossLicenseFee.Value = Gross;
- DiscountEditor.Value = Discount;
- NettLicenseFee.Value = Net;
- }
- private class RenewalPeriodLookups : LookupGenerator<object>
- {
- public RenewalPeriodLookups(object[] items) : base(items)
- {
- }
- protected override void DoGenerateLookups()
- {
- foreach (var period in LicenseUtils.TimeDiscountLevels())
- AddValue(period, string.Format("{0} month{1}", period, period > 1 ? "s" : ""));
- }
- }
- private License GetNewLicense(LicenseRenewal licenseRenewal)
- {
- var license = LicenseTrackingItemGrid.LicenseClient.PostRequest<License>(licenseRenewal, "RenewLicense");
- if(license == null)
- {
- throw new Exception("The server did not return a valid license.");
- }
- return license;
- }
- private void SaveLicense(License license)
- {
- using var client = new Client<License>();
- var oldLicenses = client
- .Query(new Filter<License>().All(), new Columns<License>(x => x.ID))
- .Rows.Select(x => x.ToObject<License>()).ToList();
- client.Delete(oldLicenses, "");
- client.Save(license, "");
- }
- private void PayNowClick(object sender, RoutedEventArgs e)
- {
- if (!Client.Ping())
- {
- MessageBox.Show("The PRS server is not available right now. Please try again later.");
- return;
- }
- var companyInformation = new Client<CompanyInformation>().Load().FirstOrDefault();
- var isCompanyEmpty = string.IsNullOrWhiteSpace(companyInformation?.CompanyName);
- var isABNEmpty = string.IsNullOrWhiteSpace(companyInformation?.ABN);
- if (companyInformation == null || (isCompanyEmpty && isABNEmpty))
- {
- 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");
- return;
- }
- else if (isCompanyEmpty)
- {
- MessageBox.Show("Please set your company's name before proceeding. This can be done through PRS Desktop, via System -> Company Information");
- return;
- }
- else if (isABNEmpty)
- {
- MessageBox.Show("Please set your company's ABN before proceeding. This can be done through PRS Desktop, via System -> Company Information");
- return;
- }
- var ccdetails = new CreditCardDetails()
- {
- Amount = Net
- };
- var grid = new CreditCardGrid();
- if (grid.EditItems(new CreditCardDetails[] { ccdetails }))
- {
- var transactionID = grid.TransactionID;
- var licenseRenewal = new LicenseRenewal
- {
- Company = companyInformation,
- DateRenewed = RenewalDate,
- OldLicense = CurrentLicense,
- NewExpiry = NewExpiration,
- LicenseTracking = Modules.Data.Rows.Select(x => x.ToObject<LicenseTrackingItem>()).ToArray(),
- Gross = Gross,
- Discount = Discount,
- Net = Net,
- TransactionID = transactionID
- };
- string? error = null;
- License? license = null;
- Progress.ShowModal("Payment success, renewing license", progress =>
- {
- try
- {
- license = GetNewLicense(licenseRenewal);
- SaveLicense(license);
- }
- catch(Exception e)
- {
- 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}'";
- }
- });
- if(error != null)
- {
- MessageBox.Show(error);
- }
- else
- {
- MessageBox.Show("License Renewed!");
- if(!LicenseUtils.TryDecryptLicense(license.Data, out var licenseData, out error))
- {
- MessageBox.Show(error);
- }
- CurrentLicense = licenseData ?? new LicenseData();
- }
- }
- }
- private void OK_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = true;
- Close();
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- Close();
- }
- }
- }
|