RSA Signature Verification

The section describes how the RSA signature sent in the callback header or redirect query data can be verified. For verification to succeed, the public key is required.

Download the Public Key File

Download the public key file by clicking the link below and store it somewhere on your server

Public key for sandbox webhook signature verification
Public key for production webhook signature verification

Next Steps

Below is the sample callback data for this demonstration (sample redirect data is available here);

{
    "event": "transaction.failed",
    "payload": {
        "id": 708,
        "merchant_reference": "MCTREFYDPE9LMZ34S8HM",
        "internal_reference": "GOVBILGHQ6ZDXFK7C7NJ",
        "transaction_type": "COLLECTION",
        "request_currency": "UGX",
        "request_amount": 35000,
        "transaction_currency": "UGX",
        "transaction_amount": 35000,
        "transaction_charge": 0,
        "charge_customer": false,
        "provider_code": "mtn_momo_ug",
        "transaction_status": "FAILED",
        "status_message": "Balance Insufficient for the transaction",
        "transaction_account": "256772000002",
        "customer_name": "JOHN DOE",
        "institution_name": "MTN",
        "total_credit": 0
    }
}
  1. Obtain the value of the rsa-signature header (if callback) OR the value of the rsa_signature query parameter (if redirect).

  2. Form the string payload to be used in signature verification. This is obtained by concatenating values of the callback/redirect data in the format; event:merchant_reference:internal_reference:transaction_type:transaction_status and these values are obtained from the callback/redirect data. The string payload would therefore be transaction.failed:MCTREFYDPE9LMZ34S8HM:GOVBILGHQ6ZDXFK7C7NJ:COLLECTION:FAILED

  3. Use the public key obtained above to verify the signature as described in the sample source codes below;

<?php

public function isValidSignature() {
    $file = "path-to-file/govnet.public.key.pem";
    $keyContent = file_get_contents($file);
    $publicKey = openssl_get_publickey($keyContent);
    $strPayload = "transaction.failed:MCTREFYDPE9LMZ34S8HM:GOVBILGHQ6ZDXFK7C7NJ:COLLECTION:FAILED";
    $signature = base64_decode("value-of-rsa-signature");

    /*true or false*/
    return openssl_verify($strPayload, $signature, $publicKey, "sha256") == 1;
}

?>

Below is an example of a generated RSA signature on the sandbox environment

KvaiKbXIf7t4iu8EyvvJp2OJUzseyNrJ6ZAHYxphM6ak1KuY6wWBkwIYLiEhTvYnpRGKN5Ohcw1jzHBYtS

Last updated