Snippets
Created by
Ruben Fernandez
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 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)
You can clone a snippet to your computer for local editing. Learn more.