Billing: Add BillingData and JSON schema validation

This commit is contained in:
2024-01-04 18:09:26 +01:00
parent 37bf8d0855
commit eb46955b3b
5 changed files with 185 additions and 2 deletions

View File

@ -0,0 +1,44 @@
using Newtonsoft.Json;
using NJsonSchema;
using System;
using System.Reflection;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
namespace Elwig.Helpers.Billing {
public class BillingData {
public static JsonSchema? Schema { get; private set; }
public static async Task Init() {
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Elwig.Schemas.payment_variant.json");
Schema = await JsonSchema.FromJsonAsync(stream ?? throw new ArgumentException("JSON schema not found"));
}
private JsonObject Data;
public BillingData(JsonObject data) {
Data = data;
}
public static JsonObject ParseJson(string json) {
if (Schema == null) throw new InvalidOperationException("Schema has to be initialized first");
try {
var errors = Schema.Validate(json);
if (errors.Count != 0) throw new ArgumentException("Invalid JSON data");
return JsonNode.Parse(json)?.AsObject() ?? throw new ArgumentException("Invalid JSON data");
} catch (JsonReaderException) {
throw new ArgumentException("Invalid JSON data");
}
}
public static BillingData FromJson(string json) {
return new(ParseJson(json));
}
public decimal CalculatePrice(string sortId, string? attrid, string qualId, bool gebunden, double oe, double kmw, bool minQuw) {
return 0m;
}
}
}