温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP 唤起微信支付

发布时间:2020-06-25 14:59:26 来源:网络 阅读:25219 作者:pansw113 栏目:web开发
<?php
/**
**/
class H5wxAction extends CommonAction {
	public function __construct() {
		parent::__construct();
		
	
	}
	/*
	字段名	变量名	必填	类型	示例值	描述
公众账号ID	appid	是	String(32)	wxd678efh667hg6787	微信分配的公众账号ID(企业号corpid即为此appId)
商户号	mch_id	是	String(32)	1230000109	微信支付分配的商户号
设备号	device_info	否	String(32)	013467007045764	终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"
随机字符串	nonce_str	是	String(32)	5K8264ILTKCH16CQ2502SI8ZNMTM67VS	随机字符串,不长于32位。推荐随机数生成算法
签名	sign	是	String(32)	C380BEC2BFD727A4B6845133519F3AD6	签名,详见签名生成算法
签名类型	sign_type	否	String(32)	HMAC-SHA256	签名类型,目前支持HMAC-SHA256和MD5,默认为MD5
商品描述	body	是	String(128)	腾讯充值中心-QQ会员充值	
商品简单描述,该字段须严格按照规范传递,具体请见参数规定
商品详情	detail	否	String(6000)		单品优惠字段(暂未上线)
附加数据	attach	否	String(127)	深圳分店	附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
商户订单号	out_trade_no	是	String(32)	20150806125346	商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
货币类型	fee_type	否	String(16)	CNY	符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
总金额	total_fee	是	Int	888	订单总金额,单位为分,详见支付金额
终端IP	spbill_create_ip	是	String(16)	123.12.12.123	必须传正确的用户端IP,详见获取用户ip指引
交易起始时间	time_start	否	String(14)	20091225091010	订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
交易结束时间	time_expire	否	String(14)	20091227091010	
订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则
注意:最短失效时间间隔必须大于5分钟
商品标记	goods_tag	否	String(32)	WXG	商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠
通知地址	notify_url	是	String(256)	http://www.weixin.qq.com/wxpay/pay.php	接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
交易类型	trade_type	是	String(16)	MWEB	H5支付的交易类型为MWEB
商品ID	product_id	否	String(32)	12235413214070356458058	trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
指定支付方式	limit_pay	否	String(32)	no_credit	no_credit--指定不能使用信用卡支付
用户标识	openid	否	String(128)	oUpF8uMuAJO_M2pxb1Q9zNjWeS6o	trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。openid如何获取,可参考【获取openid】。企业号请使用【企业号OAuth3.0接口】获取企业号内成员userid,再调用【企业号userid转openid接口】进行转换
场景信息	scene_info	是	String(256)	//IOS移动应用
{"h6_info": {"type":"IOS","app_name": "王者荣耀","bundle_id": "com.tencent.wzryIOS"}}

//安卓移动应用
{"h6_info": {"type":"Android","app_name": "王者荣耀","package_name": "com.tencent.tmgp.sgame"}}

//WAP网站应用
{"h6_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}	该字段用于上报支付的场景信息,针对H5支付有以下三种场景,请根据对应场景上报,H5支付不建议在APP端使用,针对场景1,2请接入APP支付,不然可能会出现兼容性问题

1,IOS移动应用
{"h6_info": //h6支付固定传"h6_info" 
    {"type": "",  //场景类型
     "app_name": "",  //应用名
     "bundle_id": ""  //bundle_id
     }
}

2,安卓移动应用
{"h6_info": //h6支付固定传"h6_info" 
    {"type": "",  //场景类型
     "app_name": "",  //应用名
     "package_name": ""  //包名
     }
}

3,WAP网站应用
{"h6_info": //h6支付固定传"h6_info" 
   {"type": "",  //场景类型
    "wap_url": "",//WAP网站URL地址
    "wap_name": ""  //WAP 网站名
    }
}

	
	*/
	public function config(){
		$array=array(
			'appid'=>'****', //公众账号ID	appid	是	String(32)	wxd678efh667hg6787	微信分配的公众账号ID(企业号corpid即为此appId)
			'mch_id'=>'****',// 商户号	mch_id	是	String(32)	1230000109	微信支付分配的商户号
			'nonce_str'=>rand(100000,999999),// 随机字符串	nonce_str	是	String(32)	5K8264ILTKCH16CQ2502SI8ZNMTM67VS	随机字符串,不长于32位。推荐随机数生成算法
			'key'=>'****',
			'APPSECRET'=>'*****',
		
		);
		return $array;
	
	}
	
	public function H5WeiXin(){
	
		$config = $this->config();
		
		
		$count=1;
		//$money= 1;//充值金额   
         
       
       $userip = $this->getIP(); //获得用户设备IP
       $appid = $config['appid']; 
       $mch_id = $config['mch_id']; 
       $key = $config['key']; 
       
       $rand = rand(00000,99999);  
       $out_trade_no = $_POST['number'];//订单号  
       $nonce_str=MD5($rand);//随机字符串  
       $body = $data['title'];//内容  
       $total_fee = $count; //金额  
       $spbill_create_ip = $userip; //IP  
       $notify_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/WxHD'; //回调地址  
       $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/is_wxpay?number='.$_POST['number']; //回调地址  
       $trade_type = 'MWEB';//交易类型
       $scene_info ='{"h6_info":{"type":"Wap","wap_url":"'.$_SERVER['HTTP_HOST'].'","wap_name":"'.$data['title'].'"}}';//场景信息 
       $signA ="appid=$appid&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)); // MD5 后转换成大写  
       $post_data = "<xml>  
                       <appid>$appid</appid>  
                       <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>  
                       <scene_info>$scene_info</scene_info>  
                       <spbill_create_ip>$spbill_create_ip</spbill_create_ip>  
                       <total_fee>$total_fee</total_fee>  
                       <trade_type>$trade_type</trade_type>  
                       <sign>$sign</sign>  
                   </xml>";//拼接成XML 格式  
       $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
       $dataxml = $this->http_post($url,$post_data);
       $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); 
		
		
		if($objectxml['return_code']=='SUCCESS'){
			$json['url'] = $objectxml['mweb_url'].'&redirect_url='.$redirect_url; //唤起微信链接
			$json['msg'] = '1';
		
		}else{
			$json['info'] ='参数错误';
			$json['msg'] ='0';
		}
	
		echo json_encode($json);
	
	
	
	}
	//查询是否支付成功  示例 根据需要调整
	public function is_wxpay(){
		$out_trade_no = $_GET['number'];
		
		$type = substr($out_trade_no,0,1);	
		if($type=='B'){
			$is_state = M('Bond')->where(array('number'=>$out_trade_no))->find();
			$url = U('Court/details',array('id'=>$is_state['aid']));
		}elseif($type=='W'){
			$is_state = M('Wallet')->where(array('number'=>$out_trade_no))->find();
			$url = U('Member/wallet');
		}else{
			$is_state =M('viplog')->where(array('number'=>$out_trade_no))->find();
			$url = U('Member/vip');
		}
		
		if($is_state['state']=='1'){
			cookie('id',$is_state['mid']);
			header("location: ".$url."");
		}else{
			echo '<h2>未查询到此订单的付款信息</h2>';
		}
		
	
	
	}
	
	//微信回调地址
	public function WxHD(){
			$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
			$array = $this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA']);
			M('api_log')->add(array('content'=>json_encode($this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA'])).$array['out_trade_no']));
				
			
			
			if($array['return_code']=='SUCCESS'){
				
				$out_trade_no=$array['out_trade_no'];
				//$number = explode('-',$out_trade_no);
				$type = substr($out_trade_no,0,1);	
				
				header("location: http://".$_SERVER['HTTP_HOST'].$url."");
						
						
						
				
			}
		
		
		
			
		
	}
public function xmlToArray($xml){ 
 
 //禁止引用外部xml实体 
 
libxml_disable_entity_loader(true); 
 
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
 
$val = json_decode(json_encode($xmlstring),true); 
 
return $val; 
 
} 
public function convertUrlQuery($query)
{
  $queryParts = explode('&', $query);
  $params = array();
  foreach ($queryParts as $param) {
    $item = explode('=', $param);
    $params[$item[0]] = $item[1];
  }
  return implode('&',$params);
}
	
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 getIP() { 
		if(getenv('HTTP_CLIENT_IP')) { 
			$ip = getenv('HTTP_CLIENT_IP'); 
		}elseif (getenv('HTTP_X_FORWARDED_FOR')) { 
			$ip = getenv('HTTP_X_FORWARDED_FOR'); 
		}elseif (getenv('HTTP_X_FORWARDED')) { 
			$ip = getenv('HTTP_X_FORWARDED'); 
		}elseif (getenv('HTTP_FORWARDED_FOR')) { 
		$ip = getenv('HTTP_FORWARDED_FOR'); 

		}elseif (getenv('HTTP_FORWARDED')) { 
			$ip = getenv('HTTP_FORWARDED'); 
		}else { 
			$ip = $_SERVER['REMOTE_ADDR']; 
		} 
		return $ip; 
	} 
	
	
	

}

 
?>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI