微信H5支付開發全過程(除內置微信以外的瀏覽器)
阿新 • • 發佈:2017-09-04
app 獲取ip getenv 過程 comm ati psi 初始 cli
前言:網上都是~ 呵呵 自己搞 只要花時間 多問客服總會有的
只說程序不說準備工作 自己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; }
微信H5支付開發全過程(除內置微信以外的瀏覽器)