Callback Hash calculation Guide

Here you can find examples for appropriately computing the callback hash to match with ours.

The callback hash can be used to check the authenticity of the callback.

  1. Sort the callback data based on the KEY.

  2. Create a string with the callback data delimited with '|', except the hash in the callback data and suffix the secret_key with a '|' in front and please note that if any callback data is of type "JSON Object" convert value to JSON string.

    1. For example, for the below callback request body, the string to be used for the hash is:

    2. {
          "account_id" : 2,
          "transaction_id":"transaction_id_001",
          "order_id": "order_id_001",
          "currency": "BRL",
          "amount": 100,
          "amount_net":100,
          "amount_paid":100,
          "status": "SUCCESS",
          "type": "DEPOSIT",
          "event": {
              "code": "BD1001",
              "message": "Deposit successful",
          },
         "transaction_specific":
         		{
              "e2e_id":"CREDIT TRANSACTION CREATED BY SCRIPT",
              "name":"João da Silva",
              "tax_id":"44115144432"
            },
         "hash":"4B07C060B848E9FA64F04948167C31E290DEC91525AEF52CCC030422DD118BEF07C61EFA2535BFAC3246F0C71FFAC4EA26FAAF2CA7B3C7E2D08D178D8022C027"
       }
    3. account_id|amount|amount_net|amount_paid|currency|event|order_id|status|transaction_id|transaction_specific|type|API Secret Key

    4. 2|100|100|100|BRL|{JSON stringify the event object}|order_id_001|SUCCESS|transaction_id_001|{Stringify the transaction_specific object}|DEPOSIT|Your-API-Secret-Key

  3. Hash the string with SHA512 algorithm in 'HEX' format.

  4. After hashing, convert the output to uppercase.

  5. Now compare the calculated hash with the hash in the callback data to verify authenticity.

Example callback hash calculation

const crypto = require('crypto');

const hash_checker = (secret, callback_data) => {
  const shasum = crypto.createHash('sha512'),
    reqData = Object.assign({}, callback_data);
  let hash_data = '';
  const keys = Object.keys(reqData);
  keys.sort();
  for (let i = 0; i < keys.length; i++) {
    const k = keys[i];
    reqData[k] = reqData[k]
      ? typeof reqData[k] === 'number'
        ? reqData[k].toString()
        : reqData[k]
      : reqData[k] === 0
      ? '0'
      : '';
    if (k !== 'hash') {
      if (typeof reqData[k] === 'object') {
        hash_data += JSON.stringify(reqData[k]) + '|';
      }
      if (reqData[k].length > 0 && typeof reqData[k] !== 'object') {
        hash_data += reqData[k].toString() + '|';
      }
    }
  }

  hash_data = hash_data + secret;

  const calculated_callback_hash = shasum
    .update(hash_data)
    .digest('hex')
    .toUpperCase();
  if (calculated_callback_hash === callback_data.hash) {
    console.log('Hash Matched');
  } else {
    console.log('Hash does not matched');
  }
};
<?php

function hash_checker($secret, $callback_data) {
    $reqData = $callback_data;
    ksort($reqData);

    $hash_data = '';
    foreach ($reqData as $k => $v) {
        if ($k !== 'hash') {
            if (is_array($v)) {
                 if (empty($v)) {
                     $hash_data .= '{}' . '|';  
                 } else {
                     $hash_data .= json_encode($v, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '|';         
                 }
            } elseif ($v !== null) {
                $hash_data .= strval($v) . '|';
            }
        }
    }

    $hash_data .= $secret;
    echo $hash_data;

    $calculated_callback_hash = strtoupper(hash('sha512', $hash_data));

    if ($calculated_callback_hash === $callback_data['hash']) {
        echo 'Hash Matched';
    } else {
        echo 'Hash does not match';
    }
}
<?php

function hash_checker($secret, $callback_data) {
    $reqData = $callback_data;
    ksort($reqData);

    $hash_data = '';
    foreach ($reqData as $k => $v) {
        if ($k !== 'hash') {
            if (is_array($v)) {
                 if (empty($v)) {
                     $hash_data .= '{}' . '|';  
                 } else {
                     $hash_data .= json_encode($v) . '|'; 
                     
                 }
            } elseif ($v !== null) {
                $hash_data .= strval($v) . '|';
            }
        }
    }

    $hash_data .= $secret;
    echo $hash_data;

    $calculated_callback_hash = strtoupper(hash('sha512', $hash_data));

    if ($calculated_callback_hash === $callback_data['hash']) {
        echo 'Hash Matched';
    } else {
        echo 'Hash does not match';
    }
}

$secret="YOUR API SECRET";
$d = [
    "account_id" => 16,
    "order_id" => "eff6c22e-3337-4478-945a-5275910cb9e8",
    "currency" => "ZAR",
    "amount" => 1000,
    "amount_net" => 1000,
    "status" => "SUCCESS",
    "type" => "DEPOSIT",
    "event" => [
        "code" => "BD1001",
        "message" => "Deposit successful"
    ],
    "transaction_specific" => [
        "payer_account_number" => "NA",
        "payer_account_type" => "NA",
        "payer_bank" => "NA",
        "payer_bank_branch_code" => "NA",
        "payer_name" => "NA"
    ],
    "hash" => "033D12F9EFA017A041637F27771B3901C14B681E67E4205D78E56DA6E629041462A8073BF2DF87053AD19A6B90B64411BEA36163A7FBC8FD87ADC8E66CC7004A"
];

hash_checker($secret, $d);
?>


hash_checker($secret, $d);
?>
# * Callback data received from bitolo
callback_data = {
    "account_id": 17,
    "amount": 100,
    "amount_net": 100,
    "currency": "BRL",
    "event": {"code": "BD1001", "message": "Deposit successful"},
    "hash": "F020B6D70EEFD156785F1F38C765E60AAFA6C7C7AACC8577ADBF89E586AF4C047B3BCB3784ED200DBD85C3F8B1E9D9FF290AEE6D95483B12792EFFD87A4D1202",
    "order_id": "4508026b-2f05-40a6-8c0a-ea0ce1c932f0",
    "status": "SUCCESS",
    "transaction_specific": {"e2e_id": "NA", "name": "NA", "tax_id": "NA"},
    "type": "DEPOSIT",
}


def validate_transaction_hash(callback_data: dict) -> bool:
    bitolo_hash = callback_data.pop("hash")

    # * api_secret of your account received from bitolo (Note: use appropriate currency's account secret)
    api_secret: str = "--YOUR_API_SECRET--"

    # * Note: callback keys must be sorted before calculating hash
    callback_data_hash = ""
    callback_data_keys = list(callback_data.keys())
    callback_data_keys.sort()

    for key in callback_data_keys:
        value = callback_data[key]
        if isinstance(value, dict):
            value = json.dumps(value, separators=(",", ":"))
        elif isinstance(value, (int, float)):
            value = int(value) if value % 1 == 0 else value

        callback_data_hash += f"{value}|"

    callback_data_hash += api_secret
    callback_data_hash = (
        SHA512.new(callback_data_hash.encode("utf-8")).hexdigest().upper()
    )
    return bitolo_hash == callback_data_hash


print("Hash matched: ",validate_transaction_hash(callback_data))