package com.gpc.tams.util; import com.gpc.tams.json.CheckPinpadResp; import com.gpc.tams.json.SaleResp; import com.gpc.tams.json.SaleErrorResp; import com.gpc.tams.json.SaleReq; import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.http.*; import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.client.HttpClientErrorException; import java.io.IOException; import java.util.Arrays; @Service public class PasClient { private static final ObjectMapper mapper = new ObjectMapper(); public static final String PAYMENT_URL = "http://localhost:14447/v1"; public static final String SALE_ENDPOINT = "/transaction/sale"; public static final String CHECK_PINPAD_ENDPOINT = "/pinpad"; public static final String REVERSAL_ENDPOINT = "/transaction/reversal"; public static final String REFUND_ENDPOINT = "/transaction/refund"; public static final String BALANCE_ENDPOINT = "/transaction/inquiry"; public static final String SIGNATURE_ENDPOINT = "/signature"; public static final String SUBNET_PREFIX = "172.16.1"; public static final String REQ_BODY = "Request Body: "; public static final String RESP_BODY = "Response Body: "; public static final String URL = "URL: "; public static final String X_COORELATION_ID = "x-correlation-id"; public static final String CURRENCY_USD = "USD"; public static final String NO_IMPLEMENTATION = "There is no implementation for the NAPA Payment - "; public static final String ERROR_CODE_1000 = "val-1000"; public static final String ERROR_CODE_1001 = "val-1001"; public static final String ERROR_CODE_1002 = "val-1002"; public static final String ERROR_CODE_9000 = "val-9000"; public static final String ERROR_CODE_9001 = "val-9001"; private Logger logger; RestTemplate restTemplate; public PasClient() { restTemplate = new RestTemplate(); MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter(); converter.setObjectMapper(new ObjectMapper()); restTemplate.getMessageConverters().add(converter); } public ResponseEntity post(String request, String url, String uuid) { log(REQ_BODY + request); log(URL + url); log(X_COORELATION_ID + uuid); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.add(X_COORELATION_ID, uuid); HttpEntity entity = new HttpEntity(request, headers); try { ResponseEntity response = restTemplate.postForEntity(url, entity, String.class); String resp = response.getBody(); log(RESP_BODY + resp); return response; } catch (Exception e) { logError("post failed with status code", e); throw e; } } public String get(String url, String uuid) { log(URL + url); log(X_COORELATION_ID + uuid); HttpHeaders headers = new HttpHeaders(); headers.add(X_COORELATION_ID, uuid); HttpEntity entity = new HttpEntity(headers); ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); String resp = response.getBody(); log(RESP_BODY + resp); return resp; } public void setLogger(Logger logger) { this.logger = logger; } public static boolean is2xxSuccessful(HttpStatus status) { int[] successfulCodes = {200, 201, 202}; return ArrayUtils.contains(successfulCodes, status.value()); } public static boolean isDisallowed(RefTenderType refTenderType, Integer[] disallowedRefTenderTypes) { if (ArrayUtils.isEmpty( disallowedRefTenderTypes )) { return false; } if (refTenderType == null) { return true; } // there are two tender types - the general one - Credit/Debit/Gift // and the more specific ones: // // Wright Express (credit/fleet card) // Voyager (credit/fleet card) // American Express (credit/amex) // Mastercard (credit (or debit)/mastercard) // Visa (credit (or debit)/visa) // Discover (credit/discover) // // The general tender type is returned explicitly in the PX message, but the more specific one isn't // always obvious. // return (Arrays.asList(disallowedRefTenderTypes).contains(new Integer(refTenderType.getRefTenderTypeId()))); } private void log(String mesg) { if (logger != null) { logger.info(mesg); } } private void logError(String mesg) { if (logger != null) { logger.error(mesg); } } private void logError(String mesg, Exception e) { if (logger != null) { logger.error(mesg, e); } } }