<?php
//https://ciyon.local.ciy.cn/ajax/api/v1_test.transfer
//https://ciyon.local.ciy.cn/ajax/api/v1_test.query
namespace web\api;

class v1_test {
    public static function json_transfer() {
        $param = array();
        $param['thirdno'] = '34538';
        $param['amount'] = 100;
        $param['notify_url'] = 'https://ciyon.ciy.cn/api/recv.transfer';
        $param['timestamp'] = time();
        $waitsignstr = 'amount=100,addtimes=' . $param['timestamp'];
        clog($waitsignstr);
        $param['sign'] = self::sign($waitsignstr);
        $param = self::send($param, 'transfer');
    }
    public static function json_query() {
        $param = array();
        $param['thirdno'] = '34538';
        $param['timestamp'] = time();
        $param = self::send($param, 'query');
    }
    static function send($param, $func) {
        global $db;
        $appid = 215695;
        $csql = new \ciy\sql('ap_api');
        $csql->where('id', $appid);
        $apirow = $db->getone($csql);
        $apisecret = $apirow['apisecret'];

        $param['appid'] = $appid;
        $signstr = '';
        ksort($param);
        foreach ($param as $key => $value) {
            if ($key == 'sign')
                continue;
            $signstr .= $key . '=' . $value . '&';
        }
        $signstr = substr($signstr, 0, -1);
        $signstr .= $apisecret;
        $hash = sha256($signstr);
        $param['hash'] = $hash;
        $urlget = 'https://ciyon.local.ciy.cn/ajax/api/v1.' . $func;
        $http = new \ciy\http();
        $http->request($urlget, json_encode($param));
        $statcode = $http->get_statcode();
        clog('test http status code: ' . $statcode);
        $result = $http->get_data();
        clog($result);
        exit();
    }
    static function sign($data) {
        $hashbin = hex2bin(sha256($data));
        $prikey = <<<EOD
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDI1V1v9Gf7lJRW
lbVoYUg6I/nwHNnysCmAn5ImkMQ9tGNLb4BzHhaBPrxOArEMvXHz8cadH7DcmVkr
khpaF9I4jLTW1w5o/U5YMn0dshwF8XA1izS67D6GtUO/JSubczbL0TrCNZeqSiuK
UrSmlqpM7nYpQdSa6URyXo4x32c0mbX6+42cfsOE4Ye3hW1vDZKZOQA4buVoYO0b
3uVbysQ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SabPD5A5qPLa5wBAz6FM6f1hq2WH5aKsU9
jJJ0bNCXRuch8jlODnNPw3a1Cf4h0B1mpis6Ik59Py0Je6OjvuxsE0hiFx04AZ5r
bfT3eEI2YaF9u2n4AxjbyBIIBSUWe9lqHI1g95OnAoGATtwi1tKfQ8qKPveBexMd
AoHAJW8CxnCQEC/2mqydiKiLQZTCTXqJqZLa4SvuHojMnDlnbslA5C69d6mBDWA+
mZ8Y/QE6M+LieH7UXGe5IhGdB4aympiOA1Ig/uDdpbjy0/OKi7UH4ZbDAtJ6jORn
N/r/sMhXDVJBjxNf3nJijDU=
-----END PRIVATE KEY-----
EOD;
        $privateKey = $prikey;
        if (strpos($privateKey, '-----BEGIN RSA PRIVATE KEY-----') === false && strpos($privateKey, '-----BEGIN PRIVATE KEY-----') === false)
            $privateKey = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
        $result = openssl_sign($hashbin, $signature, $privateKey, OPENSSL_ALGO_SHA256);
        if ($result !== true)
            return errjson('签名错误:' . openssl_error_string());
        return bin2hex($signature);
    }
}
