wechatpay.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK, 旧版微信支付接口(微信支付V2)
  4. * @author dodge <dodgepudding@gmail.com>
  5. * @link https://github.com/dodgepudding/wechat-php-sdk
  6. * @version 1.2
  7. * 参考旧版文档 https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
  8. * usage:
  9. * $options = array(
  10. * 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  11. * 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  12. * 'partnerid'=>'88888888', //财付通商户身份标识
  13. * 'partnerkey'=>'', //财付通商户权限密钥Key
  14. * 'paysignkey'=>'' //商户签名密钥Key
  15. * );
  16. * $payObj = new Wechatpay($options);
  17. * $package = $payObj->createPackage($out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip,$fee_type,$bank_type,$input_charset,$time_start,$time_expire,$transport_fee,$product_fee,$goods_tag,$attach);
  18. *
  19. */
  20. class Wechatpay
  21. {
  22. const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
  23. const AUTH_URL = '/token?grant_type=client_credential&';
  24. const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API接口URL需要使用此前缀
  25. const PAY_DELIVERNOTIFY = '/pay/delivernotify?';
  26. const PAY_ORDERQUERY = '/pay/orderquery?';
  27. private $appid;
  28. private $appsecret;
  29. private $access_token;
  30. private $user_token;
  31. private $partnerid;
  32. private $partnerkey;
  33. private $paysignkey;
  34. public $debug = false;
  35. public $errCode = 40001;
  36. public $errMsg = "no access";
  37. private $_logcallback;
  38. public function __construct($options)
  39. {
  40. $this->appid = isset($options['appid'])?$options['appid']:'';
  41. $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
  42. $this->partnerid = isset($options['partnerid'])?$options['partnerid']:'';
  43. $this->partnerkey = isset($options['partnerkey'])?$options['partnerkey']:'';
  44. $this->paysignkey = isset($options['paysignkey'])?$options['paysignkey']:'';
  45. $this->debug = isset($options['debug'])?$options['debug']:false;
  46. $this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
  47. }
  48. private function log($log){
  49. if ($this->debug && function_exists($this->_logcallback)) {
  50. if (is_array($log)) $log = print_r($log,true);
  51. return call_user_func($this->_logcallback,$log);
  52. }
  53. }
  54. /**
  55. * GET 请求
  56. * @param string $url
  57. */
  58. private function http_get($url){
  59. $oCurl = curl_init();
  60. if(stripos($url,"https://")!==FALSE){
  61. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  62. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  63. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  64. }
  65. curl_setopt($oCurl, CURLOPT_URL, $url);
  66. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  67. $sContent = curl_exec($oCurl);
  68. $aStatus = curl_getinfo($oCurl);
  69. curl_close($oCurl);
  70. if(intval($aStatus["http_code"])==200){
  71. return $sContent;
  72. }else{
  73. return false;
  74. }
  75. }
  76. /**
  77. * POST 请求
  78. * @param string $url
  79. * @param array $param
  80. * @param boolean $post_file 是否文件上传
  81. * @return string content
  82. */
  83. private function http_post($url,$param,$post_file=false){
  84. $oCurl = curl_init();
  85. if(stripos($url,"https://")!==FALSE){
  86. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  87. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  88. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  89. }
  90. if (is_string($param) || $post_file) {
  91. $strPOST = $param;
  92. } else {
  93. $aPOST = array();
  94. foreach($param as $key=>$val){
  95. $aPOST[] = $key."=".urlencode($val);
  96. }
  97. $strPOST = join("&", $aPOST);
  98. }
  99. curl_setopt($oCurl, CURLOPT_URL, $url);
  100. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  101. curl_setopt($oCurl, CURLOPT_POST,true);
  102. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  103. $sContent = curl_exec($oCurl);
  104. $aStatus = curl_getinfo($oCurl);
  105. curl_close($oCurl);
  106. if(intval($aStatus["http_code"])==200){
  107. return $sContent;
  108. }else{
  109. return false;
  110. }
  111. }
  112. /**
  113. * 获取access_token
  114. * @param string $appid 如在类初始化时已提供,则可为空
  115. * @param string $appsecret 如在类初始化时已提供,则可为空
  116. * @param string $token 手动指定access_token,非必要情况不建议用
  117. */
  118. public function checkAuth($appid='',$appsecret='',$token=''){
  119. if (!$appid || !$appsecret) {
  120. $appid = $this->appid;
  121. $appsecret = $this->appsecret;
  122. }
  123. if ($token) { //手动指定token,优先使用
  124. $this->access_token=$token;
  125. return $this->access_token;
  126. }
  127. //TODO: get the cache access_token
  128. $result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
  129. if ($result)
  130. {
  131. $json = json_decode($result,true);
  132. if (!$json || isset($json['errcode'])) {
  133. $this->errCode = $json['errcode'];
  134. $this->errMsg = $json['errmsg'];
  135. return false;
  136. }
  137. $this->access_token = $json['access_token'];
  138. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  139. //TODO: cache access_token
  140. return $this->access_token;
  141. }
  142. return false;
  143. }
  144. /**
  145. * 删除验证数据
  146. * @param string $appid
  147. */
  148. public function resetAuth($appid=''){
  149. if (!$appid) $appid = $this->appid;
  150. $this->access_token = '';
  151. //TODO: remove cache
  152. return true;
  153. }
  154. /**
  155. * 微信api不支持中文转义的json结构
  156. * @param array $arr
  157. */
  158. static function json_encode($arr) {
  159. $parts = array ();
  160. $is_list = false;
  161. //Find out if the given array is a numerical array
  162. $keys = array_keys ( $arr );
  163. $max_length = count ( $arr ) - 1;
  164. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
  165. $is_list = true;
  166. for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
  167. if ($i != $keys [$i]) { //A key fails at position check.
  168. $is_list = false; //It is an associative array.
  169. break;
  170. }
  171. }
  172. }
  173. foreach ( $arr as $key => $value ) {
  174. if (is_array ( $value )) { //Custom handling for arrays
  175. if ($is_list)
  176. $parts [] = self::json_encode ( $value ); /* :RECURSION: */
  177. else
  178. $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
  179. } else {
  180. $str = '';
  181. if (! $is_list)
  182. $str = '"' . $key . '":';
  183. //Custom handling for multiple data types
  184. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  185. $str .= $value; //Numbers
  186. elseif ($value === false)
  187. $str .= 'false'; //The booleans
  188. elseif ($value === true)
  189. $str .= 'true';
  190. else
  191. $str .= '"' . addslashes ( $value ) . '"'; //All other things
  192. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  193. $parts [] = $str;
  194. }
  195. }
  196. $json = implode ( ',', $parts );
  197. if ($is_list)
  198. return '[' . $json . ']'; //Return numerical JSON
  199. return '{' . $json . '}'; //Return associative JSON
  200. }
  201. /**
  202. * 获取签名
  203. * @param array $arrdata 签名数组
  204. * @param string $method 签名方法
  205. * @return boolean|string 签名值
  206. */
  207. public function getSignature($arrdata,$method="sha1") {
  208. if (!function_exists($method)) return false;
  209. ksort($arrdata);
  210. $paramstring = "";
  211. foreach($arrdata as $key => $value)
  212. {
  213. if(strlen($paramstring) == 0)
  214. $paramstring .= $key . "=" . $value;
  215. else
  216. $paramstring .= "&" . $key . "=" . $value;
  217. }
  218. $paySign = $method($paramstring);
  219. return $paySign;
  220. }
  221. /**
  222. * 生成随机字串
  223. * @param number $length 长度,默认为16,最长为32字节
  224. * @return string
  225. */
  226. public function generateNonceStr($length=16){
  227. // 密码字符集,可任意添加你需要的字符
  228. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  229. $str = "";
  230. for($i = 0; $i < $length; $i++)
  231. {
  232. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  233. }
  234. return $str;
  235. }
  236. /**
  237. * 生成原生支付url
  238. * @param number $productid 商品编号,最长为32字节
  239. * @return string
  240. */
  241. public function createNativeUrl($productid){
  242. $nativeObj["appid"] = $this->appid;
  243. $nativeObj["appkey"] = $this->paysignkey;
  244. $nativeObj["productid"] = urlencode($productid);
  245. $nativeObj["timestamp"] = time();
  246. $nativeObj["noncestr"] = $this->generateNonceStr();
  247. $nativeObj["sign"] = $this->getSignature($nativeObj);
  248. unset($nativeObj["appkey"]);
  249. $bizString = "";
  250. foreach($nativeObj as $key => $value)
  251. {
  252. if(strlen($bizString) == 0)
  253. $bizString .= $key . "=" . $value;
  254. else
  255. $bizString .= "&" . $key . "=" . $value;
  256. }
  257. return "weixin://wxpay/bizpayurl?".$bizString;
  258. //weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXXX&productid=XXXXXX&timestamp=XXXXXX&noncestr=XXXXXX
  259. }
  260. /**
  261. * 生成订单package字符串
  262. * @param string $out_trade_no 必填,商户系统内部的订单号,32个字符内,确保在商户系统唯一
  263. * @param string $body 必填,商品描述,128 字节以下
  264. * @param int $total_fee 必填,订单总金额,单位为分
  265. * @param string $notify_url 必填,支付完成通知回调接口,255 字节以内
  266. * @param string $spbill_create_ip 必填,用户终端IP,IPV4字串,15字节内
  267. * @param int $fee_type 必填,现金支付币种,默认1:人民币
  268. * @param string $bank_type 必填,银行通道类型,默认WX
  269. * @param string $input_charset 必填,传入参数字符编码,默认UTF-8,取值有UTF-8和GBK
  270. * @param string $time_start 交易起始时间,订单生成时间,格式yyyyMMddHHmmss
  271. * @param string $time_expire 交易结束时间,也是订单失效时间
  272. * @param int $transport_fee 物流费用,单位为分
  273. * @param int $product_fee 商品费用,单位为分,必须保证 transport_fee + product_fee=total_fee
  274. * @param string $goods_tag 商品标记,优惠券时可能用到
  275. * @param string $attach 附加数据,notify接口原样返回
  276. * @return string
  277. */
  278. public function createPackage($out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip,$fee_type=1,$bank_type="WX",$input_charset="UTF-8",$time_start="",$time_expire="",$transport_fee="",$product_fee="",$goods_tag="",$attach=""){
  279. $arrdata = array("bank_type" => $bank_type, "body" => $body, "partner" => $this->partnerid, "out_trade_no" => $out_trade_no, "total_fee" => $total_fee, "fee_type" => $fee_type, "notify_url" => $notify_url, "spbill_create_ip" => $spbill_create_ip, "input_charset" => $input_charset);
  280. if ($time_start) $arrdata['time_start'] = $time_start;
  281. if ($time_expire) $arrdata['time_expire'] = $time_expire;
  282. if ($transport_fee) $arrdata['transport_fee'] = $transport_fee;
  283. if ($product_fee) $arrdata['product_fee'] = $product_fee;
  284. if ($goods_tag) $arrdata['goods_tag'] = $goods_tag;
  285. if ($attach) $arrdata['attach'] = $attach;
  286. ksort($arrdata);
  287. $paramstring = "";
  288. foreach($arrdata as $key => $value)
  289. {
  290. if(strlen($paramstring) == 0)
  291. $paramstring .= $key . "=" . $value;
  292. else
  293. $paramstring .= "&" . $key . "=" . $value;
  294. }
  295. $stringSignTemp = $paramstring . "&key=" . $this->partnerkey;
  296. $signValue = strtoupper(md5($stringSignTemp));
  297. $package = http_build_query($arrdata) . "&sign=" . $signValue;
  298. return $package;
  299. }
  300. /**
  301. * 支付签名(paySign)生成方法
  302. * @param string $package 订单详情字串
  303. * @param string $timeStamp 当前时间戳(需与JS输出的一致)
  304. * @param string $nonceStr 随机串(需与JS输出的一致)
  305. * @return string 返回签名字串
  306. */
  307. public function getPaySign($package, $timeStamp, $nonceStr){
  308. $arrdata = array("appid" => $this->appid, "timestamp" => $timeStamp, "noncestr" => $nonceStr, "package" => $package, "appkey" => $this->paysignkey);
  309. $paySign = $this->getSignature($arrdata);
  310. return $paySign;
  311. }
  312. /**
  313. * 回调通知签名验证
  314. * @param array $orderxml 返回的orderXml的数组表示,留空则自动从post数据获取
  315. * @return boolean
  316. */
  317. public function checkOrderSignature($orderxml=''){
  318. if (!$orderxml) {
  319. $postStr = file_get_contents("php://input");
  320. if (!empty($postStr)) {
  321. $orderxml = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  322. } else return false;
  323. }
  324. $arrdata = array('appid'=>$orderxml['AppId'],'appkey'=>$this->paysignkey,'timestamp'=>$orderxml['TimeStamp'],'noncestr'=>$orderxml['NonceStr'],'openid'=>$orderxml['OpenId'],'issubscribe'=>$orderxml['IsSubscribe']);
  325. $paySign = $this->getSignature($arrdata);
  326. if ($paySign!=$orderxml['AppSignature']) return false;
  327. return true;
  328. }
  329. /**
  330. * 发货通知
  331. * @param string $openid 用户open_id
  332. * @param string $transid 交易单号
  333. * @param string $out_trade_no 第三方订单号
  334. * @param int $status 0:发货失败;1:已发货
  335. * @param string $msg 失败原因
  336. * @return boolean|array
  337. */
  338. public function sendPayDeliverNotify($openid,$transid,$out_trade_no,$status=1,$msg='ok'){
  339. if (!$this->access_token && !$this->checkAuth()) return false;
  340. $postdata = array(
  341. "appid"=>$this->appid,
  342. "appkey"=>$this->paysignkey,
  343. "openid"=>$openid,
  344. "transid"=>strval($transid),
  345. "out_trade_no"=>strval($out_trade_no),
  346. "deliver_timestamp"=>strval(time()),
  347. "deliver_status"=>strval($status),
  348. "deliver_msg"=>$msg,
  349. );
  350. $postdata['app_signature'] = $this->getSignature($postdata);
  351. $postdata['sign_method'] = 'sha1';
  352. unset($postdata['appkey']);
  353. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_DELIVERNOTIFY.'access_token='.$this->access_token,self::json_encode($postdata));
  354. if ($result)
  355. {
  356. $json = json_decode($result,true);
  357. if (!$json || !empty($json['errcode'])) {
  358. $this->errCode = $json['errcode'];
  359. $this->errMsg = $json['errmsg'];
  360. return false;
  361. }
  362. return $json;
  363. }
  364. return false;
  365. }
  366. /**
  367. * 查询订单信息
  368. * @param string $out_trade_no 订单号
  369. * @return boolean|array
  370. */
  371. public function getPayOrder($out_trade_no) {
  372. if (!$this->access_token && !$this->checkAuth()) return false;
  373. $sign = strtoupper(md5("out_trade_no=$out_trade_no&partner={$this->partnerid}&key={$this->partnerkey}"));
  374. $postdata = array(
  375. "appid"=>$this->appid,
  376. "appkey"=>$this->paysignkey,
  377. "package"=>"out_trade_no=$out_trade_no&partner={$this->partnerid}&sign=$sign",
  378. "timestamp"=>strval(time()),
  379. );
  380. $postdata['app_signature'] = $this->getSignature($postdata);
  381. $postdata['sign_method'] = 'sha1';
  382. unset($postdata['appkey']);
  383. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_ORDERQUERY.'access_token='.$this->access_token,self::json_encode($postdata));
  384. if ($result)
  385. {
  386. $json = json_decode($result,true);
  387. if (!$json || !empty($json['errcode'])) {
  388. $this->errCode = $json['errcode'];
  389. $this->errMsg = $json['errmsg'].json_encode($postdata);
  390. return false;
  391. }
  392. return $json["order_info"];
  393. }
  394. return false;
  395. }
  396. /**
  397. * 设置用户授权密钥
  398. * @param string $user_token
  399. * @return string
  400. */
  401. public function setUserToken($user_token) {
  402. return $this->user_token = $user_token;
  403. }
  404. /**
  405. * 获取收货地址JS的签名
  406. * @tutorial 参考weixin.js脚本的WeixinJS.editAddress方法调用
  407. * @param string $appId
  408. * @param string $url
  409. * @param int $timeStamp
  410. * @param string $nonceStr
  411. * @param string $user_token
  412. * @return Ambigous <boolean, string>
  413. */
  414. public function getAddrSign($url, $timeStamp, $nonceStr, $user_token=''){
  415. if (!$user_token) $user_token = $this->user_token;
  416. if (!$user_token) {
  417. $this->errMsg = 'no user access token found!';
  418. return false;
  419. }
  420. $url = htmlspecialchars_decode($url);
  421. $arrdata = array(
  422. 'appid'=>$this->appid,
  423. 'url'=>$url,
  424. 'timestamp'=>strval($timeStamp),
  425. 'noncestr'=>$nonceStr,
  426. 'accesstoken'=>$user_token
  427. );
  428. return $this->getSignature($arrdata);
  429. }
  430. }