diff --git a/.idea/.idea.MedusaPluginChargeMachine/.idea/.gitignore b/.idea/.idea.MedusaPluginChargeMachine/.idea/.gitignore new file mode 100644 index 0000000..d1560c0 --- /dev/null +++ b/.idea/.idea.MedusaPluginChargeMachine/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/modules.xml +/.idea.MedusaPluginChargeMachine.iml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.MedusaPluginChargeMachine/.idea/encodings.xml b/.idea/.idea.MedusaPluginChargeMachine/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.MedusaPluginChargeMachine/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.MedusaPluginChargeMachine/.idea/indexLayout.xml b/.idea/.idea.MedusaPluginChargeMachine/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.MedusaPluginChargeMachine/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.MedusaPluginChargeMachine/.idea/vcs.xml b/.idea/.idea.MedusaPluginChargeMachine/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.MedusaPluginChargeMachine/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MedusaPluginChargeMachine.slnx b/MedusaPluginChargeMachine.slnx new file mode 100644 index 0000000..6e1a77d --- /dev/null +++ b/MedusaPluginChargeMachine.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MedusaPluginChargeMachine/ChargeMachinePlugin.cs b/MedusaPluginChargeMachine/ChargeMachinePlugin.cs new file mode 100644 index 0000000..88e5613 --- /dev/null +++ b/MedusaPluginChargeMachine/ChargeMachinePlugin.cs @@ -0,0 +1,25 @@ +using Abstractions; +using MedusaPluginChargeMachine.Services; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace MedusaPluginChargeMachine; + +public class ChargeMachinePlugin: IMedusaPlugin +{ + public string Name => "Charge Machine"; + public string Version => "1.0.0"; + public string Description => "Plugin for the paseli charge machine"; + + public Task OnBuilderInitialize(WebApplicationBuilder builder) + { + builder.Services.AddTransient(); + + return Task.CompletedTask; + } + + public Task OnAppInitialize(WebApplication app) + { + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/MedusaPluginChargeMachine/Handlers/LA9/ChargeEaChargeHandler.cs b/MedusaPluginChargeMachine/Handlers/LA9/ChargeEaChargeHandler.cs new file mode 100644 index 0000000..96022c0 --- /dev/null +++ b/MedusaPluginChargeMachine/Handlers/LA9/ChargeEaChargeHandler.cs @@ -0,0 +1,42 @@ +using System.Xml.Linq; +using Abstractions.Entities; +using Abstractions.Handlers; +using Abstractions.Services; +using Microsoft.AspNetCore.Identity; +using ISessionService = MedusaPluginChargeMachine.Services.ISessionService; + +namespace MedusaPluginChargeMachine.Handlers.LA9 +{ + public class ChargeEaChargeHandler(ICardService cardService, ISessionService sessionService, UserManager userManager, XDocument body): Handler + { + public override void Configure() + { + Module("eacharge"); + Method("charge"); + } + public override async Task HandleAsync(string model) + { + var rootCall = body.Root; + var charge = rootCall?.Element("eacharge")!; + var amount = int.Parse(charge.Element("chargenum")?.Value!) * 1000; + var session = charge.Element("sessid")?.Value!; + + var foundSession = sessionService.GetSession(session); + + var card = await cardService.FindByCardId(foundSession.refId)!; + + card!.User.PaseliAmount += amount; + + await userManager.UpdateAsync(card.User); + + var balance = new XElement("balance", card.User.PaseliAmount, new XAttribute("__type", "s32")); + var chargeDailyLimit = new XElement("chargedailylimit", 100000, new XAttribute("__type", "s32")); + var chargeDaily = new XElement("chargedaily", 0, new XAttribute("__type", "s32")); + var chargepoint = new XElement("chargepoint", amount, new XAttribute("__type", "s32")); + + var eaCharge = new XElement("charge", balance, chargeDailyLimit, chargeDaily, chargepoint, new XAttribute("status", "0")); + var document = new XDocument(new XElement("response", eaCharge)); + return document; + } + } +} diff --git a/MedusaPluginChargeMachine/Handlers/LA9/CheckInEaChargeHandler.cs b/MedusaPluginChargeMachine/Handlers/LA9/CheckInEaChargeHandler.cs new file mode 100644 index 0000000..b08e565 --- /dev/null +++ b/MedusaPluginChargeMachine/Handlers/LA9/CheckInEaChargeHandler.cs @@ -0,0 +1,41 @@ +using System.Xml.Linq; +using Abstractions.Handlers; +using Abstractions.Services; +using MedusaPluginChargeMachine.Services; + +namespace MedusaPluginChargeMachine.Handlers.LA9 +{ + public class CheckInEaChargeHandler(ISessionService sessionService, ICardService cardService, XDocument body): Handler + { + private readonly XDocument _body = body; + private readonly ISessionService _sessionService = sessionService; + private readonly ICardService _cardService = cardService; + + public override void Configure() + { + Module("eacharge"); + Method("checkin"); + } + public override async Task HandleAsync(string model) + { + var rootCall = _body.Root; + var eaCharge = rootCall?.Element("eacharge")!; + var cardId = eaCharge.Element("cardid")?.Value!; + + var sessionId = _sessionService.AddSession(cardId); + + var card = await _cardService.FindByCardId(cardId); + + var userPaseli = card!.User.PaseliAmount; + + var sessseq = new XElement("sessseq", 0, new XAttribute("__type", "s32")); + var acname = new XElement("acname", "DUMMY_NAME", new XAttribute("__type", "str")); + var balance = new XElement("balance", userPaseli, new XAttribute("__type", "s32")); + var buyablep = new XElement("buyablep", 100000, new XAttribute("__type", "s32")); + var sessionIdXElement = new XElement("sessid", sessionId, new XAttribute("__type", "str")); + var eacharge = new XElement("eacharge", sessseq, acname, balance, buyablep, sessionIdXElement, new XAttribute("status", "0")); + var document = new XDocument(new XElement("response", eacharge)); + return document; + } + } +} diff --git a/MedusaPluginChargeMachine/Handlers/LA9/CheckoutEaCharge.cs b/MedusaPluginChargeMachine/Handlers/LA9/CheckoutEaCharge.cs new file mode 100644 index 0000000..e95220c --- /dev/null +++ b/MedusaPluginChargeMachine/Handlers/LA9/CheckoutEaCharge.cs @@ -0,0 +1,29 @@ +using System.Xml.Linq; +using Abstractions.Handlers; +using Abstractions.Services; +using ISessionService = MedusaPluginChargeMachine.Services.ISessionService; + +namespace MedusaPluginChargeMachine.Handlers.LA9 +{ + public class CheckoutEaCharge(ISessionService sessionService, XDocument body): Handler + { + private readonly ISessionService _sessionService = sessionService; + private readonly XDocument _body = body; + public override void Configure() + { + Module("eacharge"); + Method("checkout"); + } + public override XDocument Handle(string model) + { + var eaCharge = _body.Root?.Element("eacharge")!; + var sessionId = eaCharge.Element("sessid")!.Value!; + + _sessionService.RemoveSession(sessionId); + + var checkoutCharge = new XElement("eacharge", new XAttribute("status", "0")); + var document = new XDocument(new XElement("response", checkoutCharge)); + return document; + } + } +} diff --git a/MedusaPluginChargeMachine/Handlers/LA9/GetConfigEaChargeHandler.cs b/MedusaPluginChargeMachine/Handlers/LA9/GetConfigEaChargeHandler.cs new file mode 100644 index 0000000..30f6064 --- /dev/null +++ b/MedusaPluginChargeMachine/Handlers/LA9/GetConfigEaChargeHandler.cs @@ -0,0 +1,95 @@ +using System.Xml.Linq; +using Abstractions.Handlers; + +namespace MedusaPluginChargeMachine.Handlers.LA9 +{ + public class GetConfigEaChargeHandler(XDocument body): Handler + { + private readonly XDocument _body = body; + + public override void Configure() + { + Module("eacharge"); + Method("getconf"); + } + + public override XDocument Handle(string model) + { + var articlerevision = new XElement("articlerevision", 1, new XAttribute("__type", "str")); + + var articletype1 = new XElement("articletype", "DOCMT_0000", new XAttribute("__type", "str")); + var articledoc1 = new XElement("articledoc", new XAttribute("__type", "str")); + var item1 = new XElement("item", articletype1, articledoc1); + + var articletype2 = new XElement("articletype", "DOCMT_0001", new XAttribute("__type", "str")); + var articledoc2 = new XElement("articledoc", new XAttribute("__type", "str")); + var item2 = new XElement("item", articletype2, articledoc2); + + var articletype3 = new XElement("articletype", "DOCMT_0002", new XAttribute("__type", "str")); + var articledoc3 = new XElement("articledoc", new XAttribute("__type", "str")); + var item3 = new XElement("item", articletype3, articledoc3); + + var articletype4 = new XElement("articletype", "DOCMT_0003", new XAttribute("__type", "str")); + var articledoc4 = new XElement("articledoc", new XAttribute("__type", "str")); + var item4 = new XElement("item", articletype4, articledoc4); + + var articletype5 = new XElement("articletype", "URL_0001_A", new XAttribute("__type", "str")); + var articledoc5 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item5 = new XElement("item", articletype5, articledoc5); + + var articletype6 = new XElement("articletype", "URL_0001_B", new XAttribute("__type", "str")); + var articledoc6 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item6 = new XElement("item", articletype6, articledoc6); + + var articletype7 = new XElement("articletype", "URL_0002_A", new XAttribute("__type", "str")); + var articledoc7 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item7 = new XElement("item", articletype7, articledoc7); + + var articletype8 = new XElement("articletype", "URL_0002_B", new XAttribute("__type", "str")); + var articledoc8 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item8 = new XElement("item", articletype8, articledoc8); + + var articletype9 = new XElement("articletype", "URL_0003_A", new XAttribute("__type", "str")); + var articledoc9 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item9 = new XElement("item", articletype9, articledoc9); + + var articletype10 = new XElement("articletype", "URL_0003_B", new XAttribute("__type", "str")); + var articledoc10 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item10 = new XElement("item", articletype10, articledoc10); + + var articletype11 = new XElement("articletype", "URL_0004_A", new XAttribute("__type", "str")); + var articledoc11 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item11 = new XElement("item", articletype11, articledoc11); + + var articletype12 = new XElement("articletype", "URL_0004_B", new XAttribute("__type", "str")); + var articledoc12 = new XElement("articledoc", "http://eagate.573.jp", new XAttribute("__type", "str")); + var item12 = new XElement("item", articletype12, articledoc12); + + var articletype13 = new XElement("articletype", "PASELIUNIT", new XAttribute("__type", "str")); + var articledoc13 = new XElement("articledoc", "P", new XAttribute("__type", "str")); + var item13 = new XElement("item", articletype13, articledoc13); + + var articlelist = new XElement("articlelist", item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13); + var chargearticle = new XElement("chargearticle", articlerevision, articlelist); + + var chargedaily = new XElement("chargedaily", 0, new XAttribute("__type", "s32")); + var chargedailylimit = new XElement("chargedailylimit", 100000, new XAttribute("__type", "s32")); + var chargedetail = new XElement("chargedetail", chargedaily, chargedailylimit); + + var currentcode = new XElement("currentcode", 0, new XAttribute("__type", "s32")); + var lastchargestatus = new XElement("lastchargestatus", 1, new XAttribute("__type", "s32")); + var chargestatus = new XElement("chargestatus", currentcode, lastchargestatus); + + var eacharge = new XElement("eacharge", + new XAttribute("status", "0"), new XAttribute("expire", "1200"), chargearticle, chargedetail, chargestatus); + + var document = new XDocument( + new XElement("response", + eacharge + ) + ); + + return document; + } + } +} diff --git a/MedusaPluginChargeMachine/MedusaPluginChargeMachine.csproj b/MedusaPluginChargeMachine/MedusaPluginChargeMachine.csproj new file mode 100644 index 0000000..26022c5 --- /dev/null +++ b/MedusaPluginChargeMachine/MedusaPluginChargeMachine.csproj @@ -0,0 +1,25 @@ + + + + net9.0 + enable + enable + + + + + ..\..\Medusa.net\Abstractions\bin\Debug\net9.0\Abstractions.dll + + + + + + + + + + + + + + diff --git a/MedusaPluginChargeMachine/Services/ISessionService.cs b/MedusaPluginChargeMachine/Services/ISessionService.cs new file mode 100644 index 0000000..d08cb04 --- /dev/null +++ b/MedusaPluginChargeMachine/Services/ISessionService.cs @@ -0,0 +1,10 @@ +namespace MedusaPluginChargeMachine.Services +{ + public interface ISessionService + { + public void AddSession(string sessionId, string refId); + public string AddSession(string cardId); + public (string sessionId, string refId) GetSession(string sessionId); + public void RemoveSession(string sessionId); + } +} diff --git a/MedusaPluginChargeMachine/Services/SessionService.cs b/MedusaPluginChargeMachine/Services/SessionService.cs new file mode 100644 index 0000000..09e3906 --- /dev/null +++ b/MedusaPluginChargeMachine/Services/SessionService.cs @@ -0,0 +1,33 @@ +namespace MedusaPluginChargeMachine.Services +{ + public class SessionService : ISessionService + { + private readonly List<(string sessionId, string refId)> sessions = []; + public void AddSession(string sessionId, string refId) + { + sessions.Add((sessionId, refId)); + } + + public string AddSession(string cardid) + { + var sessionId = GenerateSessionId(); + sessions.Add((sessionId, cardid)); + return sessionId; + } + + public (string sessionId, string refId) GetSession(string sessionId) + { + return sessions.Find(s => s.sessionId == sessionId); + } + + public void RemoveSession(string sessionId) + { + sessions.Remove(sessions.Find(s => s.sessionId == sessionId)); + } + + private string GenerateSessionId() + { + return Guid.NewGuid().ToString().Replace("-", "").ToUpper(); + } + } +}