Yspay.php 21 KB

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