PHP

Integrate Hesabe Payment Gateway into your PHP application. Complete encryption library with AES encryption and decryption capabilities for secure payment processing.

Quick Start

The encryption and decryption snippets are placed separately in the documentation for clarity. Make sure to choose the correct PHP kit version based on your server's PHP version to ensure compatibility and optimal performance.

Basic Usage

You can find Request and Response classes in the kit. Use the encrypt() and decrypt() methods from the HesabeCrypt class:

<?php
$encrypted = HesabeCrypt::encrypt($requestDataJson, $encryptionKey, $ivKey);
$decrypted = HesabeCrypt::decrypt($checkoutResponseContent, $encryptionKey, $ivKey)

HesabeCrypt Class - Encryption

The encryption method uses AES-256-CBC algorithm for secure data transmission:

<?php

namespace App\Libraries;

class HesabeCrypt {
// AES Encryption Method Starts
public static function encrypt($str, $key, $ivKey)
{
    $str = self::pkcs5_pad($str);
    $encrypted = openssl_encrypt($str, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $ivKey);
    $encrypted = base64_decode($encrypted);
    $encrypted = unpack('C*', ($encrypted));
    $encrypted = self::byteArray2Hex($encrypted);
    $encrypted = urlencode($encrypted);
    return $encrypted;
}

private static function pkcs5_pad($text)
{
    $blocksize = 32;
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text.str_repeat(chr($pad), $pad);
}

private static function byteArray2Hex($byteArray)
{
    $chars = array_map("chr", $byteArray);
    $bin = join($chars);
    return bin2hex($bin);
}

HesabeCrypt Class - Decryption

The decryption method for AES algorithm to process response data:

  public static function decrypt($code, $key, $ivKey)
  {
      if (!(ctype_xdigit($code) && strlen($code) % 2 == 0)) {
          return false;
      }
      $code = self::hex2ByteArray(trim($code));
      $code = self::byteArray2String($code);
      $iv = $key;
      $code = base64_encode($code);
      $decrypted = openssl_decrypt($code, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $ivKey);
      return self::pkcs5_unpad($decrypted);
  }

  private static function hex2ByteArray($hexString)
  {
      $string = hex2bin($hexString);
      return unpack('C*', $string);
  }

  private static function byteArray2String($byteArray)
  {
      $chars = array_map("chr", $byteArray);
      return join($chars);
  }

  private static function pkcs5_unpad($text)
  {
      $pad = ord($text{strlen($text) - 1});
      if ($pad > strlen($text)) {
          return false;
      }
      if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
          return false;
      }
      return substr($text, 0, -1 * $pad);
  }
}

Download Kit

KitsDownload Link
For PHP versions below 8.3 (Legacy)Click here
For PHP 8.3 and latest versions (Latest)Click here