<?php


function P_SIGN_ENCRYPT($OrderId, $Timestamp, $trtType, $Amount)
{
        $MAC  = '';
        $RSA_KeyPath = 'secure/key.pem';
        $RSA_Key = file_get_contents ($RSA_KeyPath);
        $Data = array (
                        'ORDER' => $OrderId,
                        'NONCE' => '11111111000000011111',
                        'TIMESTAMP' => $Timestamp,
                        'TRTYPE' => $trtType,
                        'AMOUNT' => $Amount
                );
        if (!$RSA_KeyResource = openssl_get_privatekey ($RSA_Key)) die ('Failed get private key');
        $RSA_KeyDetails = openssl_pkey_get_details ($RSA_KeyResource);
        $RSA_KeyLength = $RSA_KeyDetails['bits']/8;

        foreach ($Data as $Id => $Filed) $MAC .= strlen ($Filed).$Filed;

//$MAC = '61000012011111111000000011111142011062706010010534.99';

openssl_sign($MAC,$signature,$RSA_Key,OPENSSL_ALGO_SHA256);
openssl_free_key($pkeyid);

        $P_SIGN = bin2hex ($signature);
        return strtoupper ($P_SIGN);
}

echo "".P_SIGN_ENCRYPT ($_POST["ORDER"],$_POST["TIMESTAMP"],$_POST["TRTYPE"],$_POST["AMOUNT"])."";


?>
