Enbank - Java example docs
Read on how the payment flow works first and the OpenAPI docs. Then look at the example code bellow.
Enbank Integration Documentation
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
public class Main {
public static void main(String[] args) throws IOException {
String rootPath = "https://enbank-server-local.fly.dev";
URL url = new URL(rootPath + "/transactions/new");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
// Get the request body
// - "amount" is the transaction amount. Example: "amount": 15
// - "token" is Enbank generated token. Example: "token":"<your_enbank_token>"
// - "description" the transaction description. Example: "description": "My first Enbank transaction"
String jsonInputString = generateJson(1.00, "###PRIVATE_TOKEN###", "My Enbank transaction");
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
} catch (Exception e) {
System.out.println(e.getMessage());
}
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
String transactionId = response.toString();
System.out.println(transactionId);
// You can use the transactionId in order to create checkout link.
// rootPath/checkout/transactionId
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Create a request body
private static String generateJson(Double amount, String token, String description) {
return String.format("{ \"amount\": %.2f, \"token\": \"%s\", \"description\": \"%s\", \"currency\": \"BGN\" }", amount, token, description);
}
}
This is the bare minimum to make a new transaction. As a response you will receive a JSON object with a trasactionId
You can use the transactionId in order to create checkout link.
”$rootPath/checkout/$transactionId”
Or put the transaction id in our web component:
<script src="http://pay.enbank.me/enbank-pay.min.js"></script>
<enbank-pay transactionId="$transactionId"></enbank-pay>