<?php
header("Content-Type: text/html;charset=utf-8");
if($_POST['wechatm_two'] = 'wechatm')
{
$order['order_sn'] = $_POST['order_sn'];
$order['order_amount'] = $_POST['order_amount'];
$cart_goods[0]['goods_name'] = $_POST['goods_name'];
$cart_goods[0]['goods_id'] = $_POST['goods_id'];
}
$appid = "";
$mch_id = "";
$key = "";
$out_trade_no = $order['order_sn'];
$nonce_str = createNoncestr();
$body = iconv('gbk','utf-8',$cart_goods[0]['goods_name']);
$total_fee = (int)($order['order_amount'] * 100);
$spbill_create_ip = get_client_ip();
$notify_url = "";
$trade_type = 'MWEB';
$str = iconv('gbk','utf-8','至尊');
$scene_info = '{"h5_info":{"type": "Wap","wap_url": "www.baidu.com","wap_name": '.$str.'}}';
$signA ="appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
$strSignTmp = $signA."&key=$key";
$sign = strtoupper(MD5($strSignTmp));
$post_data = "<xml>
<appid>$appid</appid>
<mch_id>$mch_id</mch_id>
<body>$body</body>
<out_trade_no>$out_trade_no</out_trade_no>
<total_fee>$total_fee</total_fee>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<notify_url>$notify_url</notify_url>
<trade_type>$trade_type</trade_type>
<scene_info>$scene_info</scene_info>
<attach>$out_trade_no</attach>
<nonce_str>$nonce_str</nonce_str>
<sign>$sign</sign>
</xml>";
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$dataxml = postXmlCurl($post_data,$url);
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_url = urlencode('/?act=order_detail&order_id='.$cart_goods[0]['goods_id']);
$obj_url = $objectxml['mweb_url'].'&redirect_url='.$return_url;
echo "<script>location.href = '{$obj_url}'</script>";
function createNoncestr( $length = 32 ){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
function postXmlCurl($xml,$url,$second = 30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}else{
$error = curl_errno($ch);
curl_close($ch);
echo "curl出错,错误码:$error"."<br>";
}
}
function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
$ip = 'unknown';
if ($ip !== 'unknown') return $ip[$type];
if($_SERVER['HTTP_X_REAL_IP']){
$ip=$_SERVER['HTTP_X_REAL_IP'];
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}