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

com.hps.integrator.services.HpsRestGatewayService Maven / Gradle / Ivy

Go to download

The SecureSubmit Java SDK simplifies processing of credit card transactions using Heartland Payment Systems' Portico Payment Gateway

There is a newer version: v2.5.2
Show newest version
package com.hps.integrator.services;

import com.google.gson.Gson;
import com.hps.integrator.abstractions.IHpsServicesConfig;
import com.hps.integrator.infrastructure.HpsException;
import org.apache.commons.codec.binary.Base64;
import sun.misc.IOUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public abstract class HpsRestGatewayService {

    int limit = -1;
    int offset = -1;

    IHpsServicesConfig servicesConfig;

    protected HpsRestGatewayService(IHpsServicesConfig config) {
        if(config != null) {
            this.servicesConfig = config;
        }
    }

    protected String doRequest(String verb, String endpoint) throws HpsException {
        return this.doRequest(verb, endpoint, null, null, null);
    }
    protected String doRequest(String verb, String endpoint, Object data) throws HpsException {
        return this.doRequest(verb, endpoint, data, null, null);

    }
    protected String doRequest(String verb, String endpoint, Object data, HashMap additionalHeaders, HashMap queryStringParameters) throws HpsException {
        HttpsURLConnection conn;
        String mUrl = servicesConfig.getServiceUri();


        try {
            String queryString = null;
            //Query string
            if(queryStringParameters != null) {

                StringBuilder sb = new StringBuilder();
                sb.append("?");
                for (Map.Entry entry : queryStringParameters.entrySet()) {
                    if (sb.length() > 0) {
                        sb.append("&");
                    }
                    sb.append(String.format("%s=%s",
                            URLEncoder.encode(entry.getKey(), "UTF-8"),
                            URLEncoder.encode(entry.getValue(), "UTF-8")
                    ));
                }
                queryString = sb.toString();
                conn = (HttpsURLConnection)new URL(mUrl + endpoint + queryString).openConnection();
            }else{
                conn = (HttpsURLConnection)new URL(mUrl + endpoint).openConnection();
            }


        }
        catch (IOException e) { throw new HpsException(e.getMessage(), e); }

        Gson gson = new Gson();
        String result = "";

        try {

            conn.setDoInput(true);
            conn.setRequestMethod(verb);
            conn.addRequestProperty("Content-Type", "application/json");

            //Headers
            if(additionalHeaders != null) {
                for (Map.Entry entry : additionalHeaders.entrySet()) {
                    conn.addRequestProperty(entry.getKey(), entry.getValue());
                }
            }

            //Payload
            if (!verb.equals("GET"))
            {
                if(data != null) {
                    String payload = gson.toJson(data);
                    System.out.println(payload);

                    byte[] bytes = payload.getBytes();

                    conn.setDoOutput(true);
                    conn.addRequestProperty("Content-Length", String.format("%s", bytes.length));

                    DataOutputStream requestStream = new DataOutputStream(conn.getOutputStream());
                    requestStream.write(bytes);
                    requestStream.flush();
                    requestStream.close();
                }
            }

            InputStream responseStream = conn.getInputStream();
            result += new String(IOUtils.readFully(responseStream, conn.getContentLength(), true));
            System.out.println(result);
            responseStream.close();

            return result;
        }
        catch (IOException e) {
            try {
                if (conn.getResponseCode() == 400) {
                    InputStream errorStream = conn.getErrorStream();
                    String errorMessage = new String(IOUtils.readFully(errorStream, errorStream.available(), false));
                    errorStream.close();

                    throw new HpsException(errorMessage, e);
                } else {
                    throw new HpsException(e.getMessage(), e);
                }
            } catch (IOException ie) { throw new HpsException(e.getMessage(), ie); }
        }
    }

    protected  T hydrateObject(String data, Class clazz) {
        Gson gson = new Gson();
        return gson.fromJson(data, clazz);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy