PHP唤起微信支付
'****', //公众账号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 格式 $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 ' $appid $body$mch_id $nonce_str $notify_url $out_trade_no $scene_info $spbill_create_ip $total_fee $trade_type $sign 未查询到此订单的付款信息
'; } } //微信回调地址 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; } } ?>
新闻名称:PHP唤起微信支付
网站URL:http://scyanting.com/article/jisjod.html