· 6 years ago · Feb 21, 2019, 11:54 AM
1@RequestMapping(value = "/order", method = RequestMethod.POST)
2 public ResponseEntity<Object> getWebhookOrder(@RequestBody String payload, @RequestHeader Map map) {
3
4 try {
5
6 String secretKey = "xxxxxxxxxxx";
7
8 String HMAC_ALGORITHM = "HmacSHA256";
9 Mac mac = Mac.getInstance(HMAC_ALGORITHM);
10 SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), HMAC_ALGORITHM);
11 mac.init(secretKeySpec);
12
13
14 String signature = new String(Hex.encodeHex(mac.doFinal(payload.toString().getBytes())));
15
16 System.out.println("header hmac "+map.get("x-shopify-hmac-sha256").toString());
17 System.out.println("generated hmac "+signature);
18 System.out.println(map.get("x-shopify-hmac-sha256").toString().equals(signature));
19 return new ResponseEntity<Object>("{}", HttpStatus.OK);
20
21 }catch(Exception exception) {
22
23 exceptionService.saveExceptions(map.get("x-shopify-shop-domain").toString(), exception);
24 return new ResponseEntity<Object>("{}", HttpStatus.BAD_REQUEST);
25
26 }
27}