From 64734603294aee74ae32cefbc6498e2e82b5b26c Mon Sep 17 00:00:00 2001 From: kwokhe Date: Fri, 3 Jan 2025 15:37:42 +0100 Subject: [PATCH 1/2] Added key-version and remove MissingMemberHandling.Ignore for SecuredMessageSerializerSettings Minor function renames for consistency --- Adyen.Test/SerializerTest.cs | 31 +++++++++---------- .../Converter/JsonConvertSerializerWrapper.cs | 27 ++++++++++++---- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Adyen.Test/SerializerTest.cs b/Adyen.Test/SerializerTest.cs index 9dac6609..3321ca47 100644 --- a/Adyen.Test/SerializerTest.cs +++ b/Adyen.Test/SerializerTest.cs @@ -93,15 +93,15 @@ public void CheckoutSessionResponseCheckForIdTest() [TestMethod] public void SaleToPoiMessageSerializationTest() { - var saleToPoiMessage = PosPaymentRequest; - var serialized = SerializeSaleToPoiMessage(saleToPoiMessage); + SaleToPOIRequest saleToPoiMessage = CreatePosPaymentRequest(); + string serialized = SerializeSaleToPoiMessage(saleToPoiMessage); Assert.AreEqual(serialized, ExpectedSaleToPoiMessageJson); } [TestMethod] public void SaleToPoiMessageWithUpdatedJsonConvertDefaultSettingsSerializationTest() { - var saleToPoiMessage = PosPaymentRequest; + SaleToPOIRequest saleToPoiMessage = CreatePosPaymentRequest(); JsonConvert.DefaultSettings = () => new JsonSerializerSettings { @@ -111,10 +111,9 @@ public void SaleToPoiMessageWithUpdatedJsonConvertDefaultSettingsSerializationTe } }; + string serialized = SerializeSaleToPoiMessage(saleToPoiMessage); try { - var serialized = SerializeSaleToPoiMessage(saleToPoiMessage); - Assert.AreEqual(serialized, ExpectedSaleToPoiMessageJson); } finally @@ -126,15 +125,15 @@ public void SaleToPoiMessageWithUpdatedJsonConvertDefaultSettingsSerializationTe [TestMethod] public void SaleToPoiMessageSecuredSerializationTest() { - var saleToPoiMessage = PosPaymentRequest; - var serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage); + SaleToPOIRequest saleToPoiMessage = CreatePosPaymentRequest(); + string serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage); Assert.AreEqual(serialized, ExpectedSaleToPoiMessageSecuredJson); } [TestMethod] public void SaleToPoiMessageSecuredWithUpdatedJsonConvertDefaultSettingsSerializationTest() { - var saleToPoiMessage = PosPaymentRequest; + SaleToPOIRequest saleToPoiMessage = CreatePosPaymentRequest(); JsonConvert.DefaultSettings = () => new JsonSerializerSettings { @@ -146,7 +145,7 @@ public void SaleToPoiMessageSecuredWithUpdatedJsonConvertDefaultSettingsSerializ try { - var serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage); + string serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage); Assert.AreEqual(serialized, ExpectedSaleToPoiMessageSecuredJson); } @@ -167,7 +166,6 @@ private static string SerializeSaleToPoiMessage(SaleToPOIMessage saleToPoiMessag return saleToPoiMessageSerializer.Serialize(saleToPoiMessage); } - private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPoiMessage) { var saleToPoiMessageSerializer = new SaleToPoiMessageSerializer(); @@ -176,6 +174,7 @@ private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPo var encryptionCredentialDetails = new EncryptionCredentialDetails { AdyenCryptoVersion = 1, + KeyVersion = 1, KeyIdentifier = "CryptoKeyIdentifier12345", Password = "p@ssw0rd123456" }; @@ -192,8 +191,9 @@ private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPo return saleToPoiMessageSerializer.Serialize(saleToPoiMessageSecured); } - private static SaleToPOIRequest PosPaymentRequest => - new SaleToPOIRequest + private static SaleToPOIRequest CreatePosPaymentRequest() + { + return new SaleToPOIRequest { MessageHeader = new MessageHeader { @@ -229,11 +229,10 @@ private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPo } } }; + } - private static string ExpectedSaleToPoiMessageJson => - "{\"SaleToPOIRequest\":{\"MessageHeader\":{\"MessageClass\":\"Service\",\"MessageCategory\":\"Payment\",\"MessageType\":\"Request\",\"ServiceID\":\"12345678\",\"SaleID\":\"POSSystemID12345\",\"POIID\":\"MX915-284251016\",\"ProtocolVersion\":\"3.0\"},\"PaymentRequest\":{\"SaleData\":{\"SaleTransactionID\":{\"TransactionID\":\"PosAuth\",\"TimeStamp\":\"2025-01-01T00:00:00\"},\"SaleToAcquirerData\":\"eyJhcHBsaWNhdGlvbkluZm8iOnsiYWR5ZW5MaWJyYXJ5Ijp7Im5hbWUiOiJhZHllbi1kb3RuZXQtYXBpLWxpYnJhcnkiLCJ2ZXJzaW9uIjoiMjYuMC4wIn19fQ==\",\"TokenRequestedType\":\"Customer\"},\"PaymentTransaction\":{\"AmountsReq\":{\"Currency\":\"EUR\",\"RequestedAmount\":10100.0}},\"PaymentData\":{\"PaymentType\":\"Normal\"}}}}"; + private static string ExpectedSaleToPoiMessageJson => "{\"SaleToPOIRequest\":{\"MessageHeader\":{\"MessageClass\":\"Service\",\"MessageCategory\":\"Payment\",\"MessageType\":\"Request\",\"ServiceID\":\"12345678\",\"SaleID\":\"POSSystemID12345\",\"POIID\":\"MX915-284251016\",\"ProtocolVersion\":\"3.0\"},\"PaymentRequest\":{\"SaleData\":{\"SaleTransactionID\":{\"TransactionID\":\"PosAuth\",\"TimeStamp\":\"2025-01-01T00:00:00\"},\"SaleToAcquirerData\":\"eyJhcHBsaWNhdGlvbkluZm8iOnsiYWR5ZW5MaWJyYXJ5Ijp7Im5hbWUiOiJhZHllbi1kb3RuZXQtYXBpLWxpYnJhcnkiLCJ2ZXJzaW9uIjoiMjYuMS4wIn19fQ==\",\"TokenRequestedType\":\"Customer\"},\"PaymentTransaction\":{\"AmountsReq\":{\"Currency\":\"EUR\",\"RequestedAmount\":10100.0}},\"PaymentData\":{\"PaymentType\":\"Normal\"}}}}"; - private static string ExpectedSaleToPoiMessageSecuredJson => - "{\"SaleToPOIRequest\":{\"MessageHeader\":{\"MessageClass\":\"Service\",\"MessageCategory\":\"Payment\",\"MessageType\":\"Request\",\"ServiceID\":\"12345678\",\"SaleID\":\"POSSystemID12345\",\"POIID\":\"MX915-284251016\",\"ProtocolVersion\":\"3.0\"},\"NexoBlob\":null,\"SecurityTrailer\":{\"AdyenCryptoVersion\":1,\"KeyIdentifier\":\"CryptoKeyIdentifier12345\",\"KeyVersion\":0,\"Hmac\":\"0lPogF5Mg97Nty9ZUuAnb3v8pvZTZvwouxdMp0HV+yQ=\"}}}"; + private static string ExpectedSaleToPoiMessageSecuredJson => @"{""SaleToPOIRequest"":{""MessageHeader"":{""MessageClass"":""Service"",""MessageCategory"":""Payment"",""MessageType"":""Request"",""ServiceID"":""12345678"",""SaleID"":""POSSystemID12345"",""POIID"":""MX915-284251016"",""ProtocolVersion"":""3.0""},""NexoBlob"":null,""SecurityTrailer"":{""AdyenCryptoVersion"":1,""KeyIdentifier"":""CryptoKeyIdentifier12345"",""KeyVersion"":1,""Hmac"":""mBUD3BeMrloo5aPlwUCFIa87YW8hY9/i3AcrOa2qbhk=""}}}"; } } diff --git a/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs b/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs index d03d02db..7ca06b4c 100644 --- a/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs +++ b/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs @@ -11,31 +11,46 @@ internal class JsonConvertSerializerWrapper { private const string DateTimeFormat = "yyyy-MM-ddTHH\\:mm\\:ss"; - private static readonly JsonSerializerSettings SaleToPoiMessageSerializerSettings = CreateSerializerSettings(new SaleToPoiMessageConverter()); - private static readonly JsonSerializerSettings SaleToPoiMessageSecuredSerializerSettings = CreateSerializerSettings(new SaleToPoiMessageSecuredConverter()); + private static readonly JsonSerializerSettings SaleToPoiMessageSerializerSettings = CreateMessageSerializerSettings(); + private static readonly JsonSerializerSettings SaleToPoiMessageSecuredSerializerSettings = CreateSecuredMessageSerializerSettings(); internal static string Serialize(SaleToPOIMessage saleToPoiMessage) { return JsonConvert.SerializeObject(saleToPoiMessage, SaleToPoiMessageSerializerSettings); } - + + private static JsonSerializerSettings CreateMessageSerializerSettings() + { + return new JsonSerializerSettings + { + Converters = new List + { + new SaleToPoiMessageConverter(), + new StringEnumConverter(), + new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat } + }, + NullValueHandling = NullValueHandling.Ignore, + MissingMemberHandling = MissingMemberHandling.Ignore, + ContractResolver = new DefaultContractResolver() + }; + } + internal static string Serialize(SaleToPoiMessageSecured saleToPoiMessageSecured) { return JsonConvert.SerializeObject(saleToPoiMessageSecured, SaleToPoiMessageSecuredSerializerSettings); } - private static JsonSerializerSettings CreateSerializerSettings(JsonConverter messageConverter) + private static JsonSerializerSettings CreateSecuredMessageSerializerSettings() { return new JsonSerializerSettings { Converters = new List { - messageConverter, + new SaleToPoiMessageSecuredConverter(), new StringEnumConverter(), new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat } }, NullValueHandling = NullValueHandling.Ignore, - MissingMemberHandling = MissingMemberHandling.Ignore, ContractResolver = new DefaultContractResolver() }; } From 406a1ad05db97047e2d093ab971690cbe6f2a525 Mon Sep 17 00:00:00 2001 From: kwokhe Date: Fri, 3 Jan 2025 15:54:09 +0100 Subject: [PATCH 2/2] Add MissingMemberHandling.Ignore --- Adyen.Test/SerializerTest.cs | 2 +- .../ApiSerialization/Converter/JsonConvertSerializerWrapper.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Adyen.Test/SerializerTest.cs b/Adyen.Test/SerializerTest.cs index 3321ca47..5365a6a2 100644 --- a/Adyen.Test/SerializerTest.cs +++ b/Adyen.Test/SerializerTest.cs @@ -231,7 +231,7 @@ private static SaleToPOIRequest CreatePosPaymentRequest() }; } - private static string ExpectedSaleToPoiMessageJson => "{\"SaleToPOIRequest\":{\"MessageHeader\":{\"MessageClass\":\"Service\",\"MessageCategory\":\"Payment\",\"MessageType\":\"Request\",\"ServiceID\":\"12345678\",\"SaleID\":\"POSSystemID12345\",\"POIID\":\"MX915-284251016\",\"ProtocolVersion\":\"3.0\"},\"PaymentRequest\":{\"SaleData\":{\"SaleTransactionID\":{\"TransactionID\":\"PosAuth\",\"TimeStamp\":\"2025-01-01T00:00:00\"},\"SaleToAcquirerData\":\"eyJhcHBsaWNhdGlvbkluZm8iOnsiYWR5ZW5MaWJyYXJ5Ijp7Im5hbWUiOiJhZHllbi1kb3RuZXQtYXBpLWxpYnJhcnkiLCJ2ZXJzaW9uIjoiMjYuMS4wIn19fQ==\",\"TokenRequestedType\":\"Customer\"},\"PaymentTransaction\":{\"AmountsReq\":{\"Currency\":\"EUR\",\"RequestedAmount\":10100.0}},\"PaymentData\":{\"PaymentType\":\"Normal\"}}}}"; + private static string ExpectedSaleToPoiMessageJson => @"{""SaleToPOIRequest"":{""MessageHeader"":{""MessageClass"":""Service"",""MessageCategory"":""Payment"",""MessageType"":""Request"",""ServiceID"":""12345678"",""SaleID"":""POSSystemID12345"",""POIID"":""MX915-284251016"",""ProtocolVersion"":""3.0""},""PaymentRequest"":{""SaleData"":{""SaleTransactionID"":{""TransactionID"":""PosAuth"",""TimeStamp"":""2025-01-01T00:00:00""},""SaleToAcquirerData"":""eyJhcHBsaWNhdGlvbkluZm8iOnsiYWR5ZW5MaWJyYXJ5Ijp7Im5hbWUiOiJhZHllbi1kb3RuZXQtYXBpLWxpYnJhcnkiLCJ2ZXJzaW9uIjoiMjYuMS4wIn19fQ=="",""TokenRequestedType"":""Customer""},""PaymentTransaction"":{""AmountsReq"":{""Currency"":""EUR"",""RequestedAmount"":10100.0}},""PaymentData"":{""PaymentType"":""Normal""}}}}"; private static string ExpectedSaleToPoiMessageSecuredJson => @"{""SaleToPOIRequest"":{""MessageHeader"":{""MessageClass"":""Service"",""MessageCategory"":""Payment"",""MessageType"":""Request"",""ServiceID"":""12345678"",""SaleID"":""POSSystemID12345"",""POIID"":""MX915-284251016"",""ProtocolVersion"":""3.0""},""NexoBlob"":null,""SecurityTrailer"":{""AdyenCryptoVersion"":1,""KeyIdentifier"":""CryptoKeyIdentifier12345"",""KeyVersion"":1,""Hmac"":""mBUD3BeMrloo5aPlwUCFIa87YW8hY9/i3AcrOa2qbhk=""}}}"; } diff --git a/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs b/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs index 7ca06b4c..7eb43716 100644 --- a/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs +++ b/Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs @@ -51,6 +51,7 @@ private static JsonSerializerSettings CreateSecuredMessageSerializerSettings() new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat } }, NullValueHandling = NullValueHandling.Ignore, + MissingMemberHandling = MissingMemberHandling.Ignore, ContractResolver = new DefaultContractResolver() }; }