<?php

$params = [
    'app_id' => '202306140000001',
    'timestamp' => time(),
    'nonce_str' => getRandomString(32),
    'data' => json_encode([
        'outOrderNo' => time().rand(1000, 9999),
        'outMemberId' => 'T001',
        'amount' => 100,
        'notifyUrl' => 'https://www.baidu.com',
    ]),
];
$params['sign'] = sign($params, 'nM2bUJT89njQGzoeDyK8cLmEYJloUsJX');
$response = post('http://66.42.38.42:9501/payment/pay', json_encode($params));
var_dump(json_decode($response, true));die();
// select * from `orders` where `app_id` = '202306140000001' and `out_order_no` = '16869297292619' limit 1
$params = [
    'app_id' => '202306140000001',
    'timestamp' => time(),
    'nonce_str' => getRandomString(32),
    'data' => json_encode([
        'outOrderNo' => '16869297292619',
    ]),
];
$params['sign'] = sign($params, 'nM2bUJT89njQGzoeDyK8cLmEYJloUsJX');
$response = post('http://146.70.113.165:9501/payment/query', json_encode($params));
var_dump(json_decode($response, true));

function post($url, $jsonStr) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $response;
}

function sign($params, $secretKey) {
    $signString =  $params['app_id'] . '&'
    . $params['timestamp'] . '&'
    . $params['nonce_str'] . '&'
    . $params['data'] . '&'
    . $secretKey;
    return md5($signString);
}

function getRandomString($length, $withSpecialChar = false)
    {
        $chars = array(
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
            '3', '4', '5', '6', '7', '8', '9'
        );
    
        if($withSpecialChar){
            $chars = array_merge($chars, array(
                '!', '@', '#', '$', '?', '|', '{', '/', ':', ';',
                '%', '^', '&', '*', '(', ')', '-', '_', '[', ']',
                '}', '<', '>', '~', '+', '=', ',', '.'
            ));
        }
    
        $charsLen = count($chars) - 1;
        shuffle($chars);
        
        $password = '';
        for($i=0; $i<$length; $i++){
            $password .= $chars[mt_rand(0, $charsLen)];
        }

        return $password;
    }