日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
基于H5的微信支付開發(fā)詳解

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

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比庫車網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式庫車網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋庫車地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

一、前言

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

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

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

二、思路詳解

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

#p#

三、代碼講解

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

微信支付接口的開發(fā)代碼我已經(jīng)提前寫好了,在這里我對(duì)其進(jìn)行分析講解,方便大家能輕松理解,當(dāng)然,假如你有一定的基礎(chǔ),直接看代碼就能理清所有流程了,并且我的代碼基本上都寫上了注釋(對(duì)于新手來說,這一點(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. defined('BASEPATH') OR exit('No direct script access allowed'); 
  3.  
  4. class Wxpay extends MY_Controller { 
  5.     public function __construct() { 
  6.         parent::__construct(); 
  7.         $this->load->model('wxpay_model'); 
  8.         //$this->load->model('wxpay'); 
  9.          
  10.     } 
  11.   
  12.     public function index() { 
  13.         //微信支付 
  14.         $this->smarty['wxPayUrl'] = $this->wxpay_model->retWxPayUrl(); 
  15.         $this->displayView('wxpay/index.tpl'); 
  16.     } 

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

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

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