//--------------------------- 签名流程 start ------------------------------
//密钥
$key = '95bc9d3ffbd532e2549a99dab113b1c4';
//要提交的数据
$data = array(
'time' => time(), //时间戳
'mch_id' => 1689, //商户id
'ptype' => 26, //类型
'order_sn' => 'kn_65fa5d4f65asdf8as6d5f46a3', //订单编号
'money' => 656.00, //价格
'goods_desc' => '描述', //商品描述
'client_ip' => '156.125.55.69', //客户ip
'format' => 'json', //格式
'notify_url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/?c=Pay&a=notify_url' //回调地址
);
//生成签名
$sign = signMd5($data, $key);
//把签名加入要提交的数组中
$data['sign'] = $sign;
echo $sign;
//组装完成开始提交数据
$result = curl_post($url, $data);
//其他业务逻辑 。。。。。
//--------------------------- 签名流程 end ------------------------------
//--------------------------- 验证签名流程 start ------------------------------
//密钥
$key = '95bc9d3ffbd532e2549a99dab113b1c4';
//接收到的数据
$data = array(
'time' => time(), //时间戳
'mch_id' => 1689, //商户id
'ptype' => 26, //类型
'order_sn' => 'kn_65fa5d4f65asdf8as6d5f46a3', //订单编号
'money' => 656.00, //价格
'goods_desc' => '描述', //商品描述
'client_ip' => '156.125.55.69', //客户ip
'format' => 'json', //格式
'notify_url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/?c=Pay&a=notify_url', //回调地址
'sign' => '5ed8a5292ad81dbc59a5e895fa61a871'
);
//接收到的签名
$yzsign = $data['sign'];
//删除签名
unset($data['sign']);
//把接收到的数据重新生成签名
$sign = signMd5($data, $key);
//判断签名是否相等
if ($sign == $yzsign) {
//签名通过,执行业务逻辑
}
//--------------------------- 验证签名流程 end ------------------------------
//数据发送,回调方法
function curl_post($url, $param = array(), $wait = 30)
{
if (!is_array($param)) {
return false;
}
$postdata = http_build_query(
$param
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $wait,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
),
));
if (strpos($url, "https") === 0) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$err = curl_error($curl);
curl_close($curl);
$result = array(
'response_code' => $httpCode,
'output' => $response,
);
return $result;
}
//签名方法
function signMd5($pdata, $mch_key)
{
ksort($pdata);
$sign_str = '';
foreach ($pdata as $pk => $pv) {
$sign_str .= "{$pk}={$pv}&";
}
$sign_str .= "key={$mch_key}";
$sign = md5($sign_str);
return $sign;
}
发表评论