威富通支付接口用到的操作类(记录下)

  1 <?php
  2 /**
  3  * 威富通支付接口
  4  */
  5 class SwiftpassClient {
  6 
  7     function __construct() {
  8         //todo
  9     }
 10 
 11     // 获取请求支付的页面url
 12     public function getUrl($url, $postData)
 13     {
 14         ksort($postData);
 15         $postData = $this->urlencode_array($postData);
 16         $postData['sign'] = md5(implode('', $postData) . md5('!!wxpay!!'));
 17         return $url . '?' . http_build_query($postData);
 18     }
 19 
 20     // 获取签名信息
 21     public function getSign($postData, $key)
 22     {
 23         $returnStr = '';
 24         if ($postData) {
 25             if (isset($postData['sign'])) unset($postData['sign']);
 26             ksort($postData);
 27             $signStr = '';
 28             foreach ($postData as $_k => $_v) {
 29                 $signStr .= "{$_k}={$_v}&";
 30             }
 31             $signStr .= 'key=' . $key;
 32             $returnStr = strtoupper(md5($signStr));
 33         }
 34 
 35         return $returnStr;
 36     }
 37 
 38     // 生成随机码 默认16位
 39     public function getRandKey($digit=16)
 40     {
 41         $returnStr = '';
 42         // A-Za-z0-9 共62个
 43         $baseKey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 44         $randKeyArr = str_split($baseKey);
 45         $maxLen = count($randKeyArr);
 46 
 47         for ($i=0; $i<$digit; $i++) {
 48             // 每10次, 打乱一次
 49             if (($i % 10) == 0) shuffle($randKeyArr);
 50             $k = $i % $maxLen;
 51             $returnStr .= $randKeyArr{$k};
 52         }
 53 
 54         return $returnStr;
 55     }
 56 
 57     // 解析PostData xml格式
 58     public function xmlToArray($xmlStr)
 59     {
 60         return json_decode(json_encode(simplexml_load_string($xmlStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
 61     }
 62 
 63     /**
 64      * 将xml转换成数组
 65      * @param $xmlData
 66      * @return array
 67      */
 68     public function xmlToArray2($xml) {
 69         $xml = simplexml_load_string($xml);
 70         //获取xml编码
 71         $ret = preg_match ("/<?xml[^>]* encoding=\"(.*)\"[^>]* ?>/i", $xml, $arr);
 72         if($ret) {
 73             $encode = strtoupper ( $arr[1] );
 74         } else {
 75             $encode = "";
 76         }
 77         if($xml && $xml->children()) {
 78             foreach ($xml->children() as $node){
 79                 //有子节点
 80                 if($node->children()) {
 81                     $k = $node->getName();
 82                     $nodeXml = $node->asXML();
 83                     $v = substr($nodeXml, strlen($k)+2, strlen($nodeXml)-2*strlen($k)-5);
 84 
 85                 } else {
 86                     $k = $node->getName();
 87                     $v = (string)$node;
 88                 }
 89 
 90                 if($encode!="" && $encode != "UTF-8") {
 91                     $k = iconv("UTF-8", $encode, $k);
 92                     $v = iconv("UTF-8", $encode, $v);
 93                 }
 94 
 95                 $parameters[$k] = $v;
 96             }
 97         }
 98         return $parameters;
 99     }
100 
101     // 数组转xml
102     public function arrayToXml($array)
103     {
104         $returnStr = "<xml>";
105         foreach ($array as $key => $val) {
106             if (is_numeric($val)) {
107                $returnStr .= "<".$key.">".$val."</".$key.">";
108             } else {
109                 $returnStr .= "<".$key."><![CDATA[".$val."]]></".$key.">";
110             }
111         }
112         $returnStr .= "</xml>";
113         return $returnStr;
114     }
115 
116 
117     // 数组值urlencode编码
118     public function urlencode_array($array)
119     {
120         if (is_array($array)) {
121             foreach ($array as $_key => $_val) {
122                 $array[$_key] = $this->urlencode_array($_val);
123             }
124         } else {
125             $array = rawurlencode($array);
126         }
127 
128         return $array;
129     }
130 
131     // 数组值urldecode解码
132     public function urldecode_array($array)
133     {
134         if (is_array($array)) {
135             foreach ($array as $_key => $_val) {
136                 $array[$_key] = $this->urldecode_array($_val);
137             }
138         } else {
139             $array = rawurldecode($array);
140         }
141 
142         return $array;
143     }
144 
145     // post xml数据
146     public function postXml($url, $array)
147     {
148         // curl初始化设置
149         $ch = curl_init();
150         curl_setopt($ch, CURLOPT_TIMEOUT, 14);  // 设置超时
151         curl_setopt($ch, CURLOPT_URL, $url);
152         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
153         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
154         curl_setopt($ch, CURLOPT_HEADER, false);
155         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
156         curl_setopt($ch, CURLOPT_POST, true);
157         curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
158 
159         // 运行curl
160         $response = curl_exec($ch);
161         curl_close($ch);
162 
163         return $response;
164     }
165 
166     public function postXmlSSL($url, $array)
167     {
168         // curl初始化设置
169         $ch = curl_init();
170         curl_setopt($ch, CURLOPT_TIMEOUT, 14);  // 设置超时
171         curl_setopt($ch, CURLOPT_URL, $url);
172         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
173         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
174         curl_setopt($ch, CURLOPT_HEADER, false);
175         curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
176         curl_setopt($ch, CURLOPT_SSLCERT, '证书路径');
177         curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
178         curl_setopt($ch, CURLOPT_SSLKEY, '密钥路径');
179         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
180         curl_setopt($ch, CURLOPT_POST, true);
181         curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
182 
183         // 运行curl
184         $response = curl_exec($ch);
185         curl_close($ch);
186 
187         return $response;
188     }
189 
190 }
191 ?>

 

posted @ 2016-04-19 10:31  多多喜  阅读(5579)  评论(2编辑  收藏  举报