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

cl.transbank.webpay.WebpayApiResource Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package cl.transbank.webpay;

import cl.transbank.WebpayApiResponseManager;
import cl.transbank.common.Options;
import cl.transbank.exception.TransbankException;
import cl.transbank.util.BeanUtils;
import cl.transbank.util.HttpUtil;
import cl.transbank.util.HttpUtilImpl;
import cl.transbank.webpay.exception.WebpayException;
import cl.transbank.model.WebpayApiRequest;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public abstract class WebpayApiResource {
    @Getter @Setter private static HttpUtil httpUtil = HttpUtilImpl.getInstance();

    public static Map buildHeaders(Options options) {
        if (null == options)
            return null;

        Map headers = new HashMap<>();
        headers.put(options.getHeaderCommerceCodeName(), options.getCommerceCode());
        headers.put(options.getHeaderApiKeyName(), options.getApiKey());

        return headers;
    }

    public static  T execute(final URL endpoint, HttpUtil.RequestMethod method, final Options options, Class clazz)
            throws TransbankException, IOException {
        return execute(endpoint, method, null, options, clazz);
    }

    public static  T execute(final URL endpoint, HttpUtil.RequestMethod method, final WebpayApiRequest request, final Options options)
            throws TransbankException, IOException {
        return execute(endpoint, method, request, options, null);
    }
    public static  T execute(final URL endpoint, HttpUtil.RequestMethod method, final WebpayApiRequest request, final Options options, Class clazz)
            throws TransbankException, IOException {
        final WebpayApiResponseManager out = WebpayApiResource.getHttpUtil().request(endpoint, method, request, WebpayApiResource.buildHeaders(options), WebpayApiResponseManager.class);

        if (null == out)
            return null;

        if (null != out.getErrorMessage())
            throw new WebpayException(out.getErrorMessage());

        if (null == clazz)
            return null;

        try {
            return BeanUtils.getInstance().copyBeanData(clazz.newInstance(), out);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new TransbankException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy