Skip to content

Commit

Permalink
Fix Terminal api missing encryption (Adyen#933)
Browse files Browse the repository at this point in the history
* Add the unit test

* Add the adyen exception for missing nexoblob

* Add only the exception for the nexoblob
  • Loading branch information
AlexandrosMor authored Nov 8, 2023
1 parent c34754c commit fce0f4a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
46 changes: 46 additions & 0 deletions Adyen.Test/MockPosApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,51 @@ public static string MockNexoJsonRequest()
" \"Currency\" : \"EUR\",\r\n \"RequestedAmount\" : 15.25 \r\n },\r\n \"TransactionConditions\" : {}\r\n },\r\n " +
" \"PaymentData\" : {\"PaymentType\" : \"Normal\"}\r\n }\r\n }\r\n}\r\n";
}
/// <summary>
/// Dummy terminal api json api/terminal api request without security trailer set for test
/// enviroment
/// </summary>
/// <returns></returns>
public static SaleToPOIRequest CreatePosPaymentRequestEmptySecurityTrailer()
{
var saleToPoiRequest = new SaleToPOIRequest
{
MessageHeader = new MessageHeader
{
MessageType = MessageType.Request,
MessageClass = MessageClassType.Service,
MessageCategory = MessageCategoryType.Payment,
SaleID = "POSSystemID12345",
POIID = "MX915-284251016",
ServiceID = DateTime.Now.ToString("ddHHmmss")//this should be unique
},
MessagePayload = new PaymentRequest
{
SaleData = new SaleData
{
SaleTransactionID = new TransactionIdentification
{
TransactionID = "PosAuth",
TimeStamp = DateTime.Now
},
TokenRequestedType = TokenRequestedType.Customer,
},
PaymentTransaction = new PaymentTransaction
{
AmountsReq = new AmountsReq
{
Currency = "EUR",
RequestedAmount = 10100
}
},
PaymentData = new PaymentData
{
PaymentType = PaymentType.Normal
}
},
SecurityTrailer = null
};
return saleToPoiRequest;
}
}
}
22 changes: 21 additions & 1 deletion Adyen.Test/TerminalApiPosRequestTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Adyen.Exceptions;
using Adyen.Security;
using Adyen.Service;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -80,6 +81,25 @@ public void TestTerminalApiCardAcquisitionResponse()
{
Assert.Fail();
}
}
}

[TestMethod]
public void TestTerminalApiRequestEmptySecurityTrailer()
{
try
{
var paymentRequest = MockPosApiRequest.CreatePosPaymentRequestEmptySecurityTrailer();
//create a mock client
var client = CreateMockTestClientPosLocalApiRequest("mocks/terminalapi/pospayment-no-security-trailer.json");
var terminalLocalApi = new TerminalLocalApi(client);
var configEndpoint = terminalLocalApi.Client.Config.LocalTerminalApiEndpoint;
var saleToPoiResponse = terminalLocalApi.TerminalRequest(paymentRequest, _encryptionCredentialDetails);
Assert.AreEqual(configEndpoint, @"https://_terminal_:8443/nexo/");
}
catch (Exception ex)
{
Assert.AreEqual(ex.Message,"NexoBlob is empty in the response");
}
}
}
}
14 changes: 14 additions & 0 deletions Adyen.Test/mocks/terminalapi/pospayment-no-security-trailer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"SaleToPOIResponse": {
"SecurityTrailer": null,
"MessageHeader": {
"ProtocolVersion": "3.0",
"SaleID": "John",
"MessageClass": "Service",
"MessageCategory": "Payment",
"ServiceID": "9739",
"POIID": "MX915-284251016",
"MessageType": "Response"
}
}
}
16 changes: 11 additions & 5 deletions Adyen/ApiSerialization/SaleToPoiMessageSecuredSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ private void CheckForTerminalError(JObject terminalResponseJObject)

private SaleToPoiMessageSecured ParseSaleToPoiMessageSecured(JToken saleToPoiMessageSecuredJToken)
{
var saleToPoiMessageSecured = new SaleToPoiMessageSecured
var saleToPoiMessageSecured = new SaleToPoiMessageSecured();
saleToPoiMessageSecured.MessageHeader = saleToPoiMessageSecuredJToken.SelectToken("MessageHeader").ToObject<MessageHeader>();
saleToPoiMessageSecured.SecurityTrailer = saleToPoiMessageSecuredJToken.SelectToken("SecurityTrailer")
.ToObject<SecurityTrailer>();
try
{
MessageHeader = saleToPoiMessageSecuredJToken.SelectToken("MessageHeader").ToObject<MessageHeader>(),
NexoBlob = saleToPoiMessageSecuredJToken.SelectToken("NexoBlob").ToObject<string>(),
SecurityTrailer = saleToPoiMessageSecuredJToken.SelectToken("SecurityTrailer").ToObject<SecurityTrailer>()
};
saleToPoiMessageSecured.NexoBlob = saleToPoiMessageSecuredJToken.SelectToken("NexoBlob").ToObject<string>();
}
catch (Exception ex)
{
throw new DeserializationException("NexoBlob is empty in the response",ex.InnerException);
}

return saleToPoiMessageSecured;
}
Expand Down

0 comments on commit fce0f4a

Please sign in to comment.