1 /**
2 *@name 微信支付
3 */
4 function wxchatPay($body = '',$order_no = '',$total_fee = '',$spbill_create_ip = '') {
5 require_once 'wx_config.php';//秘钥、appkey
6 // $notify_url = "http://app.yuanmakj.com/api/wxpay/WxPayMain/notify_back.php";//回调函数
7 $notify_url = "http://114.247.209.100:8099/api/wxpay/WxPayMain/notify_back.php";//回调函数
8 $body = $body;
9 $key = '';//key
10 $post_data["appid"] = '';//微信账号ID
11 $post_data["mch_id"] = '';//商户号
12 $post_data["nonce_str"] = createStr();//随机字符串
13 $post_data["body"] = '112';//商品描述
14 $post_data["out_trade_no"] = '';//商户订单号
15 $post_data["total_fee"] = '1';//总金额 金额不能带小数点
16 $post_data["spbill_create_ip"] = '14.23.150.211';//终端IP
17 $post_data["notify_url"] = $notify_url;//通知地址
18 $post_data["trade_type"] = 'APP';//交易类型
19 $post_data["sign"] = getSign($post_data,$key);//签名
20 //使用方法
21 file_put_contents('aa4.txt',var_export($post_data,TRUE),FILE_APPEND);
22 $xml_data = arrayXml($post_data);
23 file_put_contents('aa3.txt',var_export($xml_data,TRUE),FILE_APPEND);
24 $wx_xml_date = send_post('https://api.mch.weixin.qq.com/pay/unifiedorder', $xml_data);
25 //发送post请求结束
26 file_put_contents('aa1.txt',var_export($wx_xml_date,TRUE),FILE_APPEND);
27 $wx_str_date = xmlToArray($wx_xml_date);
28 file_put_contents('aa2.txt',var_export($wx_str_date,TRUE),FILE_APPEND);
29 $wx_str_date['timestamp'] = time().'';//时间戳
30 $wx_str_date['packagevalue'] = 'Sign=WXPay';//扩展字段,暂时写死(Sign=WXPay)
31 $res['appid'] = $wx_str_date['appid'].'';
32 $res['partnerid'] = $wx_str_date['mch_id'].'';
33 $res['prepayid'] = $wx_str_date['prepay_id'].'';
34 $res['package'] = $wx_str_date['packagevalue'].'';
35 $res['noncestr'] = $wx_str_date['nonce_str'].'';
36 $res['timestamp'] = $wx_str_date['timestamp'].'';
37 $wx_str_date['sign'] = getSign($res,$key);
38 $wx_str_date['sign'] = $wx_str_date['sign'].'';
39 return $wx_str_date;
40 }
41 function getSign($Obj,$key)
42 {
43 foreach ($Obj as $k => $v)
44 {
45 $Parameters[$k] = $v;
46 }
47 //签名步骤一:按字典序排序参数
48 ksort($Parameters);
49 $String = formatBizQueryParaMap($Parameters, false);
50 //echo '【string1】'.$String.'</br>';
51 //签名步骤二:在string后加入KEY
52 if(!empty($key)){
53 $String = $String."&key=".$key;
54 }
55 //echo "【string2】".$String."</br>";
56 //签名步骤三:MD5加密
57 $String = md5($String);
58 //echo "【string3】 ".$String."</br>";
59 //签名步骤四:所有字符转为大写
60 $result_ = strtoupper($String);
61 //echo "【result】 ".$result_."</br>";
62 return $result_;
63 }
64 /**
65 * 作用:格式化参数,签名过程需要使用
66 */
67 function formatBizQueryParaMap($paraMap, $urlencode)
68 {
69 $buff = "";
70 ksort($paraMap);
71 foreach ($paraMap as $k => $v)
72 {
73 if($urlencode)
74 {
75 $v = urlencode($v);
76 }
77 //$buff .= strtolower($k) . "=" . $v . "&";
78 $buff .= $k . "=" . $v . "&";
79 }
80 $reqPar;
81 if (strlen($buff) > 0)
82 {
83 $reqPar = substr($buff, 0, strlen($buff)-1);
84 }
85 return $reqPar;
86 }
87 /**
88 * 作用:array转xml
89 */
90 function arrayXml($arr)
91 {
92 $xml = "<xml>";
93 foreach ($arr as $key=>$val)
94 {
95 if (is_numeric($val))
96 {
97 $xml.="<".$key.">".$val."</".$key.">";
98 }
99 else
100 $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
101 }
102 $xml.="</xml>";
103 return $xml;
104 }
105
106 /**
107 * *获取随机字符串
108 * @param integer $length [description]
109 * @return [type] [description]
110 */
111 function createStr($length = 32){
112 $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
113 $str = '';
114 $max = strlen($chars)-1;
115 for($i=0;$i<$length;$i++){
116 $str .=$chars[mt_rand(0,$max)];
117 }
118 return $str;
119 }
120 /**
121 * 发送post请求
122 * @param string $url 请求地址
123 * @param array $post_data post键值对数据
124 * @return string
125 */
126 function send_post($url, $post_data) {
127 $options = array(
128 'http' => array(
129 'method' => 'POST',
130 'header' => 'Content-type:application/x-www-form-urlencoded',
131 'content' => "$post_data",
132 'timeout' => 15 * 60 // 超时时间(单位:s)
133 )
134 );
135 $context = stream_context_create($options);
136 $result = file_get_contents($url, false, $context);
137 return $result;
138 }
139 /**
140 * 作用:将xml转为array
141 */
142 function xmlToArray($xml)
143 {
144 //将XML转为array
145 $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
146 // $array_data = json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
147 return $array_data;
148 }