Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package fr.cap.rest;
  2.  
  3. import java.net.URLDecoder;
  4. import java.security.MessageDigest;
  5. import java.util.Comparator;
  6. import java.util.Locale;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.SortedMap;
  10. import java.util.TreeMap;
  11.  
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import org.apache.tomcat.util.codec.binary.Base64;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19.  
  20. @RestController
  21. @RequestMapping("/api/paiementenligne")
  22. public class RestApiPaiement {
  23.    
  24.    
  25.     @PostMapping("/callback")
  26.     public String callback(HttpServletRequest request, HttpServletResponse response) throws Exception {
  27.        
  28.         String returnValue="";
  29.        
  30.         String storeKey = "CapMission2019_2018";
  31.         // create sorted map
  32.         SortedMap<String, String> allRequestParams = new TreeMap<String, String>(new Comparator<String>() {
  33.             public int compare(String str1, String str2) {
  34.                 str1 = str1.toUpperCase(Locale.US);
  35.                 str2 = str2.toUpperCase(Locale.US);
  36.                 return str1.compareTo(str2);
  37.             }
  38.         });
  39.         // get all paramater map
  40.         Map<String, String[]> parameterMap = request.getParameterMap();
  41.         Set<String> requestParams = parameterMap.keySet();
  42.        
  43.         for (String requestParam : requestParams) {
  44.             String[] allRequestParamValues = parameterMap.get(requestParam);
  45.             if (allRequestParamValues != null && allRequestParamValues.length > 0) {
  46.                 String value = allRequestParamValues[0];           
  47.                 allRequestParams.put(requestParam, value);
  48.             }
  49.         }
  50.        
  51.         // init hash value
  52.         String hashval3 = "";
  53.         for (String requestParam : allRequestParams.keySet()) {
  54.             String lowerParam = requestParam.toLowerCase(Locale.US);
  55.             if (!lowerParam.equals("encoding") && !lowerParam.equals("hash")) {
  56.                 hashval3 += request.getParameter(URLDecoder.decode(requestParam, "UTF-8")).replace("\\", "\\\\")
  57.                         .replace("|", "\\|") + "|";
  58.             }
  59.         }
  60.        
  61.        
  62.         for(String str : allRequestParams.keySet()) {
  63.             System.out.println(str+"|"+allRequestParams.get(str));
  64.         }
  65.  
  66.        
  67.         storeKey = storeKey.replace("\\", "\\\\").replace("|", "\\|");
  68.         hashval3 += storeKey;
  69.  
  70.         MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
  71.         messageDigest.update(hashval3.getBytes());
  72.         String actualHash = new String(Base64.encodeBase64(messageDigest.digest()), "UTF-8");
  73.         String retrievedHash = request.getParameter("HASH");
  74.         String procReturnCode = request.getParameter("ProcReturnCode");
  75.        
  76.         System.out.println("Retrieved Hash : "+ retrievedHash );
  77.         System.out.println("Actual Hash : "+ actualHash );
  78.        
  79.        
  80.         System.out.println("Proc Return Code : "+ procReturnCode.equals("00") );
  81.  
  82.        
  83.        
  84.         if (actualHash.equals(retrievedHash) && procReturnCode.equals("00")) {
  85.             returnValue="ACTION=POSTAUTH";
  86.  
  87.         } else {           
  88.             returnValue="APPROVED";
  89.  
  90.         }
  91.        
  92.        
  93.         System.out.println(returnValue);
  94.        
  95.         return returnValue;
  96.        
  97.     }
  98.    
  99.    
  100.  
  101.    
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement