Yspay.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. #银联商务正常交易
  4. class Yspay extends Core
  5. {
  6. public function __construct($config)
  7. {
  8. $project = Dever::project('pay');
  9. # 通知接口
  10. $config['notify'] = $this->url($config['type'], $config['id']);
  11. $set = explode('|', $config['mchid']);
  12. $config['mid'] = $set[0];
  13. $config['tid'] = $set[1];
  14. if (isset($set[2])) {
  15. $config['prefix'] = $set[2];
  16. }
  17. $set = explode('|', $config['key']);
  18. $config['sub_appid'] = $set[0];
  19. $config['sub_appsecret'] = $set[1];
  20. if (isset($set[2])) {
  21. $config['key'] = $set[2];
  22. }
  23. $this->config = $config;
  24. }
  25. /**
  26. * 通知
  27. */
  28. public function notify()
  29. {
  30. $input = file_get_contents("php://input");
  31. if ($input) {
  32. parse_str($input, $input);
  33. } else {
  34. $input = Dever::input();
  35. }
  36. $test = Dever::input('test');
  37. if ($test == 1) {
  38. $input = json_decode('{"msgType":"wx.notify","payTime":"2021-12-29 18:24:42","buyerCashPayAmt":"1","connectSys":"UNIONPAY","sign":"E82978003E0121B320D3520B1930FB4C2072B639126F3427351631F1D0A998CF","merName":"中食民安(北京)科技有限公司","mid":"89810007372106N","invoiceAmount":"1","settleDate":"2021-12-29","billFunds":"现金:1","buyerId":"otdJ_uD-zTNmDn7-u13oActXu8lA","mchntUuid":"2d9081bd7db8ad75017dbbe68981314f","tid":"DM098308","instMid":"MINIDEFAULT","receiptAmount":"1","couponAmount":"0","targetOrderId":"4200001344202112290882463274","signType":"SHA256","billFundsDesc":"现金支付0.01元。","subBuyerId":"oGlMn5e_vdYK8uAaCgyexarFKBrY","orderDesc":"中食民安(北京)科技有限公司","seqId":"25267273719N","merOrderId":"138K_G202112297347788956443487","targetSys":"WXPay","bankInfo":"OTHERS","totalAmount":"1","createTime":"2021-12-29 18:24:38","buyerPayAmount":"1","iB":"KRHn","notifyId":"d4122965-b772-4f8f-b5c4-f3d483da0962","subInst":"100000","status":"TRADE_SUCCESS"}', true);
  39. $input = json_decode('{"bankInfo":"OTHERS","billDate":"2022-01-06","billDesc":"中食民安(北京)科技有限公司","billNo":"138KG202201064666839470673943","billPayment":"{\"payTime\":\"2022-01-06 13:24:56\",\"buyerCashPayAmt\":1,\"paySeqId\":\"25394941600N\",\"invoiceAmount\":1,\"settleDate\":\"2022-01-06\",\"buyerId\":\"otdJ_uHh75dIaI6EKu-6usw0hjD4\",\"receiptAmount\":1,\"totalAmount\":1,\"couponAmount\":0,\"billBizType\":\"bills\",\"buyerPayAmount\":1,\"targetOrderId\":\"4200001348202201065614123115\",\"payDetail\":\"现金支付0.01元。\",\"merOrderId\":\"138KG2022010646668394706739430\",\"status\":\"TRADE_SUCCESS\",\"targetSys\":\"WXPay\"}","billQRCode":"https:\/\/qr.95516.com\/48020000\/138K2201066502428138291294","billStatus":"PAID","createTime":"2022-01-06 13:24:28","instMid":"QRPAYDEFAULT","mchntUuid":"2d9081bd7db8ad75017dbbe68981314f","merName":"中食民安(北京)科技有限公司","mid":"89810007372106N","notifyId":"49a83755-b476-47c3-a0b6-629dc7a43742","qrCodeId":"138K2201066502428138291294","qrCodeType":"BILLPAY","receiptAmount":"1","seqId":"25394941600N","signType":"SHA256","subInst":"100000","tid":"DM098308","totalAmount":"1","vE":"Bclv","sign":"1D048E292E17225A4EEF4AB438630601A19E50CDA71DF9E7A4304AABBD134A38","e_sign":"1D048E292E17225A4EEF4AB438630601A19E50CDA71DF9E7A4304AABBD134A38"}', true);
  40. $input = json_decode('{"billDate":"2023-08-08","billDesc":"北京正力中食科技有限公司","billNo":"138KC2023080864622010717928","billPayment":"{\"buyerUsername\":\"bin***@sina.com\",\"payTime\":\"2023-08-08 11:17:29\",\"buyerCashPayAmt\":1,\"connectSys\":\"UNIONPAY\",\"paySeqId\":\"35701309460N\",\"invoiceAmount\":1,\"settleDate\":\"2023-08-08\",\"buyerId\":\"2088202281488124\",\"receiptAmount\":1,\"totalAmount\":1,\"couponAmount\":0,\"billBizType\":\"bills\",\"buyerPayAmount\":1,\"targetOrderId\":\"2023080822001488121426563975\",\"payDetail\":\"支付宝余额支付0.01元。\",\"merOrderId\":\"138KC20230808646220107179280\",\"status\":\"TRADE_SUCCESS\",\"targetSys\":\"Alipay 2.0\"}","billQRCode":"https:\/\/qr.95516.com\/48020000\/138K2308081881702119606249","billStatus":"PAID","cardAttr":"BALANCE","createTime":"2023-08-08 11:17:02","instMid":"QRPAYDEFAULT","li":"MRBR","mchntUuid":"2d9081bc879e5394018808c18bd75086","merName":"北京正力中食科技有限公司","mid":"89810005331AU6B","notifyId":"74c0985b-3071-4d71-9b11-a44573929371","qrCodeId":"138K2308081881702119606249","qrCodeType":"BILLPAY","receiptAmount":"1","seqId":"35701309460N","signType":"SHA256","subInst":"100000","tid":"DM760642","totalAmount":"1","sign":"3C2F3BB6C5F5BA20DEFC8B282C988ECA1CBFDAC7920B2AAE04B423EBA95BC0A2","e_sign":"70123DF2F9911DD4EEC408C11F33C29C53F4C49E2AD628FD0365AE146654CFD8"}')
  41. }
  42. $sign = $input['sign'];
  43. unset($input['sign']);
  44. if (isset($input['e_sign'])) {
  45. $sign = $input['e_sign'];
  46. unset($input['e_sign']);
  47. }
  48. ksort($input);
  49. $string = array();
  50. foreach ($input as $k => $v) {
  51. if (strstr($v, '&') || strstr($v, '@')) {
  52. $v = urlencode($v);
  53. }
  54. $string[] = $k . '=' . $v;
  55. }
  56. $string = implode('&', $string);
  57. $string .= $this->config['key'];
  58. if ($input['signType'] == 'SHA256') {
  59. $string = hash("sha256", $string);
  60. } else {
  61. $string = md5($string);
  62. }
  63. $string = strtoupper($string);
  64. $input['sign'] = $sign;
  65. $input['e_sign'] = $string;
  66. $this->log('支付回调-初始化', $input);
  67. if ($sign == $string) {
  68. if (isset($input['billNo']) && $input['billNo']) {
  69. $key = 'billNo';
  70. } else {
  71. $key = 'merOrderId';
  72. }
  73. if (isset($this->config['prefix']) && $this->config['prefix']) {
  74. $input[$key] = str_replace($this->config['prefix'], '', $input[$key]);
  75. }
  76. if (isset($input['billPayment'])) {
  77. $input['billPayment'] = json_decode($input['billPayment'], true);
  78. if (isset($input['billPayment']['status'])) {
  79. $input['status'] = $input['billPayment']['status'];
  80. }
  81. }
  82. if (!isset($input['status'])) {
  83. echo 'FAILED';die;
  84. }
  85. $desc = '';
  86. if ($input['status'] == 'TRADE_SUCCESS') {
  87. # 成功
  88. } elseif ($input['status'] == 'TRADE_REFUND') {
  89. # 退款
  90. echo 'SUCCESS';die;
  91. } elseif ($input['status'] == 'TRADE_CLOSED') {
  92. $desc = '交易关闭';
  93. } elseif ($input['status'] == 'UNKNOWN') {
  94. $desc = '不明确的交易状态';
  95. } else {
  96. echo 'FAILED';die;
  97. }
  98. $this->updateOrder($input[$key], $input['totalAmount']);
  99. echo 'SUCCESS';die;
  100. } else {
  101. echo 'FAILED';die;
  102. }
  103. }
  104. private function getType($type)
  105. {
  106. switch ($type) {
  107. case 1:
  108. //小程序支付
  109. $type = 'MINI';
  110. break;
  111. case 2://扫码支付
  112. $type = 'APP';
  113. break;
  114. case 3://APP支付
  115. $type = 'APP';
  116. break;
  117. case 4://页面拉起支付
  118. $type = 'YUEDAN';
  119. break;
  120. }
  121. return $type;
  122. }
  123. /**
  124. * 获取统一下单的基本信息
  125. */
  126. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false, $refer = false)
  127. {
  128. $trade_type = $this->getType($type);
  129. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  130. $prefix = '';
  131. if (isset($this->config['prefix']) && $this->config['prefix']) {
  132. $prefix = $this->config['prefix'];
  133. }
  134. $request['merOrderId'] = $prefix . $order_id;
  135. $request['mid'] = $this->config['mid'];
  136. $request['tid'] = $this->config['tid'];
  137. $request['instMid'] = $trade_type . 'DEFAULT';
  138. $request['totalAmount'] = round($cash * 100, 2);
  139. //$request['totalAmount'] = 100;
  140. $request['subAppId'] = $this->config['sub_appid'];
  141. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  142. $request['expireTime'] = date("Y-m-d H:i:s", time() + $this->config['timeout']);
  143. $request['notifyUrl'] = $this->config['notify'];
  144. $request['tradeType'] = $trade_type;
  145. if ($other) {
  146. if (!isset($other[0])) {
  147. $other = array($other);
  148. }
  149. $request['originalAmount'] = $request['totalAmount'];
  150. $request['platformAmount'] = $request['totalAmount'];
  151. $request['subOrders'] = array();
  152. foreach ($other as $k => $v) {
  153. $subOrders = array();
  154. if (isset($v['amount']) && $v['amount']) {
  155. $subOrders['totalAmount'] = round($v['amount'] * 100, 2);
  156. $request['platformAmount'] = round($request['platformAmount'] - $subOrders['totalAmount'], 2);
  157. } elseif (isset($v['per']) && $v['per']) {
  158. # 此处为兼容历史业务代码
  159. $v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
  160. $v['per'] = $v['per']/100;
  161. $request['platformAmount'] = round($request['totalAmount'] * $v['per'], 2);
  162. $subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
  163. break;
  164. } else {
  165. continue;
  166. }
  167. $subOrders['mid'] = $v['mid'];
  168. $subOrders['merOrderId'] = $prefix . $v['order_id'];
  169. $request['subOrders'][] = $subOrders;
  170. $fenzhang = 0;
  171. if (isset($v['fenzhang']) && $v['fenzhang'] > 0) {
  172. $fenzhang = $v['fenzhang'];
  173. }
  174. if (isset($v['merchant_id']) && $v['merchant_id']) {
  175. Dever::load('pay/yspay/cash')->add($v['merchant_id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id'], $fenzhang);
  176. }
  177. }
  178. if ($request['subOrders']) {
  179. $request['divisionFlag'] = true;
  180. if ($request['platformAmount'] > 0) {
  181. $merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id']);
  182. if ($merchant) {
  183. $subOrders = array
  184. (
  185. 'totalAmount' => $request['platformAmount'],
  186. 'mid' => $merchant['mid'],
  187. 'merOrderId' => $request['merOrderId'] . '_O',
  188. );
  189. $request['subOrders'][] = $subOrders;
  190. $request['platformAmount'] = 0;
  191. Dever::load('pay/yspay/cash')->add($merchant['id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id']);
  192. }
  193. }
  194. }
  195. }
  196. if (isset($request['divisionFlag']) && $request['divisionFlag']) {
  197. } else {
  198. $merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id']);
  199. if ($merchant) {
  200. $request['platformAmount'] = 0;
  201. Dever::load('pay/yspay/cash')->add($merchant['id'], $request['totalAmount'], $request['merOrderId'], $order_id);
  202. }
  203. }
  204. if (!$openid) {
  205. # 测试的openid
  206. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  207. } else {
  208. $request['subOpenId'] = $openid;
  209. }
  210. if ($type == 2) {
  211. # 二维码支付
  212. $request['instMid'] = 'QRPAYDEFAULT';
  213. $request['billNo'] = $request['merOrderId'];
  214. unset($request['merOrderId']);
  215. $result = Base::get_pay_code($request, $this->config);
  216. if (isset($result['billQRCode'])) {
  217. $result['request'] = $request;
  218. $result['payMsg'] = $result['billQRCode'];
  219. if ($other) {
  220. $result['other'] = $other;
  221. }
  222. $this->updateOrderParam($order_id, $result);
  223. return $result['payMsg'];
  224. }
  225. } elseif ($type == 1) {
  226. # 小程序支付
  227. $result = Base::pay($request, $this->config);
  228. if (isset($result['miniPayRequest'])) {
  229. $result['request'] = $request;
  230. $result['payMsg'] = $result['miniPayRequest'];
  231. unset($result['miniPayRequest']);
  232. if ($other) {
  233. $result['other'] = $other;
  234. }
  235. $this->updateOrderParam($order_id, $result);
  236. return $result['payMsg'];
  237. }
  238. } elseif ($type == 4) {
  239. if ($refer) {
  240. $request['returnUrl'] = $refer;
  241. }
  242. $result = Base::h5_pay($request, $this->config);
  243. return $result;
  244. }
  245. return false;
  246. }
  247. # 退款
  248. public function refund($order_id, $cash, $order, $refund_order_id = false)
  249. {
  250. if (isset($this->config['prefix']) && $this->config['prefix']) {
  251. $request['merOrderId'] = $this->config['prefix'] . $order_id;
  252. if ($refund_order_id) {
  253. $request['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  254. }
  255. } else {
  256. $request['merOrderId'] = $order_id;
  257. if ($refund_order_id) {
  258. $request['refundOrderId'] = $refund_order_id;
  259. }
  260. }
  261. $request['mid'] = $this->config['mid'];
  262. $request['tid'] = $this->config['tid'];
  263. $request['instMid'] = 'MINIDEFAULT';
  264. if (isset($order['param']['targetOrderId'])) {
  265. $request['targetOrderId'] = $order['param']['targetOrderId'];
  266. }
  267. $request['subAppId'] = $this->config['appid'];
  268. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  269. $request['refundAmount'] = round($cash * 100, 2);
  270. if (isset($order['param']['other']) && $order['param']['other']) {
  271. $other = $order['param']['other'];
  272. if (!isset($other[0])) {
  273. $other = array($other);
  274. }
  275. $request['platformAmount'] = 0;
  276. $request['subOrders'] = array();
  277. foreach ($other as $k => $v) {
  278. $subOrders = array();
  279. if (isset($v['amount']) && $v['amount']) {
  280. $subOrders['totalAmount'] = round($v['amount'] * 100, 2);
  281. } elseif (isset($v['per']) && $v['per']) {
  282. $v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
  283. $v['per'] = $v['per']/100;
  284. $request['platformAmount'] = round($request['refundAmount'] * $v['per'], 2);
  285. $subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
  286. } else {
  287. continue;
  288. }
  289. $subOrders['mid'] = $v['mid'];
  290. if (isset($this->config['prefix']) && $this->config['prefix']) {
  291. $subOrders['merOrderId'] = $this->config['prefix'] . $v['order_id'];
  292. if ($refund_order_id) {
  293. $subOrders['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  294. }
  295. } else {
  296. $subOrders['merOrderId'] = $v['order_id'];
  297. if ($refund_order_id) {
  298. $subOrders['refundOrderId'] = $refund_order_id;
  299. }
  300. }
  301. $request['subOrders'][] = $subOrders;
  302. }
  303. }
  304. $account = Dever::db('pay/account')->find($order['account_id']);
  305. $method = 'refund';
  306. if ($account && $account['system_source'] == 4) {
  307. $method = 'code_refund';
  308. $request['billNo'] = $request['merOrderId'];
  309. $request['instMid'] = 'QRPAYDEFAULT';
  310. $request['billDate'] = date('Y-m-d');
  311. }
  312. $result = Base::$method($request, $this->config);
  313. if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
  314. return true;
  315. }
  316. return false;
  317. }
  318. # 查询
  319. public function query($order_id)
  320. {
  321. $request['merOrderId'] = $order_id;
  322. $request['mid'] = $this->config['mchid'];
  323. $request['tid'] = $this->config['key'];
  324. $request['instMid'] = 'MINIDEFAULT';
  325. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  326. $result = Base::refund($request, $this->config);
  327. return $result;
  328. }
  329. # 关闭订单
  330. public function close($order_id)
  331. {
  332. $request['merOrderId'] = $order_id;
  333. $request['mid'] = $this->config['mchid'];
  334. $request['tid'] = $this->config['key'];
  335. $request['instMid'] = 'MINIDEFAULT';
  336. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  337. $result = Base::close($request, $this->config);
  338. return $result;
  339. }
  340. /**
  341. * 获取二维码支付
  342. */
  343. public function qrcode($order, $refer)
  344. {
  345. $result['type'] = 'qrcode';
  346. $result['url'] = $order;
  347. return $result;
  348. }
  349. /**
  350. * 获取小程序支付
  351. */
  352. public function applet($order)
  353. {
  354. $result = array();
  355. if (isset($order['package'])) {
  356. $prepay_id = str_replace('prepay_id=', '', $order['package']);
  357. $result['time'] = $order['timeStamp'];
  358. $result['nonce_str'] = $order['nonceStr'];
  359. $result['prepay_id'] = $prepay_id;
  360. $result['sign_type'] = $order['signType'];
  361. $result['sign'] = $order['paySign'];
  362. $result['type'] = 'applet';
  363. }
  364. return $result;
  365. }
  366. /**
  367. * 获取页面支付 去收银台
  368. */
  369. public function page($order, $refer)
  370. {
  371. return $order;
  372. }
  373. }
  374. class Base
  375. {
  376. static $test_host = 'https://test-api-open.chinaums.com/';
  377. static $host = 'https://api-mop.chinaums.com/';
  378. static $signMethod = 'SHA256'; //签名方式
  379. # 获取token
  380. static $token_url = 'v1/token/access';
  381. # 微信下单支付
  382. static $pay_wechat_url = 'v1/netpay/wx/unified-order';
  383. # 支付宝下单支付
  384. static $pay_alipay_url = 'v1/netpay/trade/create';
  385. # 云闪付下单支付
  386. static $pay_uac_url = 'v1/netpay/uac/mini-order';
  387. # 交易查询
  388. static $query_url = 'v1/netpay/query';
  389. # 退款
  390. static $refund_url = 'v1/netpay/refund';
  391. # 退款查询
  392. static $refund_query_url = 'v1/netpay/refund-query';
  393. # 订单关闭
  394. static $close_url = 'v1/netpay/close';
  395. # 二维码支付
  396. static $qrcode_url = 'v1/netpay/bills/get-qrcode';
  397. # 二维码支付退款
  398. static $qrcode_refund_url = 'v1/netpay/bills/refund';
  399. # h5支付
  400. static $h5_url = 'v1/netpay/webpay/pay';
  401. # h5支付退款
  402. static $h5_refund_url = 'v1/netpay/wx/app-pre-order';
  403. //===================== 支付相关 ==============================
  404. /**
  405. * 支付交易
  406. */
  407. static public function pay($param, $config)
  408. {
  409. $url = self::$pay_wechat_url;
  410. $result = self::get($url, $param, $config);
  411. return $result;
  412. }
  413. /**
  414. * h5支付
  415. */
  416. static public function h5_pay($param, $config)
  417. {
  418. $url = self::$h5_url;
  419. $result = self::get($url, $param, $config);
  420. return $result;
  421. }
  422. /**
  423. * 获取支付二维码
  424. */
  425. static public function get_pay_code($param, $config)
  426. {
  427. $url = self::$qrcode_url;
  428. $result = self::get($url, $param, $config);
  429. return $result;
  430. }
  431. /**
  432. * 二维码交易退款
  433. */
  434. static public function code_refund($param, $config)
  435. {
  436. $url = self::$qrcode_refund_url;
  437. $result = self::get($url, $param, $config);
  438. return $result;
  439. }
  440. /**
  441. * 支付撤销
  442. */
  443. static public function close()
  444. {
  445. $url = self::$close_url;
  446. $result = self::get($url, $param, $config);
  447. return $result;
  448. }
  449. /**
  450. * 交易退款
  451. */
  452. static public function refund($param, $config)
  453. {
  454. $url = self::$refund_url;
  455. $result = self::get($url, $param, $config, false);
  456. return $result;
  457. }
  458. /**
  459. * 交易查询
  460. */
  461. static public function query()
  462. {
  463. $url = self::$query_url;
  464. $result = self::get($url, $param, $config);
  465. return $result;
  466. }
  467. /**
  468. * 交易退款查询
  469. */
  470. static public function refundQuery()
  471. {
  472. $url = self::$refund_query_url;
  473. $result = self::get($url, $param, $config);
  474. return $result;
  475. }
  476. //====================== 获取调用凭证===========================
  477. /**
  478. * 通用调用凭证获取
  479. * 默认使用token方式,使用签名方式需要传入参数
  480. */
  481. static public function getAuth($type = 'token', $param, $config)
  482. {
  483. if ($type === 'token') {
  484. return self::getAccessTokenByToken($config);
  485. } elseif ($type === 'form') {
  486. return self::getAccessTokenByForm($param, $config);
  487. }
  488. return self::getAccessTokenBySig($param, $config);
  489. }
  490. /**
  491. * 方式一,OPEN-ACCESS-TOKEN方式
  492. */
  493. static function getAccessTokenByToken($config)
  494. {
  495. $param = [
  496. 'appId' => $config['appid'],
  497. 'timestamp' => date('YmdHis'),
  498. 'nonce' => md5(uniqid(microtime(true),true)),
  499. 'signMethod' => self::$signMethod,
  500. ];
  501. $param['signature'] = self::signature($param, $config['appsecret']);
  502. $result = self::get(self::$token_url, $param, $config);
  503. if (isset($result['errCode']) && isset($result['accessToken'])) {
  504. return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
  505. }
  506. return '';
  507. }
  508. /**
  509. * 方式二,OPEN-BODY-SIG方式
  510. */
  511. static function getAccessTokenBySig($data, $config)
  512. {
  513. $param = [
  514. 'AppId' => $config['appid'],
  515. 'Timestamp' => date('YmdHis'),
  516. 'Nonce' => md5(uniqid(microtime(true),true))
  517. ];
  518. return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
  519. }
  520. /**
  521. * 方式三,OPEN-FORM-PARAM方式
  522. */
  523. static function getAccessTokenByForm($data, $config)
  524. {
  525. $content = Dever::json_encode($data);
  526. $param = [
  527. 'appId' => $config['appid'],
  528. 'timestamp' => date('YmdHis'),
  529. 'nonce' => md5(uniqid(microtime(true),true)),
  530. 'authorization' => 'OPEN-FORM-PARAM',
  531. 'content' => $content,
  532. ];
  533. $param['signature'] = urlencode(self::signature3($param, $config['appsecret']));
  534. $param['content'] = urlencode($param['content']);
  535. return $param;
  536. }
  537. /**
  538. * 计算签名,方式一
  539. */
  540. static function signature($param, $appsecret)
  541. {
  542. return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
  543. }
  544. /**
  545. * 计算签名,方式二
  546. */
  547. static function signature2($data, $param, $appsecret)
  548. {
  549. $str = bin2hex(hash('sha256', Dever::json_encode($data), true));
  550. return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
  551. }
  552. /**
  553. * 计算签名,方式三
  554. */
  555. static function signature3($param, $appsecret)
  556. {
  557. $str = bin2hex(hash('sha256', $param['content'], true));
  558. return base64_encode(hash_hmac('sha256', $param['appId'].$param['timestamp'].$param['nonce'].$str, $appsecret, true));
  559. }
  560. /**
  561. * 获取信息
  562. */
  563. static function get($url, $param, $config, $state = true)
  564. {
  565. $url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
  566. if (strstr($url, 'webpay')) {
  567. $url .= '?';
  568. $param = self::getAuth('form', $param, $config);
  569. foreach ($param as $k => $v) {
  570. $url .= '&' . $k . '=' . $v;
  571. }
  572. $url = str_replace('?&', '?', $url);
  573. return $url;
  574. } else {
  575. $header = array();
  576. $header['Authorization'] = self::getAuth('sig', $param, $config);
  577. $result = Dever::curl($url, $param, 'post', true, $header);
  578. }
  579. if (strstr($result, '<html><head>')) {
  580. Dever::alert('系统错误');
  581. }
  582. $result = Dever::json_decode($result);
  583. if (isset($result['errCode'])) {
  584. if (!$state) {
  585. return $result;
  586. }
  587. if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
  588. return $result;
  589. } elseif (isset($result['errInfo'])) {
  590. //Dever::alert('操作失败,请联系管理员');
  591. Dever::alert($result['errInfo']);
  592. } elseif (isset($result['errMsg'])) {
  593. //Dever::alert('操作失败,请联系管理员');
  594. Dever::alert($result['errMsg']);
  595. }
  596. } else {
  597. Dever::alert('系统错误');
  598. }
  599. return $result;
  600. }
  601. }