Skip to Content

React Native

Integrate Hesabe Payment Gateway into your React Native application. Cross-platform mobile development framework with JavaScript and native performance for seamless payment processing.

React Native Integration Steps

You will find the following files in the comprehensive iOS Swift Kit:

Library Installation

You can install the encryption library for React Native using npm:

 npm install --save hesabe-crypt

You can also download the library files directly and use the encrypt and decrypt methods.

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 Implementation

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 provided key 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 Implementation

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 provided key 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

Complete React Native integration kit with examples

Example KitDownload Link
Download React Native KitClick here