import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.*;
import java.util.Base64;
public class RSASignature {
public static void main(String[] args) throws Exception {
// Request parameters
String body = "{\"amount\":\"100\",\"method\":\"NATIVE\",\"phone\":\"82445415208\",\"name\":\"LuciaLesch\",\"merchantRef\":\"T1699447296\",\"email\":\"quinten31@yahoo.com\"}";
String uri = "/v1/api/payment/initiate";
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String merchantId = "FINIX_MERCHANT_ID";
// Load private key
String privateKeyPem = "-----BEGIN PRIVATE KEY-----\n"+ YOUR_PRIVATE_KEY_CONTENT_HERE +"\n-----END PRIVATE KEY-----";
PrivateKey privateKey = getPrivateKeyFromPem(privateKeyPem);
// Generate signature
String bodyHash = sha512Hex(body);
String stringToSign = uri + bodyHash + timestamp;
String signatureBase64 = sign(stringToSign, privateKey);
// Print signature (for debugging)
System.out.println("Signature: " + signatureBase64);
// Send HTTP POST request
String apiUrl = "https://secure-staging.finixpayment.com" + uri;
String response = sendHttpPost(apiUrl, body, merchantId, signatureBase64, timestamp);
System.out.println("Response: " + response);
}
// Compute SHA-512 hex string
private static String sha512Hex(String data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] digest = md.digest(data.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : digest) sb.append(String.format("%02x", b));
return sb.toString();
}
// Sign with SHA512withRSA
private static String sign(String data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA512withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signed = signature.sign();
return Base64.getEncoder().encodeToString(signed);
}
// Read private key (PKCS#8 format)
private static PrivateKey getPrivateKeyFromPem(String pem) throws Exception {
pem = pem.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s+", "");
byte[] keyBytes = Base64.getDecoder().decode(pem);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
return KeyFactory.getInstance("RSA").generatePrivate(spec);
}
// Send HTTP POST request
private static String sendHttpPost(String urlStr, String jsonBody, String merchantId, String signature, String timestamp) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("MerchantID", merchantId);
conn.setRequestProperty("Signature", signature);
conn.setRequestProperty("Timestamp", timestamp);
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
InputStream is = (responseCode >= 200 && responseCode < 300) ? conn.getInputStream() : conn.getErrorStream();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) response.append(line);
return response.toString();
}
}
}