All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.midtrans.utils.Utility Maven / Gradle / Ivy

Go to download

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

There is a newer version: 3.2.0
Show newest version
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;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy