Snippets

EasyPaymentGateway customerAPI

Created by Ruben Fernandez last modified
package com.easypaymentgateway.controller;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Arrays;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.easypaymentgateway.dbaccess.BusinessDelegate;
import com.easypaymentgateway.viewbean.MerchantBean;
import com.easypaymentsgateway.utils.EncryptionUtils;
import com.google.gson.Gson;

@Controller
@RequestMapping("/customerAPI")
public class CustomerAPIController {



	private static Logger logger = LoggerFactory.getLogger(CustomerAPIMvcController.class);

	@Resource(name = "businessDelegate")
    BusinessDelegate businessDelegate;
	
	/**
	 * Customer API endpoint.
	 * @param basetUrl Environment base url. <ul><li>Staging: https://checkout-stg.easypaymentgateway.com/EPGCheckout/rest/online/</li><li>Production: https://checkout-prd.easypaymentgateway.com/EPGCheckout/rest/online/</li></ul> 
	 * @param endpoint Customer Endpoint <ul><li>Customer Summary: customerSummary?</li><li>Customer: customer?</li></ul>
	 * @param format Response format: application/json or application/xml
	 * @param merchant MerchantBean json value.
	 * @param customerId Merchant customer id
	 * @param paymentSolution List of the payment solutions that you want to check, separated between commas. "skrill, neteller"
	 * @param customerPaysolId Payment solution account id
	 * @return Returning the customer information.
	 */
	@RequestMapping(value = "/customeEndpoint")
	public ResponseEntity<String> customeEndpoint(@RequestParam(value=API_CONSTANT.DEFAULT_URL,required=true) String basetUrl
												,@RequestParam(value=API_CONSTANT.ENDPOINT,required=true) String endpoint
												,@RequestParam(value=API_CONSTANT.FORMAT,required=true) String format
												,@RequestParam(value=API_CONSTANT.MERCHANT,required=true) String merchant
												,@RequestParam(value=API_CONSTANT.CUSTOMER_ID,required=true) String customerId
												,@RequestParam(value=API_CONSTANT.PAYMENT_SOLUTION,required=true ) String paymentSolution
												,@RequestParam(value=API_CONSTANT.CUSTOMER_PAYSOL_ID,required=false) String customerPaysolId){
		return customerExecution(basetUrl+endpoint, format, merchant, customerId, paymentSolution, customerPaysolId);
	}
	
	private ResponseEntity<String> customerExecution(String basetUrl, String format, String merchant, String customerId,
			String paymentSolution, String customerPaysolId) {
		try {
			MerchantBean merchantBean = new Gson().fromJson(merchant, MerchantBean.class);
			String parameters = MessageFormat.format(API_CONSTANT.PARAMETER_PATTERN,customerId, paymentSolution, customerPaysolId); 
			String url = buildUrl(basetUrl, parameters, merchantBean);
			return executeRequest(getAcceptHeaderMediaType(format), url);
		} catch (UnsupportedEncodingException e) {
			return buildExceptionResponse(format, e, "Unsupporting Encoding Exception");
		} catch (URISyntaxException e) {
			return buildExceptionResponse(format, e, "Error building the url");
		}
	}
	
	private ResponseEntity<String> buildExceptionResponse(String format, Exception e,
			String message) {
		logger.error(message ,e);
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(getAcceptHeaderMediaType(format));
		return new ResponseEntity<>(message, responseHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	private String buildUrl(String baseUrl, String parameters, MerchantBean merchant)
			throws UnsupportedEncodingException {
		String encrypted = EncryptionUtils.encrypt(parameters, merchant.getMerchant_password());
		String hashedDecryption = EncryptionUtils.hashMessage(parameters, EncryptionUtils.SHA256);
		return MessageFormat.format(API_CONSTANT.URL_PATTERN,baseUrl,URLEncoder.encode(encrypted, "UTF-8"),URLEncoder.encode(hashedDecryption, "UTF-8"),String.format("%d", merchant.getId()));   
	}

	private MediaType getAcceptHeaderMediaType(String format) {
		return StringUtils.contains(format, "xml")?MediaType.APPLICATION_XML: MediaType.APPLICATION_JSON;
	}

	private ResponseEntity<String> executeRequest(MediaType acceptHeaderMediaType, String urlS) throws URISyntaxException {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(acceptHeaderMediaType));
		ResponseEntity<String> responseEntity = restTemplate.exchange(new URI(urlS),  HttpMethod.POST, new HttpEntity<String>(headers), String.class);
		return buildResponseEntity(acceptHeaderMediaType, responseEntity);
	}

	private ResponseEntity<String> buildResponseEntity(MediaType acceptHeaderMediaType,
			ResponseEntity<String> responseEntity) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(acceptHeaderMediaType);
		return new ResponseEntity<>(responseEntity.getBody(), responseHeaders, HttpStatus.OK);
	}
	

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.