Skip to main content

 

Eptura Knowledge Center

Decode notification messages

Code Snippets

Step 1

Store the following information in a string object:

  1. The BODY received in the webhook HTTP request. This is a JSON content part of which is encoded/encrypted Notification Message.
  2. ClientId used to subscribe to the Notification Pusher.
string webhookMessage = "";
string clientId = "";

Step 2

De-serialize the stored “webhookMessage” into an object using any JSON de-serializer .Newtonsoft is used in the example in our case.

Nuget Package: Newtonsoft.Json

CustomNotifications data = JsonConvert.
DeserializeObject(webhookMessage);

The above mentioned CustomNotification is a class used to de-serialize the received notification. The class structure matches the class sent by Condeco.

The class structure is given below:

public class CustomNotifications
{
        /// 
        /// Gets or sets the unique ID of this WebHook message.
        /// 
        public string Id { get; set; }
        /// 
        /// Gets or sets the number of times this message has been 
attempted to be delivered.
        /// 
        public int Attempt { get; set; }
        ///         /// Gets the set of additional properties included in this 
WebHook message.
        /// 
        public IDictionary Properties { get; set; }
        /// 
        /// Gets the collection of notifications included in this 
WebHook message. Each notification
        /// is represented as a System.Collections.Generic.Dictionary`2 
where TKey is a property
        /// name and TValue is the value of that property. For each 
notification, the Action
        /// can be found using the key Action.
        /// 
        public ICollection Notifications { get; set; } = 
new Collection();
}
public class Notification
{
        /// 
        /// Refers to the Event Type Subscribed to
        /// 
        public string Action { get; set; }
        /// 
        /// Actual encrypted notification message of interest
        /// 
        public string DynamicParamA { get; set; }
}
Example -A Sample payload(JSON) as below
        {
           "Id": "d38861430b2642c59f060a64b7df4402",
       "Attempt": 1,
       "Properties": {
       "StaticParamA": "DateTime"
    },
      "Notifications": [
        {
          "Action": "VISITOR",
          "DynamicParamA": 
"mlFcsrt0X8AjK3QAb6sRHbk7kCGOkK3uTGjIRlNXa59o8K1ttn4e0oluPIuQ2chk"
        }
      ]
   }

Step 3

Decode the de-serialized notification object using the snippet below.

The following Helper Methods use system libraries instead of third party Nuget Packages.

  1. Notifications from the JSON code are shown as an array. This will be listed as a single object unless it is representing a bulk operation.
  2. Each object has a field to describe the type of notification Action,
  3. The object also has a field that has the details of the notification as an encrypted text. This is labelled as: DynamicParamA
  4. Decrypting requires a call to the method DecryptText() as per example below. The structure of the JSON text decrypted in the DynamicParamA DecryptText() field will vary for different types of notifications.
/// 
/// This helper method is used to Decode the Condeco Notification Message
/// 
/// This parameter is the Deserialized Object of the original JSON Message Received from Condeco
/// Unique clientId used to subscribe to the Condeco Notification Pusher
/// 
private static string Decode(CustomNotifications data, string clientId)
{
        StringBuilder finalDecodedNotificationMessage = new 
StringBuilder();
        foreach (var notification in data.Notifications)
        {
                /* Decrypt the Notification JSON Message Received from Condeco */
                string decryptedNotification = DecryptText(notification.
DynamicParamA, clientId.ToUpper().Substring(0, 16), clientId.ToUpper());
                /* Serialize the Decrypted List of Notifications into 
list of string */
                List listOfNotificationsDetails = JsonConvert.
DeserializeObject>(decryptedNotification);
                /* Loop over the list of Deserialized Notifications to 
decode the message to Original JSON */
                foreach (string notificationDetail in 
listOfNotificationsDetails)
                {
                        byte[] notificationByte = Convert.
FromBase64String(notificationDetail);
                        finalDecodedNotificationMessage.Append(System.
Text.Encoding.UTF8.GetString(notificationByte, 0, notificationByte.
Length));
                }        }
        return finalDecodedNotificationMessage.ToString();
}
/// 
/// This helper method is used to Decrypt the Condeco Notification Message
/// 
/// Encrypted Notification
/// 
/// 
/// 
public static string DecryptText(string encryptedText, string salt, 
string AES)
{
                        var aesProvider = new System.Security.
Cryptography.AesCryptoServiceProvider();
    aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(AES);
    aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(salt);
    string plainText = string.Empty;
    var convertedText = Convert.FromBase64String(encryptedText);
    using (var memoryStream = new System.IO.MemoryStream(convertedText))
    {
                using (var streamReader = new System.IO.StreamReader
(new System.Security.Cryptography.CryptoStream(memoryStream, 
aesProvider.CreateDecryptor(), System.Security.Cryptography.
CryptoStreamMode.Read)))
        {
            plainText = streamReader.ReadToEnd();
        }
    }
    return plainText;
}