JavaScript

Integrate Hesabe Payment Gateway into your web applications using JavaScript. Complete encryption library with easy-to-use functions for secure payment processing.

Quick Start

Before you begin, ensure you have the necessary encryption library installed for AES functions:

NPM Installation

npm install --save hesabe-crypt

Alternatively, you can download the library files directly and use the encrypt and decrypt methods.

CDN Integration

If you're importing the library manually, you'll need to add the AES-JS CDN:

<script type="text/javascript" src="https://cdn.rawgit.com/ricmoo/aes-js/e27b99df/index.js"></script>

Import AES-JS and HesabeCrypt

Import the AES-JS library and the HesabeCrypt class before using them in the encryption/decryption functions below:

import aesjs from "aes-js";
import hesabeCrypt from "hesabe-crypt";

Encryption Function

Use this function to encrypt payment data before sending to Hesabe API:

export const getEncryptedData = value => {

    let secret_key = 'XXXXX' // Secret key provided by Hesabe
    let iv_key = 'XXXXX'     // IV key provided by Hesabe

    if(value) {
        let secret = secret_key  // merchant secret key
        let ivCode = iv_key      // merchant iv code
    
        let key = aesjs.utils.utf8.toBytes(secret);
        let iv = aesjs.utils.utf8.toBytes(ivCode);
    
        let instance = new hesabeCrypt(key, iv);
    
        let text = value;
        let encrypted = instance.encryptAes(JSON.stringify(text));
        let encrypted_data = encrypted;
    
        return encrypted_data;
    }
};

Decryption Function

Use this function to decrypt response data received from Hesabe API:

Padding fix for the npm package

hesabe-crypt (up to 2.0.5) strips the decryption padding incorrectly: it cuts the text at the first character that matches the pad byte, which truncates many real responses. Until a fixed version is published, override its pkcs5Strip with the corrected one below, as shown.

// Corrected padding removal: strips exactly the pad length and checks every pad byte.
// Overrides the buggy pkcs5Strip in hesabe-crypt (up to 2.0.5).
const pkcs5Strip = (txt) => {
  const pad = txt.charCodeAt(txt.length - 1);
  if (pad < 1 || pad > 32 || pad > txt.length) throw new Error('Invalid padding');
  for (let i = txt.length - pad; i < txt.length; i++) {
    if (txt.charCodeAt(i) !== pad) throw new Error('Invalid padding');
  }
  return txt.slice(0, txt.length - pad);
};

export const getDecryptData = value => {

  let secret_key = 'XXXXX' // Secret key provided by Hesabe
  let iv_key = 'XXXXX'     // IV key provided by Hesabe

  if(value) {
      let secret = secret_key  // merchant secret key
      let ivCode = iv_key      // merchant iv code
  
      let key = aesjs.utils.utf8.toBytes(secret);
      let iv = aesjs.utils.utf8.toBytes(ivCode);
  
      let instance = new hesabeCrypt(key, iv);
      instance.pkcs5Strip = pkcs5Strip; // use the corrected padding removal
  
      let text = value;
      let decrypted = instance.decryptAes(text);
      let decrypted_data = JSON.parse(decrypted);
      
      return decrypted_data;
  }
}

Download Kit

JavaScript KitDownload Link
Complete JavaScript integration kit with Vue.js examplesClick here