|
@@ -1,5 +1,6 @@
|
|
|
using InABox.Core;
|
|
|
using InABox.Core.Postable;
|
|
|
+using Microsoft.Web.WebView2.Wpf;
|
|
|
using MYOB.AccountRight.SDK;
|
|
|
using MYOB.AccountRight.SDK.Contracts;
|
|
|
using MYOB.AccountRight.SDK.Services;
|
|
@@ -12,6 +13,7 @@ using System.Threading.Tasks;
|
|
|
using System.Web;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
+using System.Windows.Threading;
|
|
|
|
|
|
namespace InABox.Poster.MYOB;
|
|
|
|
|
@@ -30,12 +32,7 @@ public class MYOBConnectionData(ApiConfiguration configuration, CompanyFileServi
|
|
|
public IOAuthKeyService AuthKey { get; set; } = authKey;
|
|
|
}
|
|
|
|
|
|
-public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
- PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
|
|
|
- IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
|
|
|
-
|
|
|
- where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
|
|
|
- where TSettings : MYOBPosterSettings, new()
|
|
|
+public static partial class MYOBPosterEngine
|
|
|
{
|
|
|
internal static readonly string DEV_KEY = "f3b27f88-2ef9-4d8e-95c1-d0a045d0afee";
|
|
|
internal static readonly string SECRET_KEY = "ksm0e8yo6oumcPb63A8cduaN";
|
|
@@ -45,13 +42,6 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
|
|
|
private static MYOBConnectionData? _connectionData;
|
|
|
|
|
|
- private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).GetGlobalSettings();
|
|
|
-
|
|
|
- public override bool BeforePost(IDataModel<TPostable> model)
|
|
|
- {
|
|
|
- return Poster.BeforePost(model);
|
|
|
- }
|
|
|
-
|
|
|
public static bool GetAuthorisationCode(IApiConfiguration config, out string? code)
|
|
|
{
|
|
|
var url = $"{AUTH_URL}?client_id={config.ClientId}&redirect_uri={HttpUtility.UrlEncode(config.RedirectUrl)}&scope={AUTH_SCOPE}&response_type=code";
|
|
@@ -64,24 +54,36 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
};
|
|
|
|
|
|
string? resultCode = null;
|
|
|
- var browser = new System.Windows.Forms.WebBrowser();
|
|
|
- browser.DocumentTitleChanged += (o, e) =>
|
|
|
+
|
|
|
+ var view = new WebView2
|
|
|
+ {
|
|
|
+ };
|
|
|
+ view.NavigationCompleted += (o, e) =>
|
|
|
{
|
|
|
- var htmlText = browser.DocumentText;
|
|
|
- if(htmlText is not null)
|
|
|
+ view.ExecuteScriptAsync("document.documentElement.innerHTML;").ContinueWith(task =>
|
|
|
{
|
|
|
- var match = CodeRegex().Match(htmlText);
|
|
|
- if (match.Success)
|
|
|
+ if (task.Result is null) return;
|
|
|
+
|
|
|
+ var str = Serialization.Deserialize<string>(task.Result);
|
|
|
+ if (str is null) return;
|
|
|
+
|
|
|
+ var match = CodeRegex().Match(str);
|
|
|
+ if (!match.Success) return;
|
|
|
+
|
|
|
+ resultCode = match.Groups[1].Value;
|
|
|
+ window.Dispatcher.BeginInvoke(() =>
|
|
|
{
|
|
|
- resultCode = match.Groups[1].Value;
|
|
|
- window.DialogResult = true;
|
|
|
- window.Close();
|
|
|
- }
|
|
|
- }
|
|
|
+ if(window.DialogResult is null)
|
|
|
+ {
|
|
|
+ window.DialogResult = true;
|
|
|
+ window.Close();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
};
|
|
|
+ view.Source = new Uri(url);
|
|
|
+ window.Content = view;
|
|
|
|
|
|
- browser.Navigate(url);
|
|
|
- window.Content = browser;
|
|
|
if(window.ShowDialog() == true)
|
|
|
{
|
|
|
code = resultCode;
|
|
@@ -94,7 +96,7 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static MYOBConnectionData GetConnectionData()
|
|
|
+ public static MYOBConnectionData GetConnectionData()
|
|
|
{
|
|
|
if(_connectionData is MYOBConnectionData data)
|
|
|
{
|
|
@@ -124,23 +126,43 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
|
|
|
return _connectionData;
|
|
|
}
|
|
|
+
|
|
|
+ [GeneratedRegex("code=(.*)<")]
|
|
|
+ private static partial Regex CodeRegex();
|
|
|
+}
|
|
|
+
|
|
|
+public abstract class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
+ PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
|
|
|
+ IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
|
|
|
+
|
|
|
+ where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
|
|
|
+ where TSettings : MYOBPosterSettings, new()
|
|
|
+{
|
|
|
+ private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).GetGlobalSettings();
|
|
|
+
|
|
|
+ public override bool BeforePost(IDataModel<TPostable> model)
|
|
|
+ {
|
|
|
+ return Poster.BeforePost(model);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
|
|
|
{
|
|
|
- var data = GetConnectionData();
|
|
|
+ var data = MYOBPosterEngine.GetConnectionData();
|
|
|
|
|
|
var globalSettings = GetGlobalSettings();
|
|
|
- if(data.CompanyFile is null || data.CompanyFile.Id != globalSettings.CompanyFileID)
|
|
|
+ var companyFileID = globalSettings.GetCompanyFileID();
|
|
|
+ if(data.CompanyFile is null || data.CompanyFile.Id != companyFileID)
|
|
|
{
|
|
|
- if(globalSettings.CompanyFileID == Guid.Empty)
|
|
|
+ if(companyFileID == Guid.Empty)
|
|
|
{
|
|
|
throw new PostFailedMessageException("No CompanyFileID has been set.");
|
|
|
}
|
|
|
- else if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace() || globalSettings.CompanyFilePassword.IsNullOrWhiteSpace())
|
|
|
+ else if(globalSettings.CompanyFileUserID.IsNullOrWhiteSpace())
|
|
|
{
|
|
|
throw new PostFailedMessageException("CompanyFile credentials have not been set.");
|
|
|
}
|
|
|
- var companyFile = data.CompanyFileService.GetRange().FirstOrDefault(x => x.Id == globalSettings.CompanyFileID);
|
|
|
+ var companyFile = data.CompanyFileService.GetRange().FirstOrDefault(x => x.Id == companyFileID);
|
|
|
var fileCredentials = new CompanyFileCredentials(globalSettings.CompanyFileUserID, globalSettings.CompanyFilePassword);
|
|
|
data.CompanyFile = companyFile;
|
|
|
data.CompanyFileCredentials = fileCredentials;
|
|
@@ -154,7 +176,4 @@ public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
|
|
|
public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
|
|
|
{
|
|
|
}
|
|
|
-
|
|
|
- [GeneratedRegex("code=(.*)<")]
|
|
|
- private static partial Regex CodeRegex();
|
|
|
}
|