最近由于業(yè)務(wù)所需,對(duì)接了微信H5支付,然而微信支付對(duì)這塊并沒有現(xiàn)成的demo可用,所以就必須自己老老實(shí)實(shí)對(duì)照開發(fā)文檔去寫咯!但這對(duì)于剛接觸的童鞋來說,坑多多少少還是有的,所以尋思著把自己的經(jīng)驗(yàn)分享出來,畢竟現(xiàn)成的用的還是多巴適的嘛!
好了,官方文檔的那一套就不多說了,詳情見官方文檔。
在這里,我主要分成了三個(gè)文件:WxPay.Config.php(支付配置文件)、Weixin.class.php(支付類)以及PayMentController.class.php(支付文件)。
首先,WxPay.Config.php配置文件主要包含了商戶appId、商戶號(hào)、商家key、異步回調(diào)URL、支付場(chǎng)景信息,如下:
class WxPayConfig { public static $appid = '微信支付的公眾號(hào)appid'; public static $mchid = '微信支付分配的商戶號(hào)'; public static $key = '微信商戶自己設(shè)置的安全key'; public static $notify_url = '商戶側(cè)接收微信支付異步通知的URL'; public static $scene_info = '{"h5_info":{"type":"Wap","wap_url":" 發(fā)起微信H5支付H5的URL","wap_name":"支付"}}'; }
然后,封裝Weixin.class.php支付類,主要調(diào)用統(tǒng)一下單Api,這里不多說了,直接上代碼:
?php require_once "lib/WxPay.Config.php"; class Weixin { /** * 微信H5下單付款 * @order 付款信息 * @bodys 付款內(nèi)容 * */ function getCode($order,$bodys){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信傳參地址 //1.獲取調(diào)用統(tǒng)一下單接口所需必備參數(shù) $appid =WxPayConfig::$appid;//微信公眾號(hào)appid $mch_id = WxPayConfig::$mchid;//微信支付商戶號(hào) $key = WxPayConfig::$key;//自己設(shè)置的微信商家key $out_trade_no = $order['order_sn'];//平臺(tái)內(nèi)部訂單號(hào) $nonce_str=MD5($out_trade_no);//隨機(jī)字符串 $body = $bodys;//付款內(nèi)容 $total_fee = $order['order_amount']*100;//付款金額,單位為分 $spbill_create_ip = getIP(); //獲得用戶設(shè)備IP $attach = 'weixinh5';//附加數(shù)據(jù)(自定義,在支付通知中原樣返回) $notify_url = WxPayConfig::$notify_url; //異步回調(diào)地址,需外網(wǎng)可以直接訪問 $trade_type = 'MWEB';//交易類型,微信H5支付時(shí)固定為MWEB $scene_info =WxPayConfig::$scene_info;//場(chǎng)景信息 //2.將參數(shù)按照key=value的格式,并按照參數(shù)名ASCII字典序排序生成字符串 $signA ="appid=$appidattach=$attachbody=$bodymch_id=$mch_idnonce_str=$nonce_strnotify_url=$notify_urlout_trade_no=$out_trade_noscene_info=$scene_infospbill_create_ip=$spbill_create_iptotal_fee=$total_feetrade_type=$trade_type"; //3.拼接字符串 $strSignTmp = $signA."key=$key"; //4.MD5加密后轉(zhuǎn)換成大寫 $sign = strtoupper(MD5($strSignTmp)); //5.拼接成所需XML格式 $post_data = "xml> appid>$appid/appid> attach>$attach/attach> 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> spbill_create_ip>$spbill_create_ip/spbill_create_ip> total_fee>$total_fee/total_fee> trade_type>$trade_type/trade_type> scene_info>$scene_info/scene_info> sign>$sign/sign> /xml>"; //6.以POST方式向微信傳參,并取得微信返回的支付參數(shù) $dataxml = httpRequest($url,'POST',$post_data); $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //將微信返回的XML轉(zhuǎn)換成數(shù)組 return $objectxml; } }
最后,PayMentController.class.php支付文件,支付文件接收前端發(fā)起支付的請(qǐng)求并處理后,調(diào)用Weixin.class.php支付類并接受結(jié)果后返回給前端(此處分享已經(jīng)去掉接口驗(yàn)證等系列代碼邏輯):
public function getPay(){ //1.引入支付類文件 include_once "plugins/Payment/weixin/Weixin.class.php"; $payment = new \Weixin(); $order_id = I('order_id'); //2.判斷參數(shù)是否為空 if (!empty($order_id)){ //3.根據(jù)訂單id查詢訂單是否存在 $order = M('Order')->where(array('id'=>$order_id))->find(); if ($order){//訂單存在 //4.判斷該筆訂單是否已經(jīng)支付,如已支付則返回支付失敗并給出相應(yīng)提示 if ($order['pay_status'] == '1'){ exit(json_encode(array('status'=>'205','msg'=>'該訂單已支付,請(qǐng)勿重復(fù)提交!'))); } $bodys = '訂單:'.$order['order_sn'] . '支付'; //5.調(diào)用支付類中封裝的支付方法并對(duì)應(yīng)傳參 $result = $payment->getCode($order,$bodys); //6.當(dāng)return_code和result_code均為SUCCESS,代表下單成功,將支付參數(shù)返回 if($result['return_code'] == 'SUCCESS'){ if($result['result_code'] == 'SUCCESS'){ exit(json_encode(array('status'=>'0','msg'=>'下單成功,請(qǐng)支付!','result'=>$result['mweb_url']))); }elseif($result['result_code'] == 'FAIL'){ exit(json_encode(array('status'=>'-201','msg'=>$result['err_code_des']))); } }else{ exit(json_encode(array('status'=>'-1','msg'=>'未知錯(cuò)誤,請(qǐng)稍后重試!'))); } }else{ //報(bào)錯(cuò):數(shù)據(jù)不存在 exit(json_encode(array('status'=>'-200','msg'=>'訂單不存在,請(qǐng)核實(shí)后再提交!'))); } }else{ //報(bào)錯(cuò):缺少參數(shù) exit(json_encode(array('status'=>'-204','msg'=>'參數(shù)缺失,請(qǐng)核實(shí)!'))); } }
前端在接收到支付URL后執(zhí)行即可喚醒微信支付。
附一:獲取用戶終端設(shè)備ip方法
function getIP(){ if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "Unknow"; return $ip; }
######附二:CURL請(qǐng)求方法
/** * CURL請(qǐng)求 * @param $url 請(qǐng)求url地址 * @param $method 請(qǐng)求方法 get post * @param null $postfields post數(shù)據(jù)數(shù)組 * @param array $headers 請(qǐng)求header信息 * @param bool|false $debug 調(diào)試開啟 默認(rèn)false * @return mixed */ function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) { $method = strtoupper($method); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在發(fā)起連接前等待的時(shí)間,如果設(shè)置為0,則無限等待 */ curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 設(shè)置cURL允許執(zhí)行的最長(zhǎng)秒數(shù) */ curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); switch ($method) { case "POST": curl_setopt($ci, CURLOPT_POST, true); if (!empty($postfields)) { $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields; curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr); } break; default: curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //設(shè)置請(qǐng)求方式 */ break; } $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE; curl_setopt($ci, CURLOPT_URL, $url); if($ssl){ curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https請(qǐng)求 不驗(yàn)證證書和hosts curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不從證書中檢查SSL加密算法是否存在 } curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的數(shù)量,這個(gè)選項(xiàng)是和CURLOPT_FOLLOWLOCATION一起使用的*/ curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLINFO_HEADER_OUT, true); $response = curl_exec($ci); $requestinfo = curl_getinfo($ci); if ($debug) { echo "=====post data======\r\n"; var_dump($postfields); echo "=====info===== \r\n"; print_r($requestinfo); echo "=====response=====\r\n"; print_r($response); } curl_close($ci); return $response; }
好了,一點(diǎn)點(diǎn)菜鳥心得,有不當(dāng)之處歡迎留言指證交流,一起成長(zhǎng)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:成都 山東 張家口 林芝 巴中 威海 梅州 泰州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP微信H5支付開發(fā)實(shí)例》,本文關(guān)鍵詞 PHP,微信,支付,開發(fā)實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。