Yspay.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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"}', true);
  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. $yspay_main_id = 1;
  150. if (isset($other[0]['yspay_main_id'])) {
  151. $yspay_main_id = $other[0]['yspay_main_id'];
  152. }
  153. $request['originalAmount'] = $request['totalAmount'];
  154. $request['platformAmount'] = $request['totalAmount'];
  155. $request['subOrders'] = array();
  156. foreach ($other as $k => $v) {
  157. $subOrders = array();
  158. if (isset($v['amount']) && $v['amount']) {
  159. $subOrders['totalAmount'] = round($v['amount'] * 100, 2);
  160. $request['platformAmount'] = round($request['platformAmount'] - $subOrders['totalAmount'], 2);
  161. } elseif (isset($v['per'])) {
  162. # 此处为兼容历史业务代码
  163. $v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
  164. $v['per'] = $v['per']/100;
  165. $request['platformAmount'] = round($request['totalAmount'] * $v['per'], 2);
  166. $subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
  167. break;
  168. } else {
  169. continue;
  170. }
  171. $subOrders['mid'] = $v['mid'];
  172. $subOrders['merOrderId'] = $prefix . $v['order_id'];
  173. $request['subOrders'][] = $subOrders;
  174. $fenzhang = 0;
  175. if (isset($v['fenzhang']) && $v['fenzhang'] > 0) {
  176. $fenzhang = $v['fenzhang'];
  177. }
  178. if (isset($v['merchant_id']) && $v['merchant_id']) {
  179. Dever::load('pay/yspay/cash')->add($v['merchant_id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id'], $fenzhang);
  180. }
  181. }
  182. if ($request['subOrders']) {
  183. $request['divisionFlag'] = true;
  184. if ($request['platformAmount'] > 0) {
  185. $merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id'], $yspay_main_id);
  186. if ($merchant) {
  187. $subOrders = array
  188. (
  189. 'totalAmount' => $request['platformAmount'],
  190. 'mid' => $merchant['mid'],
  191. 'merOrderId' => $request['merOrderId'] . '_O',
  192. );
  193. $request['subOrders'][] = $subOrders;
  194. $request['platformAmount'] = 0;
  195. Dever::load('pay/yspay/cash')->add($merchant['id'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id']);
  196. }
  197. }
  198. }
  199. }
  200. /* 已废弃,根据前台逻辑进行分账吧,否则都支付给主商户
  201. if (isset($request['divisionFlag']) && $request['divisionFlag']) {
  202. } else {
  203. $merchant = Dever::load('pay/yspay/cash')->getMid($this->config['id']);
  204. if ($merchant) {
  205. $request['platformAmount'] = 0;
  206. Dever::load('pay/yspay/cash')->add($merchant['id'], $request['totalAmount'], $request['merOrderId'], $order_id);
  207. }
  208. }*/
  209. if (!$openid) {
  210. # 测试的openid
  211. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  212. } else {
  213. $request['subOpenId'] = $openid;
  214. }
  215. if ($type == 2) {
  216. # 二维码支付
  217. $request['instMid'] = 'QRPAYDEFAULT';
  218. $request['billNo'] = $request['merOrderId'];
  219. unset($request['merOrderId']);
  220. $result = Base::get_pay_code($request, $this->config);
  221. if (isset($result['billQRCode'])) {
  222. $result['request'] = $request;
  223. $result['payMsg'] = $result['billQRCode'];
  224. if ($other) {
  225. $result['other'] = $other;
  226. }
  227. $this->updateOrderParam($order_id, $result);
  228. return $result['payMsg'];
  229. }
  230. } elseif ($type == 1) {
  231. # 小程序支付
  232. $result = Base::pay($request, $this->config);
  233. if (isset($result['miniPayRequest'])) {
  234. $result['request'] = $request;
  235. $result['payMsg'] = $result['miniPayRequest'];
  236. unset($result['miniPayRequest']);
  237. if ($other) {
  238. $result['other'] = $other;
  239. }
  240. $this->updateOrderParam($order_id, $result);
  241. return $result['payMsg'];
  242. }
  243. } elseif ($type == 4) {
  244. if ($refer) {
  245. $request['returnUrl'] = $refer;
  246. }
  247. $result = Base::h5_pay($request, $this->config);
  248. return $result;
  249. }
  250. return false;
  251. }
  252. # 退款
  253. public function refund($order_id, $cash, $order, $refund_order_id = false)
  254. {
  255. if (isset($this->config['prefix']) && $this->config['prefix']) {
  256. $request['merOrderId'] = $this->config['prefix'] . $order_id;
  257. if ($refund_order_id) {
  258. $request['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  259. }
  260. } else {
  261. $request['merOrderId'] = $order_id;
  262. if ($refund_order_id) {
  263. $request['refundOrderId'] = $refund_order_id;
  264. }
  265. }
  266. $request['mid'] = $this->config['mid'];
  267. $request['tid'] = $this->config['tid'];
  268. $request['instMid'] = 'MINIDEFAULT';
  269. if (isset($order['param']['targetOrderId'])) {
  270. $request['targetOrderId'] = $order['param']['targetOrderId'];
  271. }
  272. $request['subAppId'] = $this->config['appid'];
  273. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  274. $request['refundAmount'] = round($cash * 100, 2);
  275. if (isset($order['param']['other']) && $order['param']['other']) {
  276. $other = $order['param']['other'];
  277. if (!isset($other[0])) {
  278. $other = array($other);
  279. }
  280. $request['platformAmount'] = 0;
  281. $request['subOrders'] = array();
  282. foreach ($other as $k => $v) {
  283. $subOrders = array();
  284. if (isset($v['amount']) && $v['amount']) {
  285. $subOrders['totalAmount'] = round($v['amount'] * 100, 2);
  286. } elseif (isset($v['per'])) {
  287. $v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
  288. $v['per'] = $v['per']/100;
  289. $request['platformAmount'] = round($request['refundAmount'] * $v['per'], 2);
  290. $subOrders['totalAmount'] = round($request['refundAmount'] - $request['platformAmount'], 2);
  291. } else {
  292. continue;
  293. }
  294. $subOrders['mid'] = $v['mid'];
  295. if (isset($this->config['prefix']) && $this->config['prefix']) {
  296. $subOrders['merOrderId'] = $this->config['prefix'] . $v['order_id'];
  297. if ($refund_order_id) {
  298. $subOrders['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  299. }
  300. } else {
  301. $subOrders['merOrderId'] = $v['order_id'];
  302. if ($refund_order_id) {
  303. $subOrders['refundOrderId'] = $refund_order_id;
  304. }
  305. }
  306. $request['subOrders'][] = $subOrders;
  307. }
  308. }
  309. $account = Dever::db('pay/account')->find($order['account_id']);
  310. $method = 'refund';
  311. if ($account && $account['system_source'] == 4) {
  312. $method = 'code_refund';
  313. $request['billNo'] = $request['merOrderId'];
  314. $request['instMid'] = 'QRPAYDEFAULT';
  315. $request['billDate'] = date('Y-m-d');
  316. }
  317. $result = Base::$method($request, $this->config);
  318. if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
  319. return true;
  320. }
  321. return false;
  322. }
  323. # 查询
  324. public function query($order_id)
  325. {
  326. $request['merOrderId'] = $order_id;
  327. $request['mid'] = $this->config['mchid'];
  328. $request['tid'] = $this->config['key'];
  329. $request['instMid'] = 'MINIDEFAULT';
  330. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  331. $result = Base::refund($request, $this->config);
  332. return $result;
  333. }
  334. # 关闭订单
  335. public function close($order_id)
  336. {
  337. $request['merOrderId'] = $order_id;
  338. $request['mid'] = $this->config['mchid'];
  339. $request['tid'] = $this->config['key'];
  340. $request['instMid'] = 'MINIDEFAULT';
  341. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  342. $result = Base::close($request, $this->config);
  343. return $result;
  344. }
  345. /**
  346. * 获取二维码支付
  347. */
  348. public function qrcode($order, $refer)
  349. {
  350. $result['type'] = 'qrcode';
  351. $result['url'] = $order;
  352. return $result;
  353. }
  354. /**
  355. * 获取小程序支付
  356. */
  357. public function applet($order)
  358. {
  359. $result = array();
  360. if (isset($order['package'])) {
  361. $prepay_id = str_replace('prepay_id=', '', $order['package']);
  362. $result['time'] = $order['timeStamp'];
  363. $result['nonce_str'] = $order['nonceStr'];
  364. $result['prepay_id'] = $prepay_id;
  365. $result['sign_type'] = $order['signType'];
  366. $result['sign'] = $order['paySign'];
  367. $result['type'] = 'applet';
  368. }
  369. return $result;
  370. }
  371. /**
  372. * 获取页面支付 去收银台
  373. */
  374. public function page($order, $refer)
  375. {
  376. return $order;
  377. }
  378. }
  379. class Base
  380. {
  381. static $test_host = 'https://test-api-open.chinaums.com/';
  382. static $host = 'https://api-mop.chinaums.com/';
  383. static $signMethod = 'SHA256'; //签名方式
  384. # 获取token
  385. static $token_url = 'v1/token/access';
  386. # 微信下单支付
  387. static $pay_wechat_url = 'v1/netpay/wx/unified-order';
  388. # 支付宝下单支付
  389. static $pay_alipay_url = 'v1/netpay/trade/create';
  390. # 云闪付下单支付
  391. static $pay_uac_url = 'v1/netpay/uac/mini-order';
  392. # 交易查询
  393. static $query_url = 'v1/netpay/query';
  394. # 退款
  395. static $refund_url = 'v1/netpay/refund';
  396. # 退款查询
  397. static $refund_query_url = 'v1/netpay/refund-query';
  398. # 订单关闭
  399. static $close_url = 'v1/netpay/close';
  400. # 二维码支付
  401. static $qrcode_url = 'v1/netpay/bills/get-qrcode';
  402. # 二维码支付退款
  403. static $qrcode_refund_url = 'v1/netpay/bills/refund';
  404. # h5支付
  405. static $h5_url = 'v1/netpay/webpay/pay';
  406. # h5支付退款
  407. static $h5_refund_url = 'v1/netpay/wx/app-pre-order';
  408. //===================== 支付相关 ==============================
  409. /**
  410. * 支付交易
  411. */
  412. static public function pay($param, $config)
  413. {
  414. $url = self::$pay_wechat_url;
  415. $result = self::get($url, $param, $config);
  416. return $result;
  417. }
  418. /**
  419. * h5支付
  420. */
  421. static public function h5_pay($param, $config)
  422. {
  423. $url = self::$h5_url;
  424. $result = self::get($url, $param, $config);
  425. return $result;
  426. }
  427. /**
  428. * 获取支付二维码
  429. */
  430. static public function get_pay_code($param, $config)
  431. {
  432. $url = self::$qrcode_url;
  433. $result = self::get($url, $param, $config);
  434. return $result;
  435. }
  436. /**
  437. * 二维码交易退款
  438. */
  439. static public function code_refund($param, $config)
  440. {
  441. $url = self::$qrcode_refund_url;
  442. $result = self::get($url, $param, $config);
  443. return $result;
  444. }
  445. /**
  446. * 支付撤销
  447. */
  448. static public function close()
  449. {
  450. $url = self::$close_url;
  451. $result = self::get($url, $param, $config);
  452. return $result;
  453. }
  454. /**
  455. * 交易退款
  456. */
  457. static public function refund($param, $config)
  458. {
  459. $url = self::$refund_url;
  460. $result = self::get($url, $param, $config, false);
  461. return $result;
  462. }
  463. /**
  464. * 交易查询
  465. */
  466. static public function query()
  467. {
  468. $url = self::$query_url;
  469. $result = self::get($url, $param, $config);
  470. return $result;
  471. }
  472. /**
  473. * 交易退款查询
  474. */
  475. static public function refundQuery()
  476. {
  477. $url = self::$refund_query_url;
  478. $result = self::get($url, $param, $config);
  479. return $result;
  480. }
  481. //====================== 获取调用凭证===========================
  482. /**
  483. * 通用调用凭证获取
  484. * 默认使用token方式,使用签名方式需要传入参数
  485. */
  486. static public function getAuth($type = 'token', $param, $config)
  487. {
  488. if ($type === 'token') {
  489. return self::getAccessTokenByToken($config);
  490. } elseif ($type === 'form') {
  491. return self::getAccessTokenByForm($param, $config);
  492. }
  493. return self::getAccessTokenBySig($param, $config);
  494. }
  495. /**
  496. * 方式一,OPEN-ACCESS-TOKEN方式
  497. */
  498. static function getAccessTokenByToken($config)
  499. {
  500. $param = [
  501. 'appId' => $config['appid'],
  502. 'timestamp' => date('YmdHis'),
  503. 'nonce' => md5(uniqid(microtime(true),true)),
  504. 'signMethod' => self::$signMethod,
  505. ];
  506. $param['signature'] = self::signature($param, $config['appsecret']);
  507. $result = self::get(self::$token_url, $param, $config);
  508. if (isset($result['errCode']) && isset($result['accessToken'])) {
  509. return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
  510. }
  511. return '';
  512. }
  513. /**
  514. * 方式二,OPEN-BODY-SIG方式
  515. */
  516. static function getAccessTokenBySig($data, $config)
  517. {
  518. $param = [
  519. 'AppId' => $config['appid'],
  520. 'Timestamp' => date('YmdHis'),
  521. 'Nonce' => md5(uniqid(microtime(true),true))
  522. ];
  523. return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
  524. }
  525. /**
  526. * 方式三,OPEN-FORM-PARAM方式
  527. */
  528. static function getAccessTokenByForm($data, $config)
  529. {
  530. $content = Dever::json_encode($data);
  531. $param = [
  532. 'appId' => $config['appid'],
  533. 'timestamp' => date('YmdHis'),
  534. 'nonce' => md5(uniqid(microtime(true),true)),
  535. 'authorization' => 'OPEN-FORM-PARAM',
  536. 'content' => $content,
  537. ];
  538. $param['signature'] = urlencode(self::signature3($param, $config['appsecret']));
  539. $param['content'] = urlencode($param['content']);
  540. return $param;
  541. }
  542. /**
  543. * 计算签名,方式一
  544. */
  545. static function signature($param, $appsecret)
  546. {
  547. return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
  548. }
  549. /**
  550. * 计算签名,方式二
  551. */
  552. static function signature2($data, $param, $appsecret)
  553. {
  554. $str = bin2hex(hash('sha256', Dever::json_encode($data), true));
  555. return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
  556. }
  557. /**
  558. * 计算签名,方式三
  559. */
  560. static function signature3($param, $appsecret)
  561. {
  562. $str = bin2hex(hash('sha256', $param['content'], true));
  563. return base64_encode(hash_hmac('sha256', $param['appId'].$param['timestamp'].$param['nonce'].$str, $appsecret, true));
  564. }
  565. /**
  566. * 获取信息
  567. */
  568. static function get($url, $param, $config, $state = true)
  569. {
  570. $url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
  571. if (strstr($url, 'webpay')) {
  572. $url .= '?';
  573. $param = self::getAuth('form', $param, $config);
  574. foreach ($param as $k => $v) {
  575. $url .= '&' . $k . '=' . $v;
  576. }
  577. $url = str_replace('?&', '?', $url);
  578. return $url;
  579. } else {
  580. $header = array();
  581. $header['Authorization'] = self::getAuth('sig', $param, $config);
  582. $result = Dever::curl($url, $param, 'post', true, $header);
  583. }
  584. if (strstr($result, '<html><head>')) {
  585. Dever::alert('系统错误');
  586. }
  587. $result = Dever::json_decode($result);
  588. if (isset($result['errCode'])) {
  589. if (!$state) {
  590. return $result;
  591. }
  592. if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
  593. return $result;
  594. } elseif (isset($result['errInfo'])) {
  595. //Dever::alert('操作失败,请联系管理员');
  596. Dever::alert($result['errInfo']);
  597. } elseif (isset($result['errMsg'])) {
  598. //Dever::alert('操作失败,请联系管理员');
  599. Dever::alert($result['errMsg']);
  600. }
  601. } else {
  602. Dever::alert('系统错误');
  603. }
  604. return $result;
  605. }
  606. }