Skip to main content

Code Snippets 1.x

Certificate Management​

Handling the certificate is a bit different depending on what type of programming language you are using. Here are some code examples that can help you to get started quicker.

In most platforms we either “override” the common name with a generic one, or disable hostname verification, because the cert is self-signed and the device runs on the LAN we cannot match the hostname with the IP-address of the terminal.

Certificate Management
using System;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography.X509Certificates;
public void SendHTTPRequest(HttpClient client){
client.DefaultRequestHeaders.Add("Integration-Key", "Your integration key");
//Starting the POST request
var syncRequest = new HttpRequestMessage("https://192.168.1.105/api/v1/Payments")
{
Content = new StringContent(JsonConvert.SerializeObject(
new
{
amounts = new
{
currencySymbol = "SEK",
@base = 100
},
sendReceipt = true,
},
Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}
), Encoding.UTF8, "application/json")
};
var response = client.Send(syncRequest);
var statuscode1 = response.StatusCode;
}
public HttpClientHandler CertHandler()
{
try
{
//generic common name
var commonName = "wl-samport-terminal-server-v1";
//path to cert
var path = "Certs\\ECR-REST.crt";
X509Certificate2 cert = new X509Certificate2(path);
var handler = new HttpClientHandler();
//specify our own ServerCertificateCustomValidationCallback in order to
//supply our custom root certificate and to validate the commonname
handler.ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, sslErrors) =>
{
if (certificate != null && chain != null)
{
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.CustomTrustStore.Add(cert);
// We can also return choose to simply "return true;" here, We dont have
// to confirm the commonName if we dont want to(doesn't decrease the amount of encryption)
return chain.Build(certificate) && certificate.Subject.Contains("CN=" + commonName);
}
return false;
};
return handler;
}
catch (Exception e)
{
Console.WriteLine("Exception: "+ e.Message);
}
return new HttpClientHandler();
}

UDP Broadcast​

During testing on a Windows machine our Windows firewall was blocking the messages, you need to whitelist the application for it to work. Also, when we did not close our debug process properly the connection stuck in Windows so when we tried re-starting it, we got an error saying that the port was busy. We solved this by running netstat -ano|findstr 8000 to find the PID of the process and then tskill \<PID> to manually terminate it.

Broadcast example​

using System.Net;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;
namespace Receiver
{
class Program
{
static void Main(string[] args)
{
int broadcastPort = 8000;
Console.WriteLine("UDP Broadcast Receiver (" + broadcastPort + ")");
UdpClient receiver = new UdpClient(broadcastPort);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
byte[] bytes = receiver.Receive(ref sender);
dynamic parsedJson = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes));
Console.WriteLine("{0} - Message received from " + sender.Address.ToString() + ":" + sender.Port + ":", DateTime.Now.ToString("HH:mm:ss"));
Console.WriteLine(JsonConvert.SerializeObject(parsedJson, Formatting.Indented));
}
}
}
}

Broadcast message​

The message from the terminal will look like this and will have all the information needed for you to configure your application.

"WORLDLINE_TERMINAL": {
"v0": {
"terminalId": "182837313011101003287575",
"identity": "47084495",
"ipAddress": "10.232.130.102",
"port": 443,
"protocolType": "EcrRestApi",
"protocolVersion": "1.0.0",
"commonName": "wl-samport-terminal-server-v1"
},
}