<?php
/**
*工具函数
*使用curl来post一个json数据
*CURLOPT_SSL_VERIFYPEER,CURLOPT_SSL_VERIFYHOST - 在做https中要用到
*CURLOPT_RETURNTRANSFER - 不以文件流返回,带1
*/
function JsonPost($url, $jsonData) {
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, $url );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $jsonData );
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec ( $curl );
curl_close ( $curl );
return $result;
}
function tuikuan_hb_MakeSign($args,$key)
{
//签名步骤一:按字典序排序参数
ksort($args);
$string = tuikuan_hb_ToUrlParams($args);
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$key;
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 格式化参数格式化成url参数
*/
function tuikuan_hb_ToUrlParams($args)
{
$buff = "";
foreach ($args as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/*随机字符串*/
function get_rand_char($length = 4) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$strLength = 61;
$res="";
for($i = 0; $i < $length; $i ++) {
$res .= $str [rand ( 0, $strLength )];
}
return $res;
}
/*
* 下单接口
* */
$a = $_POST['amount'];
$channelId = $_POST['channelId'];
$appId="appid";
$key = '密钥';
$orderNo = 'xt'.date('YmdHid') . rand(9999,10000);
$outTradeNo = get_rand_char(32);
$amount = sprintf("%.2f", $a);
$params["appId"]= $appId;//商户号
$params["outTradeNo"]=$outTradeNo;//商户订单号
$params["channelId"]=$channelId;//通道编号
$params["amount"]=$amount;//金额
$params["callbackUrl"]="";//服务端通知
$params["successUrl"]="";//支付成功跳转
$transStr =tuikuan_hb_MakeSign($params,$key) ;
$params["sign"]=$transStr;//签名
$params["orderNo"]=$orderNo;//系统订单号
$url = '网关地址';
$re=JsonPost($url, $params);
echo $re;exit();
?>