Skip to content

Commit

Permalink
Merge pull request #1099 from Adyen/DR-1093-Added-key-version-functio…
Browse files Browse the repository at this point in the history
…n-renames

SaleToPOIMessageConverter fix: Added key-version and function renames for consistency
  • Loading branch information
Kwok-he-Chu authored Jan 3, 2025
2 parents 901250b + 406a1ad commit b46c1de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
31 changes: 15 additions & 16 deletions Adyen.Test/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -111,10 +111,9 @@ public void SaleToPoiMessageWithUpdatedJsonConvertDefaultSettingsSerializationTe
}
};

string serialized = SerializeSaleToPoiMessage(saleToPoiMessage);
try
{
var serialized = SerializeSaleToPoiMessage(saleToPoiMessage);

Assert.AreEqual(serialized, ExpectedSaleToPoiMessageJson);
}
finally
Expand All @@ -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
{
Expand All @@ -146,7 +145,7 @@ public void SaleToPoiMessageSecuredWithUpdatedJsonConvertDefaultSettingsSerializ

try
{
var serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage);
string serialized = SerializeSaleToPoiMessageSecured(saleToPoiMessage);

Assert.AreEqual(serialized, ExpectedSaleToPoiMessageSecuredJson);
}
Expand All @@ -167,7 +166,6 @@ private static string SerializeSaleToPoiMessage(SaleToPOIMessage saleToPoiMessag
return saleToPoiMessageSerializer.Serialize(saleToPoiMessage);
}


private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPoiMessage)
{
var saleToPoiMessageSerializer = new SaleToPoiMessageSerializer();
Expand All @@ -176,6 +174,7 @@ private static string SerializeSaleToPoiMessageSecured(SaleToPOIMessage saleToPo
var encryptionCredentialDetails = new EncryptionCredentialDetails
{
AdyenCryptoVersion = 1,
KeyVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
Expand All @@ -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
{
Expand Down Expand Up @@ -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=""}}}";
}
}
26 changes: 21 additions & 5 deletions Adyen/ApiSerialization/Converter/JsonConvertSerializerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,42 @@ 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<JsonConverter>
{
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<JsonConverter>
{
messageConverter,
new SaleToPoiMessageSecuredConverter(),
new StringEnumConverter(),
new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat }
},
Expand Down

0 comments on commit b46c1de

Please sign in to comment.