配置文件
function config(){
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 支付接口
$key = ''; //key为商户平台设置的密钥key
$appid = ''; //appid
$body = '';// 商品描述
$mch_id = ''; // 商户号
$nonce_str = randomkeys(32); // 随机字符串
$notify_url = ''; // 异步通知地址
$sign_type = 'MD5';// 签名类型
$out_trade_no = time();// 商户订单号
$total_fee = '1'; // 支付金额 以分为单位
$spbill_create_ip = getip(); // 终端ip
$sign_arr = [
"appid"=>$appid,
"body"=>$body,
"mch_id"=>$mch_id,
"nonce_str"=>$nonce_str,
"notify_url"=>$notify_url,
"sign_type"=>$sign_type,
"out_trade_no"=>$out_trade_no,
"total_fee"=>$total_fee,
"spbill_create_ip"=>$spbill_create_ip,
];
return ["sign_arr"=>$sign_arr,"key"=>$key,"url"=>$url];
}
微信支付 JSAPI
function test_pay(){
$openid = '';// 用户的openid
$config = config(); // 公共的配置文件
$sign_arr = $config['sign_arr'];
$sign_arr['trade_type'] = 'JSAPI';
$sign_arr['openid'] = $openid;
$key = $config['key'];
$url =$config['url'];
// 根据键值对数组进行排序
ksort($sign_arr,SORT_STRING );
$stringA = ToUrlParams($sign_arr);
$stringSignTemp = $stringA."&key=".$key;
$sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式 签名
$sign_arr['sign'] = $sign;
$res = wxpost($url,arrayToXml($sign_arr));
$pay_arr = xmlToArray($res); //这里是数组
$nonceStr = $sign_arr['nonce_str'];
$package = 'prepay_id='. $pay_arr['prepay_id'];
$timeStamp = (string)time();
$signType = 'MD5';
$json_data = [
"appId"=>$sign_arr['appid'], // appid
"nonceStr"=>$nonceStr,// 随机串
"package"=>$package,//支付id
"signType"=>$signType,//微信签名方式
"timeStamp"=>$timeStamp,// 时间戳
];
// 签名
$stringB = ToUrlParams($json_data);
$sign= strtoupper(md5($stringB."&key=$key")); //注:MD5签名方式 签名
$json_data['paySign'] = $sign;
$data = $json_data;
$data['jsApiParameters'] = json_encode($json_data);
echo json_encode($data);
}
微信外部支付 h5支付
function test_pay_h5(){
$config = config(); // 公共的配置文件
$scene_info = '{"h5_info": {"type":"Wap","wap_url": "网站域名","wap_name": "支付"}}';// 场景信息
$sign_arr = $config['sign_arr'];
$sign_arr['trade_type'] = 'MWEB';
$sign_arr['scene_info'] = $scene_info; //
$key = $config['key'];
$url =$config['url'];
ksort($sign_arr,SORT_STRING );
$stringA = ToUrlParams($sign_arr);
$stringA = $stringA."&key=$key";
$sign = strtoupper(md5($stringA));
$sign_arr['sign'] = $sign;
// 参数组装完之后 转化为xml数据
$post_data = arrayToXml($sign_arr);
// 转化完之后 进行请求接口
$rest = wxpost($url,$post_data);
$array = xmlToArray($rest);
$mweb_url = $array['mweb_url']."&redirect_url=".urlencode("想要跳转的地址");
header("location:".$mweb_url);
}
扫码支付
function test_pay_QRcode(){
require_once "./phpqrcode.php";
$config = config(); // 公共的配置文件
$sign_arr = $config['sign_arr'];
$sign_arr['trade_type'] = 'NATIVE';
$key = $config['key'];
$url =$config['url'];
// 根据键值对数组进行排序
ksort($sign_arr,SORT_STRING );
$stringA = ToUrlParams($sign_arr);
$stringSignTemp = $stringA."&key=".$key;
$sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式 签名
$sign_arr['sign'] = $sign;
$res = wxpost($url,arrayToXml($sign_arr));
$pay_arr = xmlToArray($res); //这里是数组
// 在输出图片之前 不能有任何的内容输出
QRcode::png($pay_arr['code_url']);
}
//异步回调地址
function test_index(){
$rst = file_get_contents("php://input") ;
$msg = (array)simplexml_load_string($rst, 'SimpleXMLElement', LIBXML_NOCDATA);
$data = json_encode($msg);
}
// xml 转为数组
function xmlToArray($xml,$type='')
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
//simplexml_load_string()解析读取xml数据,然后转成json格式
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($type == "json") {
$json = json_encode($xmlstring);
return $json;
}
$arr = json_decode(json_encode($xmlstring), true);
return $arr;
}
// curl请求
function wxpost($url,$post)
{
//初始化
$curl = curl_init();
$header[] = "Content-type: text/xml";//定义content-type为xml
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
// curl_setopt($curl, CURLOPT_HEADER, 1);
//定义请求类型
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
$post_data = $post;
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//执行命令
$data = curl_exec($curl);
//关闭URL请求
//显示获得的数据
if ($data)
{
curl_close($curl);
return $data;
}else{
$res = curl_error($curl);
curl_close($curl);
return $res;
}
}
// array 转为xml
function arrayToXml($arr){
$xml = "<xml>";
foreach ($arr as $key=>$val){
if(is_array($val)){
$xml.="<".$key.">".arrayToXml($val)."</".$key.">";
}else{
$xml.="<".$key.">".$val."</".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
// 拼装成url参数形式
function ToUrlParams($data)
{
ksort($data,SORT_STRING);
$buff = "";
foreach ($data as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
生成随机字符串
function randomkeys($length)
{
$key = '';
$pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
for($i=0;$i<$length;$i++)
{
$key .= $pattern{mt_rand(0,35)}; //生成php随机数
}
return $key;
}
获得客户端的ip
function getip() {
$ip = '';
if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" )) {
$ip = getenv ( "HTTP_CLIENT_IP" );
} else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) {
$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
} else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) {
$ip = getenv ( "REMOTE_ADDR" );
} else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) {
$ip = $_SERVER ['REMOTE_ADDR'];
} else {
$ip = "unknown";
}
return ($ip);
}