callback - java

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static void main(String[] args) {
        callback();
    }


    public static void callback() {
        try {
            String merchantId = "a56e0-0a10c-73015-9f330";
            String key = "<Your Secret Key>"; // Your Secret Key

            // Sample JSON callback data
            String jsoncfn = "{\"statusCode\":200,\"merchant_id\":\"a56e0-0a10c-73015-9f330\",\"hash\":\"EyBdBWHMSXtaecqzdouSWm9E+HqmUDlfuvhWwuWG9j6Y/4N9yynQnjHtnc8EpoF0+xXRlzbIk2f5Ix8RVjSu7Q==\",\"data\":{\"symbol\":\"USDT\",\"address\":\"0xCF68707fAbAbcaE21D9Cf3a1BD8BF4b2123d669E\",\"block\":10436505,\"txid\":\"0xb17db1aae600f24c2a4d6cc48cc70ec31c1e53f7fdec2ae82b80edbfa2d28b0c\",\"confirmation\":16,\"balance\":1,\"usd_balance\":1,\"timestamp\":1594448100}}";
            

            Gson gson = new Gson();
            JsonObject response = gson.fromJson(jsoncfn, JsonObject.class);
            String dataJson = gson.toJson(response.getAsJsonObject("data"));
            Mac sha512Hmac = Mac.getInstance("HmacSHA512");
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA512");
            sha512Hmac.init(secretKey);
            byte[] hmacBytes = sha512Hmac.doFinal(dataJson.getBytes(StandardCharsets.UTF_8));
            String dataHash = Base64.getEncoder().encodeToString(hmacBytes);


            if (response.get("statusCode").getAsInt() == 200 || response.get("statusCode").getAsInt() == 1001) {
                System.out.println("merchant id equal ? " + response.get("merchant_id").getAsString().equals(merchantId));

                System.out.println("dataHash => " + dataHash + " hash => " + response.get("hash").getAsString());
                if (response.get("merchant_id").getAsString().equals(merchantId) && dataHash.equals(response.get("hash").getAsString())) {
                    System.out.println("success");
                    // Do something
                    // return {code:0} // return success
                } else {
                    System.out.println("error1");
                    // Do something
                }
            } else {
                System.out.println("error2");
                // Do something
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static String hashHmac(String data, String key) throws Exception {
        Mac sha512Hmac = Mac.getInstance("HmacSHA512");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA512");
        sha512Hmac.init(secretKey);
        byte[] hash = sha512Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(hash);
    }




Last updated