<?php
## Basic Information and Methods
## Code for reference only

// Test Merchant details
//$mch_id = '803070000102722';
//$sha256_key = '61A90B7D5F40639A23B42F588406D444BF8F9D637D6F9555EEA3E94977FA0A4E';
//$mch_public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC516eRzLLHGfCKXzxqZDQiVp7O30Pa4+/gmC46RzLzoUywq7ONyrKBP+ds/5d5xliOlbz2pyuFvIUwjysuFOSbAndmnGw9VgiUBvcb8+RgB9MjcT0u6VMIiON3QF/0/jZYc6646wUScdRk+8Xs95H0YQkxEt1b9PvR5GSXijMkzwIDAQAB';

//$mch_private_key = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALnXp5HMsscZ8IpfPGpkNCJWns7fQ9rj7+CYLjpHMvOhTLCrs43KsoE/52z/l3nGWI6VvPanK4W8hTCPKy4U5JsCd2acbD1WCJQG9xvz5GAH0yNxPS7pUwiI43dAX/T+NlhzrrjrBRJx1GT7xez3kfRhCTES3Vv0+9HkZJeKMyTPAgMBAAECgYEArmaA7MRKaaogH9qr2bPbcPYU5lKDXSFW9VAa4bn7SMLwM0OOptu3lK+hwJpDTpYiePxwbpAGueRmoWFZxk75EB1NEL64BWXpwk72flxAqpMXELI1cdb1d2dbngES35Kaw/wdNugG3E1PAdtFNwcSQLoyXX1XyudRW9KzjETFvcECQQDfHLt3+UorZ5jIRr3/51bU/U5R9BAW4o/k6UIGWQAnsjzHGVnU+ewym19marFlvREaTUCIlAu3Gp3mkjxBTjQ/AkEA1TyGFKdHqBKKeOsisW9EaBD/qmcqNXehEkDlx2oHWtKifiZ8xqZO4VhpCCtc85G9XvrpI/nXtW7Wt/yI9DircQJAMXAyckFBGCEaj7ZtP5lJemSuyCYmsot9iyHMyY9LI4CxSwTpDPJcTlJzb0RRDtQ4R/W6SQlVFzvEpWTeCTKFDQJAV5bM3J756Cf1iUXMsmWqC7Uvz6cgx2WkXAMcX5VGACCVbowQpOibbiUKv7hQLhynVORr78Mov/YPbPPmraq5gQJBAMrBouURsWlChAi1pwW2QHpbcfXbkuZvRpDn5c/G9SijT6g79eBxBMTUAQn0d5963s27hG4ChInfrTlWX3dRbnc=';

// Merchant details
$mch_id = '801200033625051';
$sha256_key = '738369F9B754E6F5696F79869E6042FF3D044783DC072CC66669E25E0A2026FD';
$mch_public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDySYJKMA3xQ75SMtcfc8b5d1lkErvhuN9tMET6m+T8BNqSB7Hx1vi1QuKWILUrBLdBSDzlvVGE7k5lQgAm4ig+St21LVZokMgrjjV2gIQ6MDstsyDrX2kD3q0fWJBnPc87cGE2FRCT35VBNyEwWM2iketJlnt4hzP7SGms9ifQQwIDAQAB';

$mch_private_key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAPJJgkowDfFDvlIy1x9zxvl3WWQSu+G4320wRPqb5PwE2pIHsfHW+LVC4pYgtSsEt0FIPOW9UYTuTmVCACbiKD5K3bUtVmiQyCuONXaAhDowOy2zIOtfaQPerR9YkGc9zztwYTYVEJPflUE3ITBYzaKR60mWe3iHM/tIaaz2J9BDAgMBAAECgYAMXcYhpTxA8TU4mtGyEEW6m+XbHr2qMrZsq0cKB4TzfAfv5ial8lP1ZHmOMhMtI+t6kIZTL4uGnmcv8eNyGJ73HYsqtBKhk6Dr9743sR0IC0qgZKwpCMvtImqQVOJNsH4Rdb+iHx4nqJGTdHScwBJk4ISnWkVZH/GhhXLY+qRD0QJBAP6eTg/0dHG6OkSS8CM3xFwT1aP01MAZyD+ciWilVy3V4jl0Ffs24pfy2RXKpvbee3GukLhU1oSXC2fGKDwsUckCQQDzmhMQbE/wo0mhofSyzmZYMmOEJcI17YGy1UrmwyVHBOiSwfihI1ViyQ2+TOC62n456U/fF5oufBGy2CPMozerAkBhJatpIP8wyTokIeAnv8ZWz2aLGQX3iZ7CzooK+FLL9ThMbYNZhzZrfUfgraKIvJhi5pTnD8+NZUf/vzZoxwwpAkA3Yiw7a6SxHS68ZMnxbSOvoaUmgj8pa5o1QnEJDg0HnIIT+62/VP0wvG03etsNIGfaWsUh/4e6UdLkSme5OtKJAkEAlPpo3gDlizfjG3wx19HPfVdaHR2iVCoTyewmYE0UFDieQwrtolIaQ7kCYHIlClnSPWzl7O7NnNRudHNFMLlkBw==';


/* --------------------------------
   ENCRYPT FUNCTION (UNCHANGED)
--------------------------------- */
function encrypt($data) {
    global $mch_private_key;

    $signA = makeSign($data);

    $pem = chunk_split($mch_private_key, 64, "\n");
    $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
    $private_key = openssl_pkey_get_private($pem);

    openssl_private_encrypt($signA, $encryptData, $private_key);

    return base64_encode($encryptData);
}

/* --------------------------------
   FIXED: cURL VERSION
--------------------------------- */
if (!function_exists('globalpay_http_post_res_json')) {
    function globalpay_http_post_res_json($url, $postData) {

        $ch = curl_init($url);

        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => $postData,
            CURLOPT_HTTPHEADER     => [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($postData)
            ],
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_SSL_VERIFYPEER => true,
        ]);

        $result = curl_exec($ch);

        if ($result === false) {
            curl_close($ch);
            return false;
        }

        curl_close($ch);
        return $result;
    }
}

/* --------------------------------
   SIGN FUNCTION
--------------------------------- */
function makeSign($data) {
    global $sha256_key;

    ksort($data);
    $str = '';

    foreach ($data as $k => $v) {
        if ($v !== '' && $v !== null) {
            $str .= $k . '=' . $v . '&';
        }
    }

    $str = rtrim($str, '&');
    return hash_hmac('sha256', $str, $sha256_key);
}

/* --------------------------------
   TIMESTAMP (MS)
--------------------------------- */
function msectime() {
    list($msec, $sec) = explode(' ', microtime());
    return (int)round(($sec + $msec) * 1000);
}
