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

com.midtrans.snapbi.SnapBiApiRequestor 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.snapbi;

import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Map;

public class SnapBiApiRequestor {

    private static final OkHttpClient client;

    static {
        // Create a logging interceptor
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        if (SnapBiConfig.isEnableLogging()) {
            clientBuilder.addInterceptor(loggingInterceptor);
        }
        client = clientBuilder.build();
    }

    public static JSONObject remoteCall(String url, Map headers, Map body) throws IOException {
        
        JSONObject jsonBody = new JSONObject(body);
        String payloadJson = jsonBody.toString();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(JSON, payloadJson);

        Request.Builder requestBuilder = new Request.Builder().url(url).post(requestBody);

        for (Map.Entry entry : headers.entrySet()) {
            requestBuilder.addHeader(entry.getKey(), entry.getValue());
        }

        Request request = requestBuilder.build();
        try (Response response = client.newCall(request).execute()) {
            String responseBody = response.body() != null ? response.body().string() : "";
            return new JSONObject(responseBody);
        }
    }
}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy