前言:网上都是~ 呵呵 自己搞 只要花时间 多问客服总会有的
只说程序不说准备工作 自己ID 或者秘钥都准备好了 写的有点儿乱 可以把所有的方法 放在一个文件中调用
public function wechat ($doorder,$doczmoney,$doczbody) { // H5 微信支付 $wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //下单接口 $appid = ""; //公众账号ID $mch_id ="";// 商户号 $nonce_str =$this->randStrpay(); //随机字符串 $key =""; $body = $doczbody; //商品描述 $out_trade_no = $doorder;// 商户订单号 $total_fee = $doczmoney*100;// 我数据库存的是元 转换下 标价金额 f分 $device_info = ‘‘; $spbill_create_ip = $this->getClientp();// 终端IP $notify_url ="";// 通知地址 $trade_type ="MWEB";// 交易类型 //组合数组 方便字典升序 $paraMap = array(‘appid‘=>$appid, ‘mch_id‘=>$mch_id, ‘nonce_str‘=>$nonce_str, ‘body‘=>$body, ‘out_trade_no‘=>$out_trade_no, ‘total_fee‘=>$total_fee, ‘spbill_create_ip‘=>$spbill_create_ip, ‘notify_url‘=>$notify_url, ‘trade_type‘=>$trade_type, ‘device_info‘=>$device_info ); // 字典升序 下边有方法 这是摘出来的 自己复制到自己的位置 然后调用 下边几个调用都一样 $singStr = \Ruifi\Service\CommonService::formatParaMap($paraMap); //获取singvalue值 $sign =$this->autostr($singStr,$key); // sign 值添加到数组 $mapSing = array_merge($paraMap,array(‘sign‘=>$sign)); //print_r($mapSing);die; //数组转xml $arrXml = \Ruifi\Service\CommonService::arrayToXml($mapSing); //post 发送 $wechatreslut = \Ruifi\Service\CommonService::postXmlCurl($arrXml,$wxUrl); //接受xml $xmlToarr = \Ruifi\Service\CommonService::xmlToArray($wechatreslut); $xmlToarrStr = \Ruifi\Service\CommonService::utf8_to_gbk_arr($xmlToarr); if ($xmlToarrStr[‘result_code‘] ==‘SUCCESS‘){ $wxMurl = $xmlToarrStr[‘mweb_url‘]; header("Location: $wxMurl"); } } //32位随机字符串 function randStrpay($length=32) { $rand=‘‘; $randstr= ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘; $max = strlen($randstr)-1; mt_srand((double)microtime()*1000000); for($i=0;$i<$length;$i++) { $rand.=$randstr[mt_rand(0,$max)]; } return $rand; } // 签名算法 function autostr($singStr,$key){ $stringSignTemp = $singStr."&key=$key"; $sign = md5($stringSignTemp); $signValue = strtoupper($sign); return $signValue; } //获取IP function getClientp(){ $cip ="unknow"; if($_SERVER[‘REMOTE_ADDR‘]){ $cip = $_SERVER[‘REMOTE_ADDR‘]; }elseif(getenv("REMOTE_ADDR")){ $cip =getenv("REMOTE_ADDR"); } return $cip; }
这些方法是 上边调用的 你自己随意 /* 字典升序*/ function formatParaMap($paraMap) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { $buff .= $k . "=" . $v . "&"; } $reqPar = ‘‘; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } /* 数组转xml */ function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)) { $xml.="<".$key.">".$val."</".$key.">"; } else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } public function postXmlCurl($xml,$url,$second=30) { //初始化curl $ch = curl_init(); curl_setopt($ch, CURLOP_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); //运行curl $data = curl_exec($ch); curl_close($ch); //返回结果 if($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出错,错误码:$error"."<br>"; curl_close($ch); return false; } } /* xml 转数组 */ public function xmlToArray($xml) { //将XML转为array $array_data = json_decode(json_encode(simplexml_load_string($xml, ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true); return $array_data; }