- Validate the terminal certificate, to confirm your POS app is communicating directly with an Adyen-supplied payment terminal.
- Encrypt communications. This prevents intruders from reading the messages transmitted between the POS app and the terminal.
Encrypt communications
The available libraries are:- .NET - adyen-dotnet-api-library
- Terminal API for iOS - adyen-terminal-api-ios
- Java - adyen-java-api-library
- Node - adyen-node-api-library
- Serialize the request object to JSON and then encrypt and sign the request.
- Send the request and receive the response.
- Decrypt and deserialize the response and pass the content to the response object.
- .NET
- iOS
- Java
- Node
Preparation
Make sure that you have:- Installed our .NET library in your project.
- Installed Adyen’s root certificate.
- Set up a shared key.
Encrypt communications using the .NET library
-
In your C# project, import the required types.
using Adyen; using Adyen.Security; -
Create an
EncryptionCredentialDetailsobject specifying the identifier, passphrase, and version of your shared key. (For instructions, see Set up a shared key.)var encryptionCredentialDetails = new EncryptionCredentialDetails { KeyVersion = 1, AdyenCryptoVersion = 1, KeyIdentifier = "CryptoKeyIdentifier12345", Password = "p@ssw0rd123456" }; -
Create a
Configobject specifying the IP address of the terminal (for example,https://198.51.100.1:8443/nexo) and the environment: Test or Live.var config = new Config { Environment = Model.Environment.Test, LocalTerminalApiEndpoint = @"https://_terminal_:8443/nexo/" // _terminal_ example: `https://198.51.100.1:8443/nexo` (see the Wi-Fi settings of your terminal) }; -
Initialize a client using the config from the previous step.
var client = new Client(config); -
Create the
TerminalApiLocalServiceusing the client from the previous step.TerminalApiLocalService terminalApiLocalService = new TerminalApiLocalService(client); -
Make an asynchronous or synchronous call to send a Terminal API request to the payment terminal using the
TerminalApiLocalService.// Asynchronous call (preferred) var saleToPOIResponse = await terminalApiLocalService.RequestEncryptedAsync(paymentRequest, encryptionCredentialDetails, new CancellationToken()); // Pass cancellation token or create a new one. // Synchronous (blocking) call //var saleToPOIResponse = terminalApiLocalService.RequestEncrypted(paymentRequest, encryptionCredentialDetails);
The Terminal API for iOS library supports deriving the encryption key and encrypting and decrypting communications.
Preparation
Make sure that you have:- Installed our iOS library (TerminalAPIKit) in your project.
- Installed Adyen’s root certificate.
- Set up a shared key.
Encrypt communications
-
Derive the key used for encrypting and decrypting local communications between the terminal and your POS app. Make sure to pass the
identifier,passphrase, andversionof your shared key in string form exactly as they appear in the Customer Area. (For instructions, see Set up a shared key.)let encryptionKey = try EncryptionKey( identifier: "KEY_IDENTIFIER", passphrase: "KEY_PASSPHRASE", version: KEY_VERSION ) -
Create your request. For example, create a
Message<PaymentRequest>. -
Encrypt your request.
let encryptionKey: EncryptionKey = // the key you derived earlier let request: Message<PaymentRequest> = // the payment request you created let encryptedMessage: Data = try request.encrypt(using: encryptionKey) -
Send the
encryptedMessageto the terminal. -
When you receive the response from the terminal, decrypt the response.
let key: EncryptionKey = // the key you derived earlier let response: Data = // the response you receive from the terminal let encryptedMessage: EncryptedMessage = try Coder.decode(EncryptedMessage.self, from: response) let decryptedMessage: Message<PaymentResponse> = try decrypt(PaymentResponse.self, using: key)
Preparation
Make sure that you have:- Installed our Java library in your project.
- Installed Adyen’s root certificate.
- Set up a shared key.
Encrypt communications using the Java library
-
Import the required classes.
import com.adyen.Client; import com.adyen.Config; import com.adyen.enums.Environment; import com.adyen.httpclient.TerminalLocalAPIHostnameVerifier; import com.adyen.service.TerminalLocalAPI; import com.adyen.model.terminal.security.*; import com.adyen.model.terminal.*; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import java.security.KeyStore; import java.security.SecureRandom; -
Create a keystore for the terminal certificate.
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, null); keyStore.setCertificateEntry("adyenRootCertificate", adyenRootCertificate); -
Create a
TrustManagerFactorythat trusts the Certificate Authorities in the keystore.TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); -
Create an
SSLContextwith the desired protocol that uses our TrustManagers.SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); -
Configure a client for the
TerminalLocalAPIservice, specifying the IP address of the terminal (for example,https://198.51.100.1).Config config = new Config(); config.setEnvironment(environment); config.setTerminalApiLocalEndpoint("https://" + terminalIpAddress); config.setSSLContext(sslContext); config.setHostnameVerifier(new TerminalLocalAPIHostnameVerifier(environment)); Client client = new Client(config); -
Create a
SecurityKeyobject specifying the identifier, passphrase, and version of your shared key. (For instructions, see Set up a shared key.) This object is used to encrypt the payload.SecurityKey securityKey = new SecurityKey(); securityKey.setKeyVersion(1); securityKey.setAdyenCryptoVersion(1); securityKey.setKeyIdentifier("keyIdentifier"); securityKey.setPassphrase("passphrase"); -
Send a Terminal API request to the payment terminal using the
TerminalLocalAPIservice.TerminalLocalAPI terminalLocalAPI = new TerminalLocalAPI(client, securityKey); TerminalAPIResponse terminalAPIResponse = terminalLocalAPI.request(terminalAPIRequest);
Preparation
Make sure that you have:- Installed our Node library in your project.
- Installed Adyen’s root certificate.
- Set up a shared key.
Encrypt communications using the Node library
-
In your Node project, require the parts of the module you want to use.
const {Client, TerminalLocalAPI} from "@adyen/api-library"; -
Create a
Configobject with your path to the Adyen root certificate and the IP address of the terminal (for example,https://198.51.100.1:8443/nexo), and install and save the certificate in your project folder ascert.cer.const config: Config = new Config(); config.certificatePath = "./cert.cer"; config.terminalApiLocalEndpoint = "The IP of your terminal (for example https://198.51.100.1:8443/nexo)"; config.apiKey = "YOUR_ADYEN_API_KEY"; -
Create a
SecurityKeyobject specifying the identifier, passphrase, and version of your shared key. (For instructions, see Set up a shared key.)const securityKey: SecurityKey = { AdyenCryptoVersion: 1, KeyIdentifier: "keyIdentifier", KeyVersion: 1, Passphrase: "passphrase", }; -
Initialize the client and the API objects using the config, and create the
TerminalLocalAPIservice.client = new Client({ config }); const terminalLocalAPI = new TerminalLocalAPI(client); -
Create the request object.
const paymentRequest: SaleToPOIRequest = { // The Terminal API `MessageHeader` and `PaymentRequest` } -
Send the Terminal API request to the payment terminal using the
TerminalLocalAPIservice.const terminalApiResponse: terminal.TerminalApiResponse = await terminalLocalAPI.request(paymentRequest, securityKey);
Full code samples
Select a tab in the code sample block below to see the code for encrypting communications. In all cases you need to know the key identifier, passphrase, and version of your shared key.To find the details of your shared key in your Customer Area:- Under In-person payments, go to the Terminal settings for your merchant account or store.
- Select Integrations and under Terminal API go to Encryption key.
- To see the key identifier, passphrase, and version values, select Decrypted.
// Import the required classes
import com.adyen.Client;
import com.adyen.Config;
import com.adyen.enums.Environment;
import com.adyen.httpclient.TerminalLocalAPIHostnameVerifier;
import com.adyen.service.TerminalLocalAPI;
import com.adyen.model.terminal.security.*;
import com.adyen.model.terminal.*;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.security.KeyStore;
import java.security.SecureRandom;
// Create a keystore for the terminal certificate
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("adyenRootCertificate", adyenRootCertificate);
// Create a TrustManagerFactory that trusts the Certificate Authorities in the keystore
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
// Create an SSLContext with the desired protocol that uses our TrustManagers
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
// Configure a client for the TerminalLocalAPI service
// specifying the IP address of the terminal, for example https://198.51.100.1:8443/nexo
Config config = new Config();
config.setEnvironment(environment);
config.setTerminalApiLocalEndpoint("https://" + terminalIpAddress);
config.setSSLContext(sslContext);
config.setHostnameVerifier(new TerminalLocalAPIHostnameVerifier(environment));
Client client = new Client(config);
// Create a SecurityKey object specifying the identifier, passphrase, and version of your shared key
SecurityKey securityKey = new SecurityKey();
securityKey.setKeyVersion(1);
securityKey.setAdyenCryptoVersion(1);
securityKey.setKeyIdentifier("keyIdentifier");
securityKey.setPassphrase("passphrase");
// Send a Terminal API request to the payment terminal using the TerminalLocalAPI service
TerminalLocalAPI terminalLocalAPI = new TerminalLocalAPI(client, securityKey);
TerminalAPIResponse terminalAPIResponse = terminalLocalAPI.request(terminalAPIRequest);
// In your C# project, import the required types
using Adyen;
using Adyen.Security;
// Create an EncryptionCredentialDetails object specifying the identifier, passphrase, and version of your shared key
var encryptionCredentialDetails = new EncryptionCredentialDetails
{
KeyVersion = 1,
AdyenCryptoVersion = 1,
KeyIdentifier = "CryptoKeyIdentifier12345",
Password = "p@ssw0rd123456"
};
// Create a Config object with the IP address of the terminal (for example, https://198.51.100.1:8443/nexo)
// and the environment: Test or Live
var config = new Config
{
Environment = Model.Environment.Test,
LocalTerminalApiEndpoint = @"https://_terminal_:8443/nexo/" // _terminal_ example: `https://198.51.100.1:8443/nexo` (can be found in the WIFI settings of your terminal)
};
// Initialize a client using the config
var client = new Client(config);
// Create the TerminalApiLocalService using the client
TerminalApiLocalService terminalApiLocalService = new TerminalApiLocalService(client);
// Make an asynchronous or synchronous call to send a Terminal API request to the payment terminal
// Asynchronous call (preferred)
var saleToPOIResponse = await terminalApiLocalService.RequestEncryptedAsync(paymentRequest, encryptionCredentialDetails, new CancellationToken()); // Pass cancellation token or create a new one.
// Synchronous (blocking) call
//var saleToPOIResponse = terminalApiLocalService.RequestEncrypted(paymentRequest, encryptionCredentialDetails);
// In your Node project, require the parts of the module you want to use
const {Client, TerminalLocalAPI} from "@adyen/api-library";
// Create a Config object with your path to the Adyen root certificate and the IP address of the terminal
// (for example, `https://198.51.100.1:8443/nexo`), and install and save the certificate in your project folder as `cert.cer`.
const config: Config = new Config();
config.certificatePath = "./cert.cer";
config.terminalApiLocalEndpoint = "The IP of your terminal (eg https://192.168.47.169)";
config.apiKey = "YOUR_API_KEY_HERE";
// Create a SecurityKey object specifying the identifier, passphrase, and version of your shared key
const securityKey: SecurityKey = {
AdyenCryptoVersion: 1,
KeyIdentifier: "keyIdentifier",
KeyVersion: 1,
Passphrase: "passphrase",
};
// Initialize the client and the API objects
client = new Client({ config });
const terminalLocalAPI = new TerminalLocalAPI(client);
// Create the request object
const paymentRequest: SaleToPOIRequest = {
// The Terminal API `MessageHeader` and `PaymentRequest`
}
// Send the Terminal API request to the payment terminal
const terminalApiResponse: terminal.TerminalApiResponse = await terminalLocalAPI.request(paymentRequest, securityKey);
// Derive a key, passing the identifier, passphrase, and version of your shared key in string form
let encryptionKey = try EncryptionKey(
identifier: "KEY_IDENTIFIER",
passphrase: "KEY_PASSPHRASE",
version: KEY_VERSION
)
// Create your Terminal API request, for example a `Message<PaymentRequest>`
// Encrypt the request
let encryptionKey: EncryptionKey = // the key you derived earlier
let request: Message<PaymentRequest> = // the payment request you created
let encryptedMessage: Data = try request.encrypt(using: encryptionKey)
// Send the encryptedMessage to the terminal
// When you receive the response from the terminal, decrypt the response
let key: EncryptionKey = // the key you derived earlier
let response: Data = // the response you receive from the terminal
let encryptedMessage: EncryptedMessage = try Coder.decode(EncryptedMessage.self, from: response)
let decryptedMessage: Message<PaymentResponse> = try decrypt(PaymentResponse.self, using: key)
Troubleshooting
Crypto errors and SSL connection errors indicate a problem with the protection of the local communications.Crypto errors
Example:Exception: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
{
"errors":[
"Nexo Service: crypto error"
],
"ServiceID":"1234567890"
}
- With the .NET library, check the
EncryptionCredentialDetailsobject. - With the Java library, check the
SecurityKeyobject. - With the Node library, check the
SecurityKeyobject.
- Check the key derivation function. This uses the passphrase of the shared key.
- Check the security trailer function. This uses the version and the identifier of the shared key.
SSL connection error
Example:Exception : System.Net.WebException: The SSL connection could not be established