Java is a high-level programming language, object-oriented and with static typing, which relies on the execution software platform of the same name, specifically designed to be as independent as possible from the execution hardware platform by compiling in bytecode first and then interpreting by part of a JVM.
Through the SDK made available by Nexi it is possible to choose between the OkHttp client and HttpClient5.
To install the Java SDK via Maven add the following dependency to the pom.xml file:
Next you need to include the dependency for one of the two supported HTTP clients:
In order to communicate with the gateway it is necessary to create a configuration object that implements the interface io.github.nexipayments.sdknpg.configuration.IConfiguration.
For use in production it is necessary to use the interface io.github.nexipayments.sdknpg.configuration.ConfigurationProduction, alternatively you can proceed with a customized implementation that must respect the established interface.
It is necessary to provide an additional object for the persistent storage of security tokens, used to authenticate the notifications that the interface must manage io.github.nexipayments.sdknpg.securitytokenstorage.ISecurityTokenStorage.
This storage associates one or more tokens with each orderId, these must be persistently persisted to validate incoming events from the gateway.
Once the two objects that adhere to the interfaces just mentioned have been instantiated, you can proceed to use one of the two static methods of the class io.github.nexipayments.sdknpg.PaymentGatewayClient to get the client instance suitable for the chosen HTTP library:
- getInstanceOkHttp3: will use okhttp3
- getInstanceApacheHttpClient5: will use httpclient5
Example of use
Once the object has been obtained through one of the two static methods presented in the previous section, it is possible to proceed with requests to the gateway. Below is an example of payment using the method Hosted Payment Page:
Objects, as well as "set" methods for setting parameters, are referenced by the API POST /orders/hpp. The SDK was developed keeping the parameter names the same as those of the gateway API, to facilitate and improve the user experience.
To validate the notifications coming in from the gateway, it is necessary to expose the endpoint indicated in the "notificationUrl" parameter of the start request POST /orders/hpp, the endpoint must accept a JSON with POST method. This JSON can be validated by the SDK with the parseAndValidateWebhook method which, using the persistent storage object, verifies the consistency of the security token or raises an exception:
If the system used supports the deserialization of JSON with a specified class, the interface can be used io.github.nexipayments.sdknpg.pojo.WebhookNotificationBody and proceed with the validation of the object received: