Level 1
External PHP Class:
<?php
namespace App\Classes;
class EncryptParameters{
/**
* Merchant's IV key
*/
private $iv;
/**
* Merchant's secret key
*/
private $key;
/**
* EncryptParameters constructor.
*/
public function __construct()
{
$this->iv = '2BfjH469WxmcJdPM';
$this->key = 'QpJXdbYv6tHLhCTK';
// $this->request = !empty($_POST)? $_POST:json_decode(file_get_contents('php://input'), true);
}
/**
* Encrypt the string of customer details with the IV and secret key.
*
* @param $payload Pass in the array of parameters to be pass to express checkout.
* @return string
*/
public function encryptData($payload = [])
{
//The encryption method to be used
$encrypt_method = "AES-256-CBC";
// Hash the secret key
$key = hash('sha256', $this->key);
// Hash the iv - encrypt method AES-256-CBC expects 16 bytes
$iv = substr(hash('sha256', $this->iv), 0, 16);
$encrypted = openssl_encrypt(
json_encode($payload, true),
$encrypt_method,
$key,
0,
$iv
);
//Base 64 Encode the encrypted payload
$encrypted = base64_encode($encrypted);
//echo json_encode($iv);
$result = array(
'params' => $encrypted,
'accessKey' => $payload['a$I.qb6TCaxdv5stvnZuvWGuRrTOvflWUo.EtSxFOmnLXd5P1Yegwim'],
'countryCode' => $payload['KE']
);
echo json_encode($result);
}
}
$class = new EncryptParameters;
$request = !empty($_POST)? $_POST:json_decode(file_get_contents('php://input'), true);
$class->encryptData($request);