Yspay.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. $prefix = '';
  129. if (isset($this->config['prefix']) && $this->config['prefix']) {
  130. $prefix = $this->config['prefix'];
  131. }
  132. $request['merOrderId'] = $prefix . $order_id;
  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. $subOrders['merOrderId'] = $prefix . $v['order_id'];
  167. $request['subOrders'][] = $subOrders;
  168. $fenzhang = 0;
  169. if (isset($v['fenzhang']) && $v['fenzhang'] > 0) {
  170. $fenzhang = $v['fenzhang'];
  171. }
  172. Dever::load('pay/yspay/cash')->add($subOrders['mid'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id'], $fenzhang);
  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'] . '_O',
  184. );
  185. $request['subOrders'][] = $subOrders;
  186. $request['platformAmount'] = 0;
  187. Dever::load('pay/yspay/cash')->add($subOrders['mid'], $subOrders['totalAmount'], $subOrders['merOrderId'], $v['order_id']);
  188. }
  189. }
  190. }
  191. }
  192. if (isset($request['divisionFlag']) && $request['divisionFlag']) {
  193. } else {
  194. $request['platformAmount'] = 0;
  195. Dever::load('pay/yspay/cash')->add($request['mid'], $request['totalAmount'], $request['merOrderId'], $order_id);
  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'] = 0;
  269. $request['subOrders'] = array();
  270. foreach ($other as $k => $v) {
  271. $subOrders = array();
  272. if (isset($v['amount']) && $v['amount']) {
  273. $subOrders['totalAmount'] = round($v['amount'] * 100, 2);
  274. } elseif (isset($v['per']) && $v['per']) {
  275. $v['per'] = $v['per'] && $v['per'] >= 0 ? $v['per'] : 0;
  276. $v['per'] = $v['per']/100;
  277. $request['platformAmount'] = round($request['refundAmount'] * $v['per'], 2);
  278. $subOrders['totalAmount'] = round($request['totalAmount'] - $request['platformAmount'], 2);
  279. } else {
  280. continue;
  281. }
  282. $subOrders['mid'] = $v['mid'];
  283. if (isset($this->config['prefix']) && $this->config['prefix']) {
  284. $subOrders['merOrderId'] = $this->config['prefix'] . $v['order_id'];
  285. if ($refund_order_id) {
  286. $subOrders['refundOrderId'] = $this->config['prefix'] . $refund_order_id;
  287. }
  288. } else {
  289. $subOrders['merOrderId'] = $v['order_id'];
  290. if ($refund_order_id) {
  291. $subOrders['refundOrderId'] = $refund_order_id;
  292. }
  293. }
  294. $request['subOrders'][] = $subOrders;
  295. }
  296. }
  297. $account = Dever::db('pay/account')->find($order['account_id']);
  298. $method = 'refund';
  299. if ($account && $account['system_source'] == 4) {
  300. $method = 'code_refund';
  301. $request['billNo'] = $request['merOrderId'];
  302. $request['instMid'] = 'QRPAYDEFAULT';
  303. $request['billDate'] = date('Y-m-d');
  304. }
  305. $result = Base::$method($request, $this->config);
  306. if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
  307. return true;
  308. }
  309. return false;
  310. }
  311. # 查询
  312. public function query($order_id)
  313. {
  314. $request['merOrderId'] = $order_id;
  315. $request['mid'] = $this->config['mchid'];
  316. $request['tid'] = $this->config['key'];
  317. $request['instMid'] = 'MINIDEFAULT';
  318. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  319. $result = Base::refund($request, $this->config);
  320. return $result;
  321. }
  322. # 关闭订单
  323. public function close($order_id)
  324. {
  325. $request['merOrderId'] = $order_id;
  326. $request['mid'] = $this->config['mchid'];
  327. $request['tid'] = $this->config['key'];
  328. $request['instMid'] = 'MINIDEFAULT';
  329. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  330. $result = Base::close($request, $this->config);
  331. return $result;
  332. }
  333. /**
  334. * 获取二维码支付
  335. */
  336. public function qrcode($order, $refer)
  337. {
  338. $result['type'] = 'qrcode';
  339. $result['url'] = $order;
  340. return $result;
  341. }
  342. /**
  343. * 获取小程序支付
  344. */
  345. public function applet($order)
  346. {
  347. $result = array();
  348. if (isset($order['package'])) {
  349. $prepay_id = str_replace('prepay_id=', '', $order['package']);
  350. $result['time'] = $order['timeStamp'];
  351. $result['nonce_str'] = $order['nonceStr'];
  352. $result['prepay_id'] = $prepay_id;
  353. $result['sign_type'] = $order['signType'];
  354. $result['sign'] = $order['paySign'];
  355. $result['type'] = 'applet';
  356. }
  357. return $result;
  358. }
  359. /**
  360. * 获取页面支付 去收银台
  361. */
  362. public function page($order, $refer)
  363. {
  364. return $order;
  365. }
  366. }
  367. class Base
  368. {
  369. static $test_host = 'https://test-api-open.chinaums.com/';
  370. static $host = 'https://api-mop.chinaums.com/';
  371. static $signMethod = 'SHA256'; //签名方式
  372. # 获取token
  373. static $token_url = 'v1/token/access';
  374. # 微信下单支付
  375. static $pay_wechat_url = 'v1/netpay/wx/unified-order';
  376. # 支付宝下单支付
  377. static $pay_alipay_url = 'v1/netpay/trade/create';
  378. # 云闪付下单支付
  379. static $pay_uac_url = 'v1/netpay/uac/mini-order';
  380. # 交易查询
  381. static $query_url = 'v1/netpay/query';
  382. # 退款
  383. static $refund_url = 'v1/netpay/refund';
  384. # 退款查询
  385. static $refund_query_url = 'v1/netpay/refund-query';
  386. # 订单关闭
  387. static $close_url = 'v1/netpay/close';
  388. # 二维码支付
  389. static $qrcode_url = 'v1/netpay/bills/get-qrcode';
  390. # 二维码支付退款
  391. static $qrcode_refund_url = 'v1/netpay/bills/refund';
  392. # h5支付
  393. static $h5_url = 'v1/netpay/webpay/pay';
  394. # h5支付退款
  395. static $h5_refund_url = 'v1/netpay/wx/app-pre-order';
  396. //===================== 支付相关 ==============================
  397. /**
  398. * 支付交易
  399. */
  400. static public function pay($param, $config)
  401. {
  402. $url = self::$pay_wechat_url;
  403. $result = self::get($url, $param, $config);
  404. return $result;
  405. }
  406. /**
  407. * h5支付
  408. */
  409. static public function h5_pay($param, $config)
  410. {
  411. $url = self::$h5_url;
  412. $result = self::get($url, $param, $config);
  413. return $result;
  414. }
  415. /**
  416. * 获取支付二维码
  417. */
  418. static public function get_pay_code($param, $config)
  419. {
  420. $url = self::$qrcode_url;
  421. $result = self::get($url, $param, $config);
  422. return $result;
  423. }
  424. /**
  425. * 二维码交易退款
  426. */
  427. static public function code_refund($param, $config)
  428. {
  429. $url = self::$qrcode_refund_url;
  430. $result = self::get($url, $param, $config);
  431. return $result;
  432. }
  433. /**
  434. * 支付撤销
  435. */
  436. static public function close()
  437. {
  438. $url = self::$close_url;
  439. $result = self::get($url, $param, $config);
  440. return $result;
  441. }
  442. /**
  443. * 交易退款
  444. */
  445. static public function refund($param, $config)
  446. {
  447. $url = self::$refund_url;
  448. $result = self::get($url, $param, $config, false);
  449. return $result;
  450. }
  451. /**
  452. * 交易查询
  453. */
  454. static public function query()
  455. {
  456. $url = self::$query_url;
  457. $result = self::get($url, $param, $config);
  458. return $result;
  459. }
  460. /**
  461. * 交易退款查询
  462. */
  463. static public function refundQuery()
  464. {
  465. $url = self::$refund_query_url;
  466. $result = self::get($url, $param, $config);
  467. return $result;
  468. }
  469. //====================== 获取调用凭证===========================
  470. /**
  471. * 通用调用凭证获取
  472. * 默认使用token方式,使用签名方式需要传入参数
  473. */
  474. static public function getAuth($type = 'token', $param, $config)
  475. {
  476. if ($type === 'token') {
  477. return self::getAccessTokenByToken($config);
  478. } elseif ($type === 'form') {
  479. return self::getAccessTokenByForm($param, $config);
  480. }
  481. return self::getAccessTokenBySig($param, $config);
  482. }
  483. /**
  484. * 方式一,OPEN-ACCESS-TOKEN方式
  485. */
  486. static function getAccessTokenByToken($config)
  487. {
  488. $param = [
  489. 'appId' => $config['appid'],
  490. 'timestamp' => date('YmdHis'),
  491. 'nonce' => md5(uniqid(microtime(true),true)),
  492. 'signMethod' => self::$signMethod,
  493. ];
  494. $param['signature'] = self::signature($param, $config['appsecret']);
  495. $result = self::get(self::$token_url, $param, $config);
  496. if (isset($result['errCode']) && isset($result['accessToken'])) {
  497. return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
  498. }
  499. return '';
  500. }
  501. /**
  502. * 方式二,OPEN-BODY-SIG方式
  503. */
  504. static function getAccessTokenBySig($data, $config)
  505. {
  506. $param = [
  507. 'AppId' => $config['appid'],
  508. 'Timestamp' => date('YmdHis'),
  509. 'Nonce' => md5(uniqid(microtime(true),true))
  510. ];
  511. return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
  512. }
  513. /**
  514. * 方式三,OPEN-FORM-PARAM方式
  515. */
  516. static function getAccessTokenByForm($data, $config)
  517. {
  518. $content = Dever::json_encode($data);
  519. $param = [
  520. 'appId' => $config['appid'],
  521. 'timestamp' => date('YmdHis'),
  522. 'nonce' => md5(uniqid(microtime(true),true)),
  523. 'authorization' => 'OPEN-FORM-PARAM',
  524. 'content' => $content,
  525. ];
  526. $param['signature'] = urlencode(self::signature3($param, $config['appsecret']));
  527. $param['content'] = urlencode($param['content']);
  528. return $param;
  529. }
  530. /**
  531. * 计算签名,方式一
  532. */
  533. static function signature($param, $appsecret)
  534. {
  535. return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
  536. }
  537. /**
  538. * 计算签名,方式二
  539. */
  540. static function signature2($data, $param, $appsecret)
  541. {
  542. $str = bin2hex(hash('sha256', Dever::json_encode($data), true));
  543. return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
  544. }
  545. /**
  546. * 计算签名,方式三
  547. */
  548. static function signature3($param, $appsecret)
  549. {
  550. $str = bin2hex(hash('sha256', $param['content'], true));
  551. return base64_encode(hash_hmac('sha256', $param['appId'].$param['timestamp'].$param['nonce'].$str, $appsecret, true));
  552. }
  553. /**
  554. * 获取信息
  555. */
  556. static function get($url, $param, $config, $state = true)
  557. {
  558. $url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
  559. if (strstr($url, 'webpay')) {
  560. $url .= '?';
  561. $param = self::getAuth('form', $param, $config);
  562. foreach ($param as $k => $v) {
  563. $url .= '&' . $k . '=' . $v;
  564. }
  565. $url = str_replace('?&', '?', $url);
  566. return $url;
  567. } else {
  568. $header = array();
  569. $header['Authorization'] = self::getAuth('sig', $param, $config);
  570. $result = Dever::curl($url, $param, 'post', true, $header);
  571. }
  572. if (strstr($result, '<html><head>')) {
  573. Dever::alert('系统错误');
  574. }
  575. $result = Dever::json_decode($result);
  576. if (isset($result['errCode'])) {
  577. if (!$state) {
  578. return $result;
  579. }
  580. if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
  581. return $result;
  582. } elseif (isset($result['errInfo'])) {
  583. //Dever::alert('操作失败,请联系管理员');
  584. Dever::alert($result['errInfo']);
  585. } elseif (isset($result['errMsg'])) {
  586. //Dever::alert('操作失败,请联系管理员');
  587. Dever::alert($result['errMsg']);
  588. }
  589. } else {
  590. Dever::alert('系统错误');
  591. }
  592. return $result;
  593. }
  594. }