微信H5支付统一下单和查询

作者:谢高升 发布:2018-08-10 浏览:3098次

微信H5统一下单和查询接口;

//需要注意的是 notify_url 在laravel中是要以.php存在的不能是 pay.xiegaosheng.com/notify

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class H5payController  extends Controller
{

	public function pay(Request $request)
	{
		$appid = $request->input('appid');
		$mch_id = $request->input('mch_id');
		$key = $request->input('key');
		$callback = $request->input('callback');
		$request->session()->put('callback',$callback);
		$ip = $request->input('ip');
		$notify_url = 'http://pay.xiegaosheng.com/notify.php';
		$total_fee = $request->input('total_fee');
		$mac = $request->input('mac');
		$place_id = $request->place_id;
		//$ip = $this->get_client_ip();
		$attach = '支付测试';
		$body = 'H5支付测试';
		$nonce_str = $this->getStr(18);
		$out_trade_no = $request->input('order_no');
		//$out_trade_no = date('YmdHis').$this->getStr(16);
		$trade_type = 'MWEB';
		//$key = '1us2j491FJ3bg8HFs90KsejxT6Fq8Jk5';
		 $scene_info ='{"h5_info": {"type":"IOS","app_name": "H5测试","package_name": "com.tencent.tmgp.sgame"}}'; //场景信息
		 $signA = "appid=$appid&attach=$attach&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$ip&total_fee=$total_fee&trade_type=MWEB";
		$strSignTmp = $signA."&key=$key"; //拼接字符串
		$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
$xml = <<<xml
	<xml>
		<appid>$appid</appid>
		<attach>{$attach}</attach>
		<body>{$body}</body>
		<mch_id>$mch_id</mch_id>
		<nonce_str>$nonce_str</nonce_str>
		<notify_url>$notify_url</notify_url>
		<out_trade_no>$out_trade_no</out_trade_no>
		<spbill_create_ip>$ip</spbill_create_ip>
		<total_fee>$total_fee</total_fee>
		<trade_type>$trade_type</trade_type>
		<scene_info>$scene_info</scene_info>
		<sign>$sign</sign>
	</xml>
xml;
//	echo $xml;exit;
		 $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	     $data = $this->http_post($url,$xml);
	     $objectxml = (array)simplexml_load_string($data,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组
	    
		$urlencode = urlencode("http://pay.xiegaosheng.com/verify?mac=$mac&order_no=$out_trade_no&&place_id=$place_id&callback=$callback");
		if ($objectxml['return_code']=='SUCCESS') {
			# code...
			$objectxml['mweb_url'] .= '&redirect_url='.$urlencode; 
		}else{
			$objectxml['mweb_url']='';
		}
		//dd($objectxml);
		return $objectxml;
     	//return view('pay',['url'=>$objectxml]);
	}

	

	public function http_post($url, $data) {
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL,$url);
	    curl_setopt($ch, CURLOPT_HEADER,0);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    $res = curl_exec($ch);
	    curl_close($ch);
	    return $res;
	}

//验证订单号是否存在
	public function verify(Request $request)
	{
		
		$token = md5('wifi'.date('Y-m-d H'));
		$place_id = $request->place_id;
		$url = "http://pay.xiegaosheng.com/getplace?id=$place_id&token=$token";
		//echo $url;exit;
		$json = $this->http_get($url);

		if ($json !='fail') {
			# code...
			$param = json_decode($json,true);
			$appid = $param['appid'];
			$mch_id = $param['mch_id'];
			$key = $param['key'];
		}else{
			die('参数错误');
		}
		$nonce_str = $this->getStr(18);
		$out_trade_no = $request->order_no;
		$signA = "appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&out_trade_no=$out_trade_no";
		
		$strSignTmp = $signA."&key=$key"; //拼接字符串
		$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
		$xml = <<<xml
		<xml>
		   <appid>$appid</appid>
		   <mch_id>$mch_id</mch_id>
		   <nonce_str>$nonce_str</nonce_str>
		   <out_trade_no>$out_trade_no</out_trade_no>
		   <sign>$sign</sign>
		</xml>
xml;
//查询订单
		$url='https://api.mch.weixin.qq.com/pay/orderquery';
	   	$data = $this->http_post($url,$xml);
		$objectxml = (array)simplexml_load_string($data,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组
		$callback = $request->input('callback');
		//echo $callback;exit;
		if ($objectxml['return_code']=='SUCCESS' && $objectxml['result_code']=='SUCCESS' && $objectxml['trade_state']=='SUCCESS') {
			$callback .="?order_no=$out_trade_no&status=1&type=2";
			 header("Location: $callback");exit;
		}else{
			$callback .="?order_no=$out_trade_no&status=2&type=2";
			header("Location: $callback");exit;
		}

	}
        //随机字符串
	public function getStr($length)
	{  
		$key= '';
  		$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';  
	    for($i=0;$i<$length;$i++)   
	    {   
	        $key .= $pattern{mt_rand(0,35)};    //生成php随机数   
	    }   
	    return $key;   
	}
	//发送http请求
    public function http_get($url, $data='') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
    //获取ip地址
    public function get_client_ip()
    {
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    }
}


标签: H5支付