<?php class wxTransfer { public function pay_transfer($zid) { $apply=M('cashier_balance');//生成模型 $where='id='.$zid; $db_config = loadConfig('db'); $tablepre = $db_config['default']['tablepre']; $sql="select mid,openid,cash from ".$tablepre.'cashier_balance where '. $where; $share=new model(); $data=$share->selectBySql($sql); $mid=$data[0]['mid']; $openid=$data[0]['openid']; $cash=$data[0]['cash']*100; // echo $mid,$openid,$cash; // die; $wx_user = M('cashier_payconfig')->getwxuserConf($mid); $apiclient_cert_url = rawurldecode($wx_user['apiclient_cert']); $apiclient_key_url = rawurldecode($wx_user['apiclient_key']); $rootca_url = rawurldecode($wx_user['rootca']); //var_dump($wx_user);die; $ip = $this -> dangqiangIp(); //echo $ip;die; $api='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //构造参数 $randString=md5(uniqid()); $randOrder='000'.rand(1,1000000); $data=[ 'mch_appid'=>$wx_user['appid'],// 'mchid'=>$wx_user['mchid'],// 'nonce_str'=>$randString,// 'partner_trade_no'=>$randOrder,// 'openid' =>$openid,// 'check_name' => 'NO_CHECK', 'amount'=>$cash,//单位是分 'desc'=>'付款', 'spbill_create_ip'=> $ip,//终端ip ]; //对以上参数计算签名 ksort($data);//以键名排序 $string=''; foreach($data as $k=>$v) { $string.="$k=$v&"; } //把秘钥连接到参数上 $string.='key='.$wx_user['key']; //把链接的字符串全部大写再MD5加密 $sign=strtoupper(md5($string)); //把参数和签名放到一起 $data['sign']=$sign; //参数转成xml $xml='<xml>'; foreach($data as $k=>$v) { $xml.="<$k>$v</$k>"; } $xml.='</xml>'; $ret = $this->postXmlCurl($api,$isHttps = TRUE, $apiclient_cert_url, $apiclient_key_url, $rootca_url, $xml); $xml=simplexml_load_string($ret); if($xml->result_code=='SUCCESS') { file_put_contents('./upload/log/xml.txt',strlen($ret)); // if(strlen($ret)>=530) // { $where='id='.$zid; $db_config = loadConfig('db'); $tablepre = $db_config['default']['tablepre']; $time=time(); $sql="update ".$tablepre.'cashier_balance set pay_state=2,pay_time='.$time . ' where '. $where; $share=new model(); $data=$share->selectBySql($sql); // echo "<script>alert('付款成功');location.href='?m=User&c=balance&a=pay_list'</script>"; return true; // }else // { // // echo "<script>alert('付款失败,请检查错误原因');location.href='?m=User&c=balance&a=apply_list'</script>"; // file_put_contents('./upload/log/xml.txt', $ret); // return false; // } }else { return false; die('error~'); } } public function postXmlCurl($url, $isHttps = TRUE, $apiclient_cert_url, $apiclient_key_url, $rootca_url, $xml) { // 创建curl对象 $ch = curl_init (); // 配置这个对象 curl_setopt ( $ch, CURLOPT_URL, $url); // 请求的URL地址 if($isHttps) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch,CURLOPT_SSLCERT,getcwd()."$apiclient_cert_url"); curl_setopt($ch,CURLOPT_SSLKEY,getcwd()."$apiclient_key_url"); curl_setopt($ch,CURLOPT_CAINFO,"$rootca_url"); // post提交方式 curl_setopt ( $ch, CURLOPT_POST, 1); // 是否是POST请求 curl_setopt ( $ch, CURLOPT_HEADER, 0); // 去掉HTTP协议头 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1); // 返回接口的结果,而不是输出 curl_setopt($ch,CURLOPT_POSTFIELDS, $xml); // 提交的数据 $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; }else{ $error = curl_errno($ch); echo "curlww出错,错误码:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>"; curl_close($ch); return false; } } public function dangqiangIp() { $ip = "Unknown"; if (isset($_SERVER["HTTP_X_REAL_IP"]) && !empty($_SERVER["HTTP_X_REAL_IP"])) { $ip = $_SERVER["HTTP_X_REAL_IP"]; } elseif (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) && !empty($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])) { $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]; } elseif (isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) && !empty($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])) { $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"]; } elseif (isset($HTTP_SERVER_VARS["REMOTE_ADDR"]) && !empty($HTTP_SERVER_VARS["REMOTE_ADDR"])) { $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; } elseif (getenv("HTTP_X_FORWARDED_FOR")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } elseif (getenv("HTTP_CLIENT_IP")) { $ip = getenv("HTTP_CLIENT_IP"); } elseif (getenv("REMOTE_ADDR")) { $ip = getenv("REMOTE_ADDR"); } return $ip; } }