com.midtrans.utils.Utility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-library Show documentation
Show all versions of java-library Show documentation
This is the Official Java API client/library for Midtrans Payment API. Visit https://midtrans.com. More information about the product and see documentation at http://docs.midtrans.com for more technical details. This library used java version 1.8
package com.midtrans.utils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Properties;
public final class Utility {
/**
* Create basic auth with Encode method is in the Base64.Encoder class that will return Basic auth String
*
* @param serverKey {String} - midtrans server-key/creator-key credential
* @return {String}
*/
public static String encodeBase64(String serverKey) {
return "Basic " + Base64.getEncoder().encodeToString((serverKey + ":").getBytes(StandardCharsets.UTF_8));
}
/**
* returns information about the version of Midtrans java library.
*
* @return {String}
*/
public static String getLibraryVersion() {
InputStream resourceAsStream = Utility.class.getResourceAsStream("/version.properties");
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
} catch (IOException e) {
return "Unable to reach version";
}
return (properties.getProperty("version") == null) ? "unable to reach" : properties.getProperty("version");
}
/**
* returns a boolean indicating whether the jsonString has the specified key as its own property
*
* @return {boolean}
*/
public static boolean hasOwnProperty(String jsonString, String jsonKey) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject.has(jsonKey);
} catch (JSONException ex) {
return false;
}
}
}