HMAC Signature Verification

The section describes how the hmac signature sent in the callback header can be verified

Obtain the Signing Key

The signing key is an alpha-numeric string generated by our platform during your merchant account creation and it is stored against your account record. This value can be found under your account details in the merchant dashboard. GBiPayments uses this value to create the HMAC signature and the same will be used when verifying the signature. It is recommended that it is copied and stored safely together with the security keys.

Below is the sample callback data to be used for the demonstration;

{
    "event": "transaction.charges",
    "payload": {
        "id": 11833,
        "merchant_reference": "MCTREFBNKWHXANJBYX2L",
        "internal_reference": "GBPREFFFZNGLVH96GSKK",
        "transaction_type": "COLLECTION",
        "request_currency": "UGX",
        "transaction_amount": 100000,
        "transaction_currency": "UGX",
        "transaction_charge": 3000,
        "transaction_account": "256777000001",
        "charge_customer": false,
        "total_credit": 97000,
        "provider_code": "mtn_momo_ug",
        "request_amount": 100000,
        "institution_name": "MTN Mobile Money Uganda",
        "customer_name": "JOHN DOE",
        "transaction_status": "PENDING",
        "status_message": "Collection initialized successfully. Confirm charges"
    }
}

Next Steps

  1. Obtain the value of the hmac-signature header. The value sent in the signature header takes the format t=timestamp,s=hmac_hash

  2. Form the string payload to be used in signature verification. This is obtained by concatenating values of the callback data in the format; event:merchant_reference:internal_reference:transaction_type:transaction_status and these values are obtained from the callback data. The string payload in this case would therefore be transaction.charges:MCTREFBNKWHXANJBYX2L:GBPREFFFZNGLVH96GSKK:COLLECTION:PENDING

  3. Create the hmac hash of the string payload.

  4. Compare the resulting hash to the value in the hmac-signature header. Equality means the signature is valid.

<?php

public function isValidSignature() {
    $strPayload = "transaction.charges:MCTREFBNKWHXANJBYX2L:GBPREFFFZNGLVH96GSKK:COLLECTION:PENDING";
    $signingKey = "your signing key string";
    $hmacSignature = "value of hmac-signature header";

    try {
      $timestamp = null;
      $hmacHash = null;

      // Split the hmacSignature into key-value pairs
      foreach (explode(",", $hmacSignature) as $sig_part) {
        [$key, $value] = explode("=", $sig_part);
        switch ($key) {
          case "t":
            $timestamp = $value;
            break;
          case "s":
            $hmacHash = $value;
            break;
        }
      }

      // Optional timestamp check based on your logic

      // Calculate the HMAC signature
      $signature = hash_hmac("sha256", $strPayload, $signingKey, false);

      // Compare the calculated and provided signatures
      return $signature === $hmacHash;
    } catch (Exception $e) {
      return false;
    }
}

?>

Below is a sample signature generated using the signing key: SGNKY5XMTK9CXFYKACJR

t=1722438477791,s=46c522f023bebe1931120485e620789b34f7ca99e6baa000b14f548815789691

Last updated