Search code examples
javahmacadyen

adyen HPP integration


I am trying to integrate the adyen hpp pages. I basically use the same code like in their example on github: https://github.com/Adyen/adyen-java-sample-code/blob/master/src/com/adyen/examples/hpp/CreatePaymentOnHpp_SHA_256.java

on that example I just set the merchantAccount, skinCode adn the hmac secret code to my data.

Additionally I added some code to create a test url for the hmac calculation:

        String queryString = params.keySet().stream()
            .map(key -> {
                try {
                    return key + "=" +  URLEncoder.encode(params.get(key), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return "Error: could not URL-encode value";
            }).collect(Collectors.joining("&"));

        String testUrl = "https://ca-test.adyen.com/ca/ca/skin/checkhmac.shtml" + "?" + queryString;

        System.out.println(testUrl);

I also created some code to create an url for the hpp. It looks like this:

        URIBuilder b = new URIBuilder(hppUrl);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            b.addParameter(entry.getKey(), URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return b.build().toString();

So the calculation of the HMAC seems to be correct, because the testUrl is always correct. But if I click on the generated link for the hpp, then I always get an error that I should check the HMAC calculation.

I hope someone can give me a hint on how to fix it


Solution

  • The URIBuilder already URL-encodes the parameters for you by default. The implementation you posted encodes your parameters twice, causing parameters such as your merchantReference to be different from the signing string that was used to calculate your merchant signature.

    The following code would fix the issue and create a valid HPP link:

    URIBuilder b = new URIBuilder(hppUrl);
    params.entrySet().forEach(e-> b.addParameter(e.getKey(),e.getValue()));
    System.out.println(b.build().toString());