当前位置: 首页 > 工具软件 > SM3-PHP > 使用案例 >

招行收款通支付对接来了个国密sm2,sm3,文档也写得不完整,给的demo还是java,哎苦了我们php的,现在直接给出php版本

强宾白
2023-12-01
招行收款通支付对接来了个国密sm2,sm3,文档也写得不完整,给的demo还是java,哎苦了我们php的,现在直接给出php版本
$post_data = [
    'version'=>'2.0',
    'merchId'=>$merchId, //商户号
     'payNo'=>$payNo,
    'orderId' => $orderId,
    'feeAmt'=>$money,
    'custName'=>$name,
    'custPhone'=>$tel,
    'userId'=>$userId,
    'returnUrl'=>$returnUrl
];
ksort($post_data);

//body SM3
$jsonstr=json_encode($post_data);echo 'BODY:'; var_dump($jsonstr);
$sign=$sm3->digest($jsonstr,1);

//Header SM2
$post_data_apisign = array();
$post_data_apisign["appid"]=$appid;
$post_data_apisign["secret"]=$appsecret;
$post_data_apisign["timestamp"]=$timestamp;
$post_data_apisign["sign"]=$sign;

//$apisign = Sign($post_data_apisign,$privateKey);//签名

ksort($post_data_apisign);
$post_data_apisign = ToUrlParams($post_data_apisign);
$apisign = $sm2->doSign($post_data_apisign,$privateKey,'1234567812345678');
$apisign = SM2_Asn1ToRaw($apisign);
echo '<br>';
$header = get_header_arr($sign,$apisign,$appid,$appsecret,$timestamp,$verify);var_dump($header);
$result = curl_post($requert_url,$jsonstr,$header);var_dump($result);
$result = json_decode($result,true);
if($result['respCode'] == 'CMBMB99'){
    header("location:{$result['sytUrl']}");
}


/**  参数排序拼接
 * @param $array
 * @return string
 */
function ToUrlParams(array $array){
    $buff = "";
    foreach ($array as $k => $v)
    {
        if($v != "" && !is_array($v)){
            $buff .= $k . "=" . $v . "&";
        }
    }
    $buff = trim($buff, "&");
    return $buff;
}


function SM2_Asn1ToRaw($apisign)
{
    $apisign = hex2bin($apisign);
    $bytes = array_values(unpack('C*', $apisign));
    $pos = 3;
    if ($bytes[$pos] == 32) {
        $pos += 1;
    } elseif ($bytes[$pos] == 33) {
        $pos += 2;
    }

    $data = array_slice($bytes, $pos, 32);
    $pos += 32;
    $pos += 1;
    if ($bytes[$pos] == 32) {
        $pos += 1;
    } elseif ($bytes[$pos] == 33) {
        $pos += 2;
    }

    $data = array_merge($data, array_slice($bytes, $pos, 32));

    $str = pack('C*', ...$data);

    return bin2hex($str);
}


function curl_post($url,$params,$header){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    if(!empty($header)){
        curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );
    }

    if(is_array($params)){
        $params = json_encode($params);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS,$params);

    $data = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);//关闭cURL会话
    return $data;
}

另外,很多人来问sm2和sm3的封装类再哪,百度、GitHub上面很多,下面给出一个一个地址,有需要自取,原封不动引入调用下就行:https://github.com/lpilp/phpsm2sm3sm4

 类似资料: