跳到主要内容

签名示例(PHP)

<?php
class
{
/**
* 签名及请求示例
* @param $mchId string 聚合平台商户号
* @param $secretKey string 聚合平台密钥
* @param $appid string 微信appid
* @param $order array 支付订单
* @return array
*/
function test_jsapi_get($mchId, $secretKey, $appid ,$order)
{
$notify_url="callback.php";
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$randsi = date("YmdHis") . rand(1000, 9999);

$array=
[
"mch_id"=>$mchId,
"timestamp"=>time(),
"nonce_str"=>$randsi,
"sign_type"=>'MD5',
"content"=>json_encode([
"payment"=>'WXPAY',
"trade_type"=>'JSAPI',
"client_ip"=>$ip,
"out_trade_no"=>$order['orderid'],
"fee"=>$order['money'] * 100,
"body"=>$order['name'],
"open_id"=>,
"device_id"=>'',
"shop_id"=>'',
"notify_url"=>$notify_url,
"app_id"=>$appid
],JSON_UNESCAPED_UNICODE),
];
foreach ($array as $k => $v)
{
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$buff = "";
ksort($Parameters);
foreach ($Parameters as $k => $v) {
$buff .= $k . "=" . $v . "&";
}
$String = '';
if (strlen($buff) > 0) {
$String = substr($buff, 0, strlen($buff) - 1);
}
$String = $String . "&secret_key=" . $secretKey;
//签名步骤三:MD5加密
$String = md5($String);
//签名步骤四:所有字符转为大写
$strings = strtoupper($String);
$array['sign'] = $strings;
$res = json_decode(self::http_post('https://pay.kudianvip.com/openapi/pay/unifiedpay',json_encode($array)), true);

if($res['code'] != 0){
return ReturnError('错误信息为:'.$res['msg']);
}
//返回参数格式设计
if (!empty($res['result']))
{
$res_data=json_decode($res['result'],true);
$pay_info = $res_data['pay_info'];
if (!empty($pay_info)){
//$jsApiParameters=$pay_info;
$jsApiParameters['appId'] = $pay_info['app_id'];
$jsApiParameters['timeStamp'] = $pay_info['timestamp'];
$jsApiParameters['nonceStr'] = $pay_info['nonce_str'];
$jsApiParameters['package'] = $pay_info['package'];
$jsApiParameters['signType'] = $pay_info['sign_type'];
$jsApiParameters['paySign'] = $pay_info['pay_sign'];
return ReturnSuccess('jsapi',$jsApiParameters);
}else{
$this->AddLogAlert('调起支付错误','参数:'.json_encode($array).'|结果:'.$pay_info);
return ReturnError('错误信息为:'.$res['msg']);
}
}else
{
$this->AddLogAlert('调起支付错误','参数:'.json_encode($array).'|结果:'.json_encode($res));
return ReturnError('错误信息为:'.$res['msg']);
}
}

/**
* 使用http_post提交
* @param $url
* @param $params
* @param bool $contentType
* @return bool|mixed|string
*/
protected function http_post($url, $params, $contentType=true)
{

if (function_exists('curl_init')){
// curl方式
$oCurl = curl_init();
if (stripos($url, 'https://') !== FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
$string = $params;
if (is_array($params)){
$aPOST = array();
foreach ($params as $key => $val) {
$aPOST[] = $key . '=' . urlencode($val);
}
$string = join('&', $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, TRUE);
//$contentType json处理
if($contentType){
$headers = array(
"Content-type: application/json;charset='utf-8'",
);

curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $params);
}else{
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $string);
}
$response = curl_exec($oCurl);
curl_close($oCurl);
return $response;
} elseif (function_exists('stream_context_create')) {
// php5.3以上
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($params),
)
);
$_opts = stream_context_get_params(stream_context_get_default());
$context = stream_context_create(array_merge_recursive($_opts['options'], $opts));
return file_get_contents($url, false, $context);
}else{
return FALSE;
}
}
}
?>