ksernow
ksernow

Reputation: 714

CashFree webhook verification sample code does not work

I am trying to verify webhook for cash free payment gateway. I have ensure my secret key is correct but the sample code never computes the correct singature as coming from the webhook. https://docs.cashfree.com/v3/docs/test-and-validate-webhooks-1

public String generateSignature() {

    bufferedReader = request.getReader();
  String line;
  while ((line = bufferedReader.readLine()) != null) {
     stringBuilder.append(line).append('\n');
  }      
  String payload = stringBuilder.toString();
  String timestamp = request.getHeader("x-webhook-timestamp");
      
  String data = timestamp+payload;

  String secretKey = "SECRET-KEY"; // Get secret key from Cashfree Merchant Dashboard;
  Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
  SecretKeySpec secret_key_spec = new SecretKeySpec(secretKey.getBytes(),"HmacSHA256");
  sha256_HMAC.init(secret_key_spec);
  String computed_signature = Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(data.getBytes()));
  return computed_signature; // compare with "x-webhook-signature"
}

Upvotes: 0

Views: 169

Answers (0)

Related Questions