基于H5的微信支付開(kāi)發(fā)詳解

2015-11-10 15:48:00 來(lái)源:51CTO.com 作者:佚名 人氣: 次閱讀 349 條評(píng)論

這次總結(jié)一下用戶在微信內(nèi)打開(kāi)網(wǎng)頁(yè)時(shí),可以調(diào)用微信支付完成下單功能的模塊開(kāi)發(fā),也就是在微信內(nèi)的H5頁(yè)面通過(guò)jsApi接口實(shí)現(xiàn)支付功能。當(dāng)然了,微信官網(wǎng)上的微信支付開(kāi)發(fā)文...

這次總結(jié)一下用戶在微信內(nèi)打開(kāi)網(wǎng)頁(yè)時(shí),可以調(diào)用微信支 付完成下單功能的模塊開(kāi)發(fā),也就是在微信內(nèi)的H5頁(yè)面通過(guò)jsApi接口實(shí)現(xiàn)支付功能。當(dāng)然了,微信官網(wǎng)上的微信支付開(kāi)發(fā)文檔也講解的很詳細(xì),并且有實(shí)現(xiàn) 代碼可供參考,有的朋友直接看文檔就可以自己實(shí)現(xiàn)此支付接口的開(kāi)發(fā)了。

一、前言

為何我還寫一篇微信支付接口的博文呢?第一,我們 必須知道,所謂的工作經(jīng)驗(yàn)很多都是靠總結(jié)出來(lái)的,你只有總結(jié)了更多知識(shí),積累了更多經(jīng)驗(yàn),你才能在該行業(yè)中脫穎而出,我個(gè)人覺(jué)得如今的招聘,很多都需要工 作經(jīng)驗(yàn)(1年、3年、5年....),其實(shí),工作時(shí)間的長(zhǎng)久不能衡量一個(gè)人技術(shù)水平的高低,有的人一年的工作經(jīng)驗(yàn)?zāi)苣?年工作經(jīng)驗(yàn)的程序猿的工資,有的3 年工作經(jīng)驗(yàn)的卻有可能比別人只有一年工作經(jīng)驗(yàn)的還低,所以說(shuō),總結(jié)才能讓自己的知識(shí)體系,經(jīng)驗(yàn)深度更牛逼更穩(wěn)固(雖然寫一篇博文挺花費(fèi)時(shí)間的);第二,寫 博文分享給大家還是挺有成就感的,首先是能讓新手從我分享的博文中能學(xué)到東西,并且能快速將博文所講解的技術(shù)運(yùn)用到實(shí)際中來(lái),所以我寫的博文基本上能讓新 人快速讀懂并且容易理解,另外,技術(shù)大神的話,看到博文有講解的不對(duì)之處,還可以指出,并且可以交流,何樂(lè)而不為呢,我們需要的就是分享和交流。

扯遠(yuǎn)了,直接進(jìn)入該主題的詳解。

現(xiàn)在的微信支付方式有N種,看下圖,有刷卡支付、 公眾號(hào)支付、掃碼支付和APP支付,另外還有支付工具的開(kāi)發(fā),本博文選擇的是公眾號(hào)支付借口而開(kāi)發(fā)進(jìn)行講解,其他幾種支付接口開(kāi)發(fā)基本上思路都是一樣的, 只要你能看懂我這博文所講解的基本思路,你基本上也能獨(dú)自開(kāi)發(fā)其他幾個(gè)支付接口。

HTML5教程 HTML5技術(shù) 微信支付 微信支付接口開(kāi)發(fā) 微信支付api

二、思路詳解

我們可以拿微信支付接口文檔里的業(yè)務(wù)流程時(shí)序圖看 看,如下圖,基本思路是這樣子:首先在后臺(tái)生成一個(gè)鏈接,展示給用戶讓用戶點(diǎn)擊(例如頁(yè)面上有微信支付的按鈕),用戶點(diǎn)擊按鈕后,網(wǎng)站后臺(tái)會(huì)根據(jù)訂單的相 關(guān)信息生成一個(gè)支付訂單,此時(shí)會(huì)調(diào)用統(tǒng)一下單接口,對(duì)微信支付系統(tǒng)發(fā)起請(qǐng)求,而微信支付系統(tǒng)受到請(qǐng)求后,會(huì)根據(jù)請(qǐng)求過(guò)來(lái)的數(shù)據(jù),生成一個(gè) 預(yù)支付交易會(huì)話標(biāo)識(shí)(prepay_id,就是通過(guò)這個(gè)來(lái)識(shí)別該訂單的),我們的網(wǎng)站收到微信支付系統(tǒng)的響應(yīng)后,會(huì)得到prepay_id,然后通過(guò)自己 構(gòu)造微信支付所需要的參數(shù),接著將支付所需參數(shù)返回給客戶端,用戶此時(shí)可能會(huì)有一個(gè)訂單信息頁(yè),會(huì)有一個(gè)按鈕,點(diǎn)擊支付,此時(shí)會(huì)調(diào)用JSAPI接口對(duì)微信 支付系統(tǒng)發(fā)起 請(qǐng)求支付,微信支付系統(tǒng)檢查了請(qǐng)求的相關(guān)合法性之后,就會(huì)提示輸入密碼,用戶此時(shí)輸入密碼確認(rèn),微信支付系統(tǒng)會(huì)對(duì)其進(jìn)行驗(yàn)證,通過(guò)的話會(huì)返回支付結(jié)果,然 后微信跳轉(zhuǎn)會(huì)H5頁(yè)面,這其中有一步是異步通知網(wǎng)站支付結(jié)果,我們網(wǎng)站需要對(duì)此進(jìn)行處理(比如說(shuō)異步支付結(jié)果通過(guò)后,需要更新數(shù)據(jù)表或者訂單信息,例如標(biāo) 志用戶已支付該訂單了,同時(shí)也需要更新訂單日志,防止用戶重復(fù)提交訂單)。

HTML5教程 HTML5技術(shù) 微信支付 微信支付接口開(kāi)發(fā) 微信支付api

三、代碼講解

本次開(kāi)發(fā)環(huán)境用的是php5.6 + MySQL + Redis + Linux + Apache,所選用的框架的CI框架(這些環(huán)境不一定需要和我的一致,框架也可以自己選擇,反正自己稍微修改下代碼就能移植過(guò)去了)。

微信支付接口的開(kāi)發(fā)代碼我已經(jīng)提前寫好了,在這里我對(duì)其進(jìn)行分析講解,方便大家能輕松理解,當(dāng)然,假如你有一定的基礎(chǔ),直接看代碼就能理清所有流程了,并且我的代碼基本上都寫上了注釋(對(duì)于新手來(lái)說(shuō),這一點(diǎn)比微信文檔所提供的代碼好一點(diǎn))。

1、構(gòu)造一個(gè)鏈接展示給用戶

這里我們提前需要知道一個(gè)點(diǎn),那就是請(qǐng)求統(tǒng)一下單接口需要微信用戶的openid(詳情可看這https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1),而獲取openid需要先獲取code(詳情可看這微信登錄接口),所以我們需要構(gòu)造一個(gè)獲取code的URL:

  1. Wxpay.php文件:
  2. <?php
  3. defined('BASEPATH')ORexit('NodirectscriptAccessallowed');
  4. classWxpayextendsMY_Controller{
  5. publicfunction__construct(){
  6. parent::__construct();
  7. $this->load->model('wxpay_model');
  8. //$this->load->model('wxpay
  9. }
  10. publicfunctionindex(){
  11. //微信支付
  12. $this->smarty['wxPayUrl']=$this->wxpay_model->retWxPayUrl();
  13. $this->displayView('wxpay/index.tpl');
  14. }
  15. }

在這先看看model里所寫的幾個(gè)類:model里有幾個(gè)類:微信支付類、統(tǒng)一下單接口類、響應(yīng)型接口基類、請(qǐng)求型接口基類、所有接口基類、配置類。為何要分那么多類而不在一個(gè)類里實(shí)現(xiàn)所有的方法的,因?yàn)?,這樣看起來(lái)代碼邏輯清晰,哪個(gè)類該干嘛就干嘛。

這里我直接附上model的代碼了,里面基本上每一個(gè)類每一個(gè)方法甚至每一行代碼都會(huì)有解釋的了,這里我就不對(duì)其展開(kāi)一句句分析了:

  1. <?php
  2. defined('BASEPATH')ORexit('Nodirectscriptaccessallowed');
  3. classWxpay_modelextendsCI_Model{
  4. publicfunction__construct(){
  5. parent::__construct();
  6. }
  7. /**
  8. *返回可以獲得微信code的URL(用以獲取openid)
  9. *@return[type][description]
  10. */
  11. publicfunctionretWxPayUrl(){
  12. $jsApi=newJsApi_handle();
  13. return$jsApi->createOauthUrlForCode();
  14. }
  15. /**
  16. *微信jsapi點(diǎn)擊支付
  17. *@param[type]$data[description]
  18. *@return[type][description]
  19. */
  20. publicfunctionwxPayJsApi($data){
  21. $jsApi=newJsApi_handle();
  22. //統(tǒng)一下單接口所需數(shù)據(jù)
  23. $payData=$this->returnData($data);
  24. //獲取code碼,用以獲取openid
  25. $code=$_GET['code'];
  26. $jsApi->setCode($code);
  27. //通過(guò)code獲取openid
  28. $openid=$jsApi->getOpenId();
  29. $unifiedOrderResult=null;
  30. if($openid!=null){
  31. //取得統(tǒng)一下單接口返回的數(shù)據(jù)
  32. $unifiedOrderResult=$this->getResult($payData,'JSAPI',$openid);
  33. //獲取訂單接口狀態(tài)
  34. $returnMessage=$this->returnMessage($unifiedOrder,'prepay_id');
  35. if($returnMessage['resultCode']){
  36. $jsApi->setPrepayId($retuenMessage['resultField']);
  37. //取得wxjsapi接口所需要的數(shù)據(jù)
  38. $returnMessage['resultData']=$jsApi->getParams();
  39. }
  40. return$returnMessage;
  41. }
  42. }
  43. /**
  44. *統(tǒng)一下單接口所需要的數(shù)據(jù)
  45. *@param[type]$data[description]
  46. *@return[type][description]
  47. */
  48. publicfunctionreturnData($data){
  49. $payData['sn']=$data['sn'];
  50. $payData['body']=$data['goods_name'];
  51. $payData['out_trade_no']=$data['order_no'];
  52. $payData['total_fee']=$data['fee'];
  53. $payData['attach']=$data['attach'];
  54. return$payData;
  55. }
  56. /**
  57. *返回統(tǒng)一下單接口結(jié)果(參考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
  58. *@param[type]$payData[description]
  59. *@param[type]$trade_type[description]
  60. *@param[type]$openid[description]
  61. *@return[type][description]
  62. */
  63. publicfunctiongetResult($payData,$trade_type,$openid=null){
  64. $unifiedOrder=newUnifiedOrder_handle();
  65. if($opneid!=null){
  66. $unifiedOrder->setParam('openid',$openid);
  67. }
  68. $unifiedOrder->setParam('body',$payData['body']);//商品描述
  69. $unifiedOrder->setParam('out_trade_no',$payData['out_trade_no']);//商戶訂單號(hào)
  70. $unifiedOrder->setParam('total_fee',$payData['total_fee']);//總金額
  71. $unifiedOrder->setParam('attach',$payData['attach']);//附加數(shù)據(jù)
  72. $unifiedOrder->setParam('notify_url',base_url('/Wxpay/pay_callback'));//通知地址
  73. $unifiedOrder->setParam('trade_type',$trade_type);//交易類型
  74. //非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
  75. //$unifiedOrder->setParam("sub_mch_id","XXXX");//子商戶號(hào)
  76. //$unifiedOrder->setParam("device_info","XXXX");//設(shè)備號(hào)
  77. //$unifiedOrder->setParam("time_start","XXXX");//交易起始時(shí)間
  78. //$unifiedOrder->setParam("time_expire","XXXX");//交易結(jié)束時(shí)間
  79. //$unifiedOrder->setParam("goods_tag","XXXX");//商品標(biāo)記
  80. //$unifiedOrder->setParam("product_id","XXXX");//商品ID
  81. return$unifiedOrder->getResult();
  82. }
  83. /**
  84. *返回微信訂單狀態(tài)
  85. */
  86. publicfunctionreturnMessage($unifiedOrderResult,$field){
  87. $arrMessage=array("resultCode"=>0,"resultType"=>"獲取錯(cuò)誤","resultMsg"=>"該字段為空");
  88. if($unifiedOrderResult==null){
  89. $arrMessage["resultType"]="未獲取權(quán)限";
  90. $arrMessage["resultMsg"]="請(qǐng)重新打開(kāi)頁(yè)面";
  91. }elseif($unifiedOrderResult["return_code"]=="FAIL")
  92. {
  93. $arrMessage["resultType"]="網(wǎng)絡(luò)錯(cuò)誤";
  94. $arrMessage["resultMsg"]=$unifiedOrderResult['return_msg'];
  95. }
  96. elseif($unifiedOrderResult["result_code"]=="FAIL")
  97. {
  98. $arrMessage["resultType"]="訂單錯(cuò)誤";
  99. $arrMessage["resultMsg"]=$unifiedOrderResult['err_code_des'];
  100. }
  101. elseif($unifiedOrderResult[$field]!=NULL)
  102. {
  103. $arrMessage["resultCode"]=1;
  104. $arrMessage["resultType"]="生成訂單";
  105. $arrMessage["resultMsg"]="OK";
  106. $arrMessage["resultField"]=$unifiedOrderResult[$field];
  107. }
  108. return$arrMessage;
  109. }
  110. /**
  111. *微信回調(diào)接口返回驗(yàn)證簽名并回應(yīng)微信
  112. *@param[type]$xml[description]
  113. *@return[type][description]
  114. */
  115. publicfunctionwxPayNotify($xml){
  116. $notify=newWxpay_server();
  117. $notify->saveData($xml);
  118. //驗(yàn)證簽名,并回復(fù)微信
  119. //對(duì)后臺(tái)通知交互時(shí),如果微信收到商戶的應(yīng)答不是成功或者超時(shí),微信認(rèn)為通知失敗
  120. //微信會(huì)通過(guò)一定的策略(如30分鐘共8次),定期重新發(fā)起通知
  121. if($notify->checkSign()==false){
  122. $notify->setReturnParameter("return_code","FAIL");//返回狀態(tài)碼
  123. $notify->setReturnParameter("return_msg","簽名失敗");//返回信息
  124. }else{
  125. $notify->checkSign=TRUE;
  126. $notify->setReturnParameter("return_code","SUCCESS");//設(shè)置返回碼
  127. }
  128. return$notify;
  129. }
  130. }
  131. /**
  132. *JSAPI支付——H5網(wǎng)頁(yè)端調(diào)起支付接口
  133. */
  134. classJsApi_handleextendsJsApi_common{
  135. public$code;//code碼,用以獲取openid
  136. public$openid;//用戶的openid
  137. public$parameters;//jsapi參數(shù),格式為json
  138. public$prepay_id;//使用統(tǒng)一支付接口得到的預(yù)支付id
  139. public$curl_timeout;//curl超時(shí)時(shí)間
  140. function__construct()
  141. {
  142. //設(shè)置curl超時(shí)時(shí)間
  143. $this->curl_timeout=WxPayConf::CURL_TIMEOUT;
  144. }
  145. /**
  146. *生成獲取code的URL
  147. *@return[type][description]
  148. */
  149. publicfunctioncreateOauthUrlForCode(){
  150. //重定向URL
  151. $redirectUrl="http://www.itcen.cn/wxpay/confirm/".$orderId."?showwxpaytitle=1";
  152. $urlParams['appid']=WxPayConf::APPID;
  153. $urlParams['redirect_uri']=$redirectUrl;
  154. $urlParams['response_type']='code';
  155. $urlParams['scope']='snsapi_base';
  156. $urlParams['state']="STATE"."#wechat_redirect";
  157. //拼接字符串
  158. $queryString=$this->ToUrlParams($urlParams,false);
  159. return"https://open.weixin.qq.com/connect/oauth2/authorize?".$queryString;
  160. }
  161. /**
  162. *設(shè)置code
  163. *@param[type]$code[description]
  164. */
  165. publicfunctionsetCode($code){
  166. $this->code=$code;
  167. }
  168. /**
  169. *作用:設(shè)置prepay_id
  170. */
  171. publicfunctionsetPrepayId($prepayId)
  172. {
  173. $this->prepay_id=$prepayId;
  174. }
  175. /**
  176. *作用:獲取jsapi的參數(shù)
  177. */
  178. publicfunctiongetParams()
  179. {
  180. $jsApiObj["appId"]=WxPayConf::APPID;
  181. $timeStamp=time();
  182. $jsApiObj["timeStamp"]="$timeStamp";
  183. $jsApiObj["nonceStr"]=$this->createNoncestr();
  184. $jsApiObj["package"]="prepay_id=$this->prepay_id";
  185. $jsApiObj["signType"]="MD5";
  186. $jsApiObj["paySign"]=$this->getSign($jsApiObj);
  187. $this->parameters=json_encode($jsApiObj);
  188. return$this->parameters;
  189. }
  190. /**
  191. *通過(guò)curl向微信提交code用以獲取openid
  192. *@return[type][description]
  193. */
  194. publicfunctiongetOpenId(){
  195. //創(chuàng)建openid的鏈接
  196. $url=$this->createOauthUrlForOpenid();
  197. //初始化
  198. $ch=curl_init();
  199. curl_setopt($ch,CURL_TIMEOUT,$this->curl_timeout);
  200. curl_setopt($ch,CURL_URL,$url);
  201. curl_setopt($ch,CURL_SSL_VERIFYPEER,FALSE);
  202. curl_setopt($ch,CURL_SSL_VERIFYHOST,FALSE);
  203. curl_setopt($ch,CURL_HEADER,FALSE);
  204. curl_setopt($ch,CURL_RETURNTRANSFER,TRUE);
  205. //執(zhí)行curl
  206. $res=curl_exec($ch);
  207. curl_close($ch);
  208. //取出openid
  209. $data=json_decode($res);
  210. if(isset($data['openid'])){
  211. $this->openid=$data['openid'];
  212. }else{
  213. returnnull;
  214. }
  215. return$this->openid;
  216. }
  217. /**
  218. *生成可以獲取openid的URL
  219. *@return[type][description]
  220. */
  221. publicfunctioncreateOauthUrlForOpenid(){
  222. $urlParams['appid']=WxPayConf::APPID;
  223. $urlParams['secret']=WxPayConf::APPSECRET;
  224. $urlParams['code']=$this->code;
  225. $urlParams['grant_type']="authorization_code";
  226. $queryString=$this->ToUrlParams($urlParams,false);
  227. return"https://api.weixin.qq.com/sns/oauth2/access_token?".$queryString;
  228. }
  229. }
  230. /**
  231. *統(tǒng)一下單接口類
  232. */
  233. classUnifiedOrder_handleextendsWxpay_client_handle{
  234. publicfunction__construct(){
  235. //設(shè)置接口鏈接
  236. $this->url="https://api.mch.weixin.qq.com/pay/unifiedorder";
  237. //設(shè)置curl超時(shí)時(shí)間
  238. $this->curl_timeout=WxPayConf::CURL_TIMEOUT;
  239. }
  240. }
  241. /**
  242. *響應(yīng)型接口基類
  243. */
  244. classWxpay_server_handleextendsJsApi_common{
  245. public$data;//接收到的數(shù)據(jù),類型為關(guān)聯(lián)數(shù)組
  246. public$returnParams;//返回參數(shù),類型為關(guān)聯(lián)數(shù)組
  247. /**
  248. *將微信請(qǐng)求的xml轉(zhuǎn)換成關(guān)聯(lián)數(shù)組
  249. *@param[type]$xml[description]
  250. *@return[type][description]
  251. */
  252. publicfunctionsaveData($xml){
  253. $this->data=$this->xmlToArray($xml);
  254. }
  255. /**
  256. *驗(yàn)證簽名
  257. *@return[type][description]
  258. */
  259. publicfunctioncheckSign(){
  260. $tmpData=$this->data;
  261. unset($temData['sign']);
  262. $sign=$this->getSign($tmpData);
  263. if($this->data['sign']==$sign){
  264. returntrue;
  265. }
  266. returnfalse;
  267. }
  268. /**
  269. *設(shè)置返回微信的xml數(shù)據(jù)
  270. */
  271. functionsetReturnParameter($parameter,$parameterValue)
  272. {
  273. $this->returnParameters[$this->trimString($parameter)]=$this->trimString($parameterValue);
  274. }
  275. /**
  276. *將xml數(shù)據(jù)返回微信
  277. */
  278. functionreturnXml()
  279. {
  280. $returnXml=$this->createXml();
  281. return$returnXml;
  282. }
  283. }
  284. /**
  285. *請(qǐng)求型接口的基類
  286. */
  287. classWxpay_client_handleextendsJsApi_common{
  288. public$params;//請(qǐng)求參數(shù),類型為關(guān)聯(lián)數(shù)組
  289. public$response;//微信返回的響應(yīng)
  290. public$result;//返回參數(shù),類型類關(guān)聯(lián)數(shù)組
  291. public$url;//接口鏈接
  292. public$curl_timeout;//curl超時(shí)時(shí)間
  293. /**
  294. *設(shè)置請(qǐng)求參數(shù)
  295. *@param[type]$param[description]
  296. *@param[type]$paramValue[description]
  297. */
  298. publicfunctionsetParam($param,$paramValue){
  299. $this->params[$this->tirmString($param)]=$this->trimString($paramValue);
  300. }
  301. /**
  302. *獲取結(jié)果,默認(rèn)不使用證書(shū)
  303. *@return[type][description]
  304. */
  305. publicfunctiongetResult(){
  306. $this->postxml();
  307. $this->result=$this->xmlToArray($this->response);
  308. return$this->result;
  309. }
  310. /**
  311. *post請(qǐng)求xml
  312. *@return[type][description]
  313. */
  314. publicfunctionpostxml(){
  315. $xml=$this->createXml();
  316. $this->response=$this->postXmlCurl($xml,$this->curl,$this->curl_timeout);
  317. return$this->response;
  318. }
  319. publicfunctioncreateXml(){
  320. $this->params['appid']=WxPayConf::APPID;//公眾號(hào)ID
  321. $this->params['mch_id']=WxPayConf::MCHID;//商戶號(hào)
  322. $this->params['nonce_str']=$this->createNoncestr();//隨機(jī)字符串
  323. $this->params['sign']=$this->getSign($this->params);//簽名
  324. return$this->arrayToXml($this->params);
  325. }
  326. }
  327. /**
  328. *所有接口的基類
  329. */
  330. classJsApi_common{
  331. function__construct(){
  332. }
  333. publicfunctiontrimString($value){
  334. $ret=null;
  335. if(null!=$value){
  336. $ret=trim($value);
  337. if(strlen($ret)==0){
  338. $ret=null;
  339. }
  340. }
  341. return$ret;
  342. }
  343. /**
  344. *產(chǎn)生隨機(jī)字符串,不長(zhǎng)于32位
  345. *@paraminteger$length[description]
  346. *@return[type][description]
  347. */
  348. publicfunctioncreateNoncestr($length=32){
  349. $chars="abcdefghijklmnopqrstuvwxyz0123456789";
  350. $str='';
  351. for($i=0;$i<$length;$i++){
  352. $str.=substr($chars,mt_rand(0,strlen($chars)-1),1);
  353. }
  354. return$str;
  355. }
  356. /**
  357. *格式化參數(shù)拼接字符串,簽名過(guò)程需要使用
  358. *@param[type]$urlParams[description]
  359. *@param[type]$needUrlencode[description]
  360. */
  361. publicfunctionToUrlParams($urlParams,$needUrlencode){
  362. $buff="";
  363. ksort($urlParams);
  364. foreach($urlParamsas$k=>$v){
  365. if($needUrlencode)$v=urlencode($v);
  366. $buff.=$k.'='.$v.'&';
  367. }
  368. $reqString='';
  369. if(strlen($buff)>0){
  370. $reqString=substr($buff,0,strlen($buff)-1);
  371. }
  372. return$reqString;
  373. }
  374. /**
  375. *生成簽名
  376. *@param[type]$params[description]
  377. *@return[type][description]
  378. */
  379. publicfunctiongetSign($obj){
  380. foreach($objas$k=>$v){
  381. $params[$k]=$v;
  382. }
  383. //簽名步驟一:按字典序排序參數(shù)
  384. ksort($params);
  385. $str=$this->ToUrlParams($params,false);
  386. //簽名步驟二:在$str后加入key
  387. $str=$str."$key=".WxPayConf::KEY;
  388. //簽名步驟三:md5加密
  389. $str=md5($str);
  390. //簽名步驟四:所有字符轉(zhuǎn)為大寫
  391. $result=strtoupper($str);
  392. return$result;
  393. }
  394. /**
  395. *array轉(zhuǎn)xml
  396. *@param[type]$arr[description]
  397. *@return[type][description]
  398. */
  399. publicfunctionarrayToXml($arr){
  400. $xml="<xml>";
  401. foreach($arras$k=>$v){
  402. if(is_numeric($val)){
  403. $xml.="<".$key.">".$key."</".$key.">";
  404. }else{
  405. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  406. }
  407. }
  408. $xml.="</xml>";
  409. return$xml;
  410. }
  411. /**
  412. *將xml轉(zhuǎn)為array
  413. *@param[type]$xml[description]
  414. *@return[type][description]
  415. */
  416. publicfunctionxmlToArray($xml){
  417. $arr=json_decode(json_encode(simplexml_load_string($xml,'SinpleXMLElement',LIBXML_NOCDATA)),true);
  418. return$arr;
  419. }
  420. /**
  421. *以post方式提交xml到對(duì)應(yīng)的接口
  422. *@param[type]$xml[description]
  423. *@param[type]$url[description]
  424. *@paraminteger$second[description]
  425. *@return[type][description]
  426. */
  427. publicfunctionpostXmlCurl($xml,$url,$second=30){
  428. //初始化curl
  429. $ch=curl_init();
  430. //設(shè)置超時(shí)
  431. curl_setopt($ch,CURL_TIMEOUT,$second);
  432. curl_setopt($ch,CURL_URL,$url);
  433. //這里設(shè)置代理,如果有的話
  434. //curl_setopt($ch,CURLOPT_PROXY,'8.8.8.8
  435. //curl_setopt($ch,CURLOPT_PROXYPORT,8080);
  436. curl_setopt($ch,CURL_SSL_VERIFYHOST,FALSE);
  437. curl_setopt($ch,CURL_SSL_VERIFYPEER,FALSE);
  438. //設(shè)置header
  439. curl_setopt($ch,CURL_HEADER,FALSE);
  440. //要求結(jié)果為字符串且輸出到屏幕上
  441. curl_setopt($ch,CURL_RETURNTRANSFER,TRUE);
  442. //以post方式提交
  443. curl_setopt($ch,CURL_POST,TRUE);
  444. curl_setopt($ch,CURL_POSTFIELDS,$xml);
  445. //執(zhí)行curl
  446. $res=curl_exec($ch);
  447. if($res){
  448. curl_close($ch);
  449. return$res;
  450. }else{
  451. $error=curl_errno($ch);
  452. echo"curl出錯(cuò),錯(cuò)誤碼:$error"."<br>";
  453. echo"<a;
  454. curl_close($ch);
  455. returnfalse;
  456. }
  457. }
  458. }
  459. /**
  460. *配置類
  461. */
  462. classWxPayConf{
  463. //微信公眾號(hào)身份的唯一標(biāo)識(shí)。
  464. constAPPID='wx654a22c6423213b7';
  465. //受理商ID,身份標(biāo)識(shí)
  466. constMCHID='10043241';
  467. constMCHNAME='KellyCen的博客';
  468. //商戶支付密鑰Key。
  469. constKEY='0000000000000000000000000000000';
  470. //JSAPI接口中獲取openid
  471. constAPPSECRET='000000000000000000000000000';
  472. //證書(shū)路徑,注意應(yīng)該填寫絕對(duì)路徑
  473. constSSLCERT_PATH='/home/WxPayCacert/apiclient_cert.pem';
  474. constSSLKEY_PATH='/home/WxPayCacert/apiclient_key.pem';
  475. constSSLCA_PATH='/home/WxPayCacert/rootca.pem';
  476. //本例程通過(guò)curl使用HTTPPOST方法,此處可修改其超時(shí)時(shí)間,默認(rèn)為30秒
  477. constCURL_TIMEOUT=30;
  478. }
  479. Wxpay_model.php

獲取到code的URL后,將其分配到頁(yè)面去,讓用戶去點(diǎn)擊,用戶進(jìn)行點(diǎn)擊后,就會(huì)從微信服務(wù)器獲取到code,然后回調(diào)到redirect_uri所指的地址去。

    無(wú)相關(guān)信息