Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.cap.rest;
- import java.net.URLDecoder;
- import java.security.MessageDigest;
- import java.util.Comparator;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Set;
- import java.util.SortedMap;
- import java.util.TreeMap;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.tomcat.util.codec.binary.Base64;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @RequestMapping("/api/paiementenligne")
- public class RestApiPaiement {
- @PostMapping("/callback")
- public String callback(HttpServletRequest request, HttpServletResponse response) throws Exception {
- String returnValue="";
- String storeKey = "CapMission2019_2018";
- // create sorted map
- SortedMap<String, String> allRequestParams = new TreeMap<String, String>(new Comparator<String>() {
- public int compare(String str1, String str2) {
- str1 = str1.toUpperCase(Locale.US);
- str2 = str2.toUpperCase(Locale.US);
- return str1.compareTo(str2);
- }
- });
- // get all paramater map
- Map<String, String[]> parameterMap = request.getParameterMap();
- Set<String> requestParams = parameterMap.keySet();
- for (String requestParam : requestParams) {
- String[] allRequestParamValues = parameterMap.get(requestParam);
- if (allRequestParamValues != null && allRequestParamValues.length > 0) {
- String value = allRequestParamValues[0];
- allRequestParams.put(requestParam, value);
- }
- }
- // init hash value
- String hashval3 = "";
- for (String requestParam : allRequestParams.keySet()) {
- String lowerParam = requestParam.toLowerCase(Locale.US);
- if (!lowerParam.equals("encoding") && !lowerParam.equals("hash")) {
- hashval3 += request.getParameter(URLDecoder.decode(requestParam, "UTF-8")).replace("\\", "\\\\")
- .replace("|", "\\|") + "|";
- }
- }
- for(String str : allRequestParams.keySet()) {
- System.out.println(str+"|"+allRequestParams.get(str));
- }
- storeKey = storeKey.replace("\\", "\\\\").replace("|", "\\|");
- hashval3 += storeKey;
- MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
- messageDigest.update(hashval3.getBytes());
- String actualHash = new String(Base64.encodeBase64(messageDigest.digest()), "UTF-8");
- String retrievedHash = request.getParameter("HASH");
- String procReturnCode = request.getParameter("ProcReturnCode");
- System.out.println("Retrieved Hash : "+ retrievedHash );
- System.out.println("Actual Hash : "+ actualHash );
- System.out.println("Proc Return Code : "+ procReturnCode.equals("00") );
- if (actualHash.equals(retrievedHash) && procReturnCode.equals("00")) {
- returnValue="ACTION=POSTAUTH";
- } else {
- returnValue="APPROVED";
- }
- System.out.println(returnValue);
- return returnValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement