diff --git a/BodyshopUploader/BodyshopPartner.csproj b/BodyshopUploader/BodyshopPartner.csproj index 67c5fcc..0f52710 100644 --- a/BodyshopUploader/BodyshopPartner.csproj +++ b/BodyshopUploader/BodyshopPartner.csproj @@ -327,6 +327,8 @@ + + Resources.es-MX.resx diff --git a/BodyshopUploader/Models/QbRequestItem.cs b/BodyshopUploader/Models/QbRequestItem.cs new file mode 100644 index 0000000..c96f4e3 --- /dev/null +++ b/BodyshopUploader/Models/QbRequestItem.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace BodyshopPartner.Models +{ + public class QbRequestItem : DTO_Base + { + public QbRequestItem() + { + + } + + private string _id; + [JsonProperty("id")] + public string Id + { + get { return _id; } + set { SetProperty(ref _id, value); } + } + + private string[] _okStatusCodes; + [JsonProperty("OkStatusCodes")] + public string[] OkStatusCodes + { + get { return _okStatusCodes; } + set { SetProperty(ref _okStatusCodes, value); } + } + + private string _qbxml; + [JsonProperty("qbxml")] + public string QbXML + { + get { return _qbxml; } + set { SetProperty(ref _qbxml, value); } + } + } + + +} diff --git a/BodyshopUploader/Models/QbResponseItem.cs b/BodyshopUploader/Models/QbResponseItem.cs new file mode 100644 index 0000000..9c43cde --- /dev/null +++ b/BodyshopUploader/Models/QbResponseItem.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace BodyshopPartner.Models +{ + public class QbResponseItem : DTO_Base + { + public QbResponseItem() + { + + } + + private string _id; + [JsonProperty("id")] + public string Id + { + get { return _id; } + set { SetProperty(ref _id, value); } + } + + private bool _success; + [JsonProperty("success")] + public bool Success + { + get { return _success; } + set { SetProperty(ref _success, value); } + } + + private string _errorMessage; + [JsonProperty("errorMessage")] + public string ErrorMessage + { + get { return _errorMessage; } + set { SetProperty(ref _errorMessage, value); } + } + } +} diff --git a/BodyshopUploader/Utils/HTTPServer.cs b/BodyshopUploader/Utils/HTTPServer.cs index 8134683..0999aee 100644 --- a/BodyshopUploader/Utils/HTTPServer.cs +++ b/BodyshopUploader/Utils/HTTPServer.cs @@ -8,6 +8,7 @@ using System.Xml.Linq; using Newtonsoft; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using BodyshopPartner.Models; namespace BodyshopPartner.Utils { @@ -38,26 +39,48 @@ namespace BodyshopPartner.Utils private static void HandleQbPost(System.Net.HttpListenerRequest req, System.Net.HttpListenerResponse res) { - logger.Debug("/qb/ - POST"); - //Input will be an array of XMLs. - - string[] requestXMLstrings = ParseBodyToArray(req); + logger.Trace("/qb/ - POST"); hlog("Received QuickBooks Receivable request."); + //Input will be an array of objects containing XMLs. + + List AllRequests = ParseRequest(req); + List HttpResponse = new List(); // string requestXMLstring = ParseBodyToString(req); - foreach (var requestXMLstring in requestXMLstrings) + foreach (QbRequestItem request in AllRequests) { - XDocument requestXMLdoc = ParseStringToXml(requestXMLstring); - XDocument response = XDocument.Parse(QuickBooksInterop.ProcessQBXmlRequest(requestXMLstring)); - Models.QbXmlResponse ResponseStatus = QuickBooksInterop.ParseResponseXml(response); - logger.Info(response.ToString()); - logger.Info(ResponseStatus.ToString()); + XDocument response = XDocument.Parse(QuickBooksInterop.ProcessQBXmlRequest(request.QbXML)); + QbXmlResponse ResponseStatus = QuickBooksInterop.ParseResponseXml(response); + + logger.Trace(response.ToString()); + logger.Debug(ResponseStatus.ToString()); hlog(ResponseStatus.ToString()); + QbResponseItem r = new QbResponseItem() + { + Id = request.Id, + Success = ResponseStatus.StatusCode == "0" || request.OkStatusCodes.Contains(ResponseStatus.StatusCode), + ErrorMessage = ResponseStatus.StatusMessage + }; + + HttpResponse.Add(r); } hlog("Completed QuickBooks request."); - res.WithCORS().Close(); + + res.WithCORS().AsText(JsonConvert.SerializeObject(HttpResponse)); + } + + public static List ParseRequest(System.Net.HttpListenerRequest req) + { + System.IO.Stream body = req.InputStream; + System.Text.Encoding encoding = req.ContentEncoding; + System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); + string s = reader.ReadToEnd(); + body.Close(); + reader.Close(); + + return JsonConvert.DeserializeObject>(s); } public static string[] ParseBodyToArray(System.Net.HttpListenerRequest req)