Skip to content

Commit

Permalink
Explicitly configure ContractResolver for SaleToPOIMessage serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-olesiyuk-apaleo committed Dec 16, 2024
1 parent af0f3b7 commit bacdfe3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
88 changes: 85 additions & 3 deletions Adyen.Test/SerializerTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using Adyen.ApiSerialization;
using Adyen.Model.Checkout;
using Adyen.Model.TerminalApi;
using Adyen.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using PaymentResponse = Adyen.Model.TerminalApi.PaymentResponse;

namespace Adyen.Test
Expand Down Expand Up @@ -87,9 +87,91 @@ public void CheckoutSessionResponseCheckForIdTest()
Assert.IsTrue(deserializedResponse.ToJson().Contains("\"id\": \"CS0068299CB8DA273A\","));
}

[TestMethod]
public void EnsureSaleToPoiMessageSerializationDoesNotDependOnJsonConvertDefaultSettings()
{
var saleToPoiMessage = MockPosApiRequest.CreatePosPaymentRequest();
var serializedWithEmptyDefaultSettings = GetSerializedSaleToPoiMessage(saleToPoiMessage);

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};

try
{
var serializedWithUpdatedDefaultSettings = GetSerializedSaleToPoiMessage(saleToPoiMessage);

Assert.AreEqual(serializedWithEmptyDefaultSettings, serializedWithUpdatedDefaultSettings);
}
finally
{
JsonConvert.DefaultSettings = null;
}
}

[TestMethod]
public void EnsureSaleToPoiMessageSecuredSerializationDoesNotDependOnJsonConvertDefaultSettings()
{
var saleToPoiMessage = MockPosApiRequest.CreatePosPaymentRequest();
var serializedWithEmptyDefaultSettings = GetSerializedSaleToPoiMessageSecured(saleToPoiMessage);

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};

try
{
var serializedWithUpdatedDefaultSettings = GetSerializedSaleToPoiMessageSecured(saleToPoiMessage);

Assert.AreEqual(serializedWithEmptyDefaultSettings, serializedWithUpdatedDefaultSettings);
}
finally
{
JsonConvert.DefaultSettings = null;
}
}

private static string GetSaleToPoiMessage(string online)
{
return "{\"SaleToPOIResponse\": {\"PaymentResponse\": {\"POIData\": {},\"PaymentResult\": {\"AuthenticationMethod\": [\"" + online + "\"],\"PaymentAcquirerData\": {\"AcquirerPOIID\": \"MX925-260390740\",\"MerchantID\": \"PME_POS\"},\"PaymentType\": \"Normal\"},\"Response\": {\"Result\": \"Success\"}},\"MessageHeader\": {\"ProtocolVersion\": \"3.0\",\"SaleID\": \"Appie\",\"MessageClass\": \"Service\",\"MessageCategory\": \"Payment\",\"ServiceID\": \"20095135\",\"POIID\": \"MX925-260390740\",\"MessageType\": \"Response\"}}}";
}

private static string GetSerializedSaleToPoiMessage(SaleToPOIMessage saleToPoiMessage)
{
var saleToPoiMessageSerializer = new SaleToPoiMessageSerializer();
return saleToPoiMessageSerializer.Serialize(saleToPoiMessage);
}

private static string GetSerializedSaleToPoiMessageSecured(SaleToPOIMessage saleToPoiMessage)
{
var saleToPoiMessageSerializer = new SaleToPoiMessageSerializer();
var serializedSaleToPoiMessage = saleToPoiMessageSerializer.Serialize(saleToPoiMessage);

var encryptionCredentialDetails = new EncryptionCredentialDetails
{
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
var messageSecuredEncryptor = new SaleToPoiMessageSecuredEncryptor();
var saleToPoiMessageSecured = messageSecuredEncryptor.Encrypt(
serializedSaleToPoiMessage,
saleToPoiMessage.MessageHeader,
encryptionCredentialDetails);

// Clear SecurityTrailer.Nonce and NexoBlob as they are randomly generated every run
saleToPoiMessageSecured.NexoBlob = null;
saleToPoiMessageSecured.SecurityTrailer.Nonce = null;

return saleToPoiMessageSerializer.Serialize(saleToPoiMessageSecured);
}
}
}
30 changes: 22 additions & 8 deletions Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Adyen.Model.TerminalApi;
using System.Collections.Generic;
using Adyen.Model.TerminalApi;
using Adyen.Security;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace Adyen.ApiSerialization.Converter
{
Expand All @@ -11,19 +13,31 @@ internal class JsonConvertSerializerWrapper

internal static string Serialize(SaleToPOIMessage saleToPoiMessage)
{
var serialize= JsonConvert.SerializeObject(saleToPoiMessage,
new SaleToPoiMessageConverter(),
new StringEnumConverter(),
new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat });
var serialize = JsonConvert.SerializeObject(saleToPoiMessage,
GetSerializerSettings(new SaleToPoiMessageConverter()));
return serialize;
}

internal static string Serialize(SaleToPoiMessageSecured saleToPoiMessageSecured)
{
return JsonConvert.SerializeObject(saleToPoiMessageSecured,
new SaleToPoiMessageSecuredConverter(),
new StringEnumConverter(),
new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat });
GetSerializerSettings(new SaleToPoiMessageSecuredConverter()));
}

private static JsonSerializerSettings GetSerializerSettings(JsonConverter messageConverter)
{
return new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
messageConverter,
new StringEnumConverter(),
new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat }
},
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
ContractResolver = new DefaultContractResolver()
};
}
}
}
2 changes: 0 additions & 2 deletions Adyen/ApiSerialization/Converter/SaleToPoiMessageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ internal class SaleToPoiMessageConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
writer.WriteStartObject();
writer.WritePropertyName(value.GetType().Name);
writer.WriteStartObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ internal class SaleToPoiMessageSecuredConverter : JsonConverter

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.NullValueHandling = NullValueHandling.Ignore;

writer.WriteStartObject();
writer.WritePropertyName(GetProperTypeNameForSerialization(value.GetType()));

Expand Down

0 comments on commit bacdfe3

Please sign in to comment.