Yspay.php 20 KB

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