Lakala.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace common\components\lakala;
  3. use common\components\noticeUtil;
  4. use common\components\stringUtil;
  5. class Lakala
  6. {
  7. private $appid;
  8. private $serial_no;
  9. private $merchant_no;
  10. private $term_no;
  11. private $merchantPrivateKeyPath;
  12. private $lklCertificatePath;
  13. private $schema = 'LKLAPI-SHA256withRSA';
  14. private $version = '3.0';
  15. private $preUrl = '';
  16. public function accountBalanceQuery()
  17. {
  18. $arr = [
  19. 'merchantNo' => $this->merchant_no,
  20. 'orgNo' => 970936,
  21. ];
  22. $rand = time() . '_' . stringUtil::charsShuffle(22);
  23. $baseRequestVO = [
  24. 'reqData' => $arr,
  25. 'timestamp' => time(),
  26. 'reqId' => $rand,
  27. 'ver' => '1.0.0',
  28. ];
  29. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  30. $authorization = $this->getAuthorization($body);
  31. $url = 'https://s2.lakala.com/api/v2/laep/industry/ewalletBalanceQuery';
  32. return $this->post($url, $body, $authorization);
  33. }
  34. //申请主动结算 ssh
  35. public function applyInviteClear($params)
  36. {
  37. $subMerchantId = $params['subMerchantId'];
  38. $tradeMode = $params['tradeMode'];
  39. $arr = [
  40. 'tradeMode' => $tradeMode,
  41. 'merchantNo' => $this->merchant_no,
  42. 'subMerchantId' => $subMerchantId,
  43. 'orderNo' => $orderNo,
  44. //机构代号
  45. 'orgCode' => '970936',
  46. 'version' => 'v1.0.0',
  47. ];
  48. $rand = time() . '_' . stringUtil::charsShuffle(22);
  49. $baseRequestVO = [
  50. 'reqData' => $arr,
  51. 'timestamp' => time(),
  52. 'reqId' => $rand,
  53. 'ver' => '1.0.0',
  54. ];
  55. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  56. $authorization = $this->getAuthorization($body);
  57. $url = 'https://s2.lakala.com/api/v2/mms/sme/mrchAuthStateQuery';
  58. return $this->post($url, $body, $authorization);
  59. }
  60. public function __construct($params)
  61. {
  62. $this->appid = $params['appid'] ?? '';
  63. $this->serial_no = $params['serial_no'] ?? '';
  64. $this->merchant_no = $params['merchant_no'] ?? '';
  65. $this->term_no = $params['term_no'] ?? '';
  66. $this->merchantPrivateKeyPath = $params['merchantPrivateKeyPath'];
  67. $this->lklCertificatePath = $params['lklCertificatePath'];
  68. if (getenv('YII_ENV') == 'production') {
  69. $this->preUrl = 'https://s2.lakala.com/api/v3/labs/trans/';
  70. } else {
  71. //$this->preUrl = 'https://test.wsmsd.cn/sit/api/v3/labs/trans/';
  72. $this->preUrl = 'https://s2.lakala.com/api/v3/labs/trans/';
  73. }
  74. }
  75. //退款查询
  76. public function refundQuery($params)
  77. {
  78. $orderSn = $params['orderSn'];
  79. $arr = [
  80. 'merchant_no' => $this->merchant_no,
  81. 'term_no' => $this->term_no,
  82. //'out_trade_no' => $orderSn,
  83. 'trade_no' => $orderSn,
  84. ];
  85. $baseRequestVO = [
  86. 'req_data' => $arr,
  87. 'req_time' => date("YmdHis"),
  88. 'version' => $this->version,
  89. ];
  90. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  91. $authorization = $this->getAuthorization($body);
  92. $url = 'https://s2.lakala.com/api/v3/labs/query/tradequery';
  93. return $this->post($url, $body, $authorization);
  94. }
  95. public function openStateQuery($params)
  96. {
  97. $subMerchantId = $params['subMerchantId'];
  98. $tradeMode = $params['tradeMode'];
  99. $arr = [
  100. 'tradeMode' => $tradeMode,
  101. 'merchantNo' => $this->merchant_no,
  102. 'subMerchantId' => $subMerchantId,
  103. ];
  104. $baseRequestVO = [
  105. 'reqData' => $arr,
  106. 'timestamp' => time(),
  107. 'ver' => '1.0.0',
  108. ];
  109. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  110. $authorization = $this->getAuthorization($body);
  111. $url = 'https://s2.lakala.com/api/v2/mms/sme/mrchAuthStateQuery';
  112. return $this->post($url, $body, $authorization);
  113. }
  114. //收银台支付,APP支付和H5支付时使用 ssh 2023112
  115. public function cashierPay($params)
  116. {
  117. $orderSn = $params['orderSn'];
  118. $amount = $params['amount'];
  119. $notifyUrl = $params['notifyUrl'];
  120. $subject = $params['subject'];
  121. $capitalType = $params['capitalType'];
  122. $time = time() + 108000;
  123. $orderEfficientTime = date("YmdHis", $time);
  124. $bus = json_encode([['busi_type' => 'SCPAY', 'params' => ['pay_mode' => 'ALIPAY']]]);
  125. $arr = [
  126. 'merchant_no' => $this->merchant_no,
  127. 'term_no' => $this->term_no,
  128. 'out_order_no' => $orderSn,
  129. 'total_amount' => bcmul($amount, 100),
  130. 'order_info' => $subject,
  131. 'support_refund' => 1,
  132. 'notify_url' => $notifyUrl,
  133. 'order_efficient_time' => $orderEfficientTime,
  134. 'busi_type_param' => $bus,
  135. 'counter_param' => json_encode(['pay_mode' => 'ALIPAY']),
  136. 'counter_remark' => '{"capitalType":' . $capitalType . '}'
  137. ];
  138. $baseRequestVO = [
  139. 'req_data' => $arr,
  140. 'req_time' => date("YmdHis"),
  141. 'version' => $this->version,
  142. ];
  143. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  144. $authorization = $this->getAuthorization($body);
  145. $url = 'https://s2.lakala.com/api/v3/ccss/counter/order/create';
  146. return $this->post($url, $body, $authorization);
  147. }
  148. //收银机订单查询 ssh 20231026
  149. public function cashierQuery($params)
  150. {
  151. $out_order_no = $params['out_order_no'];
  152. $arr = [
  153. 'merchant_no' => $this->merchant_no,
  154. 'out_order_no' => $out_order_no,
  155. ];
  156. $baseRequestVO = [
  157. 'req_data' => $arr,
  158. 'req_time' => date("YmdHis"),
  159. 'version' => $this->version,
  160. ];
  161. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  162. $authorization = $this->getAuthorization($body);
  163. $url = 'https://s2.lakala.com/api/v3/ccss/counter/order/query';
  164. return $this->post($url, $body, $authorization);
  165. }
  166. public function query($params, $lklSn = 0)
  167. {
  168. $orderSn = $params['orderSn'] ?? '';
  169. $tradeNo = $params['tradeNo'] ?? '';
  170. $ip = $this->getClientIp();
  171. $location_info = ['request_ip' => $ip];
  172. $arr = [
  173. 'merchant_no' => $this->merchant_no,
  174. 'term_no' => $this->term_no,
  175. 'location_info' => $location_info,
  176. ];
  177. if ($lklSn == 0) {
  178. $arr['out_trade_no'] = $orderSn;
  179. } else {
  180. $arr['trade_no'] = $tradeNo;
  181. }
  182. $baseRequestVO = [
  183. 'req_data' => $arr,
  184. 'req_time' => date("YmdHis"),
  185. 'version' => $this->version,
  186. ];
  187. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  188. $authorization = $this->getAuthorization($body);
  189. $url = 'https://s2.lakala.com/api/v3/labs/query/tradequery';
  190. return $this->post($url, $body, $authorization);
  191. }
  192. //被扫付款
  193. public function scanPay($params)
  194. {
  195. $orderSn = $params['orderSn'];
  196. $amount = $params['amount'];
  197. $notifyUrl = $params['notifyUrl'];
  198. $subject = $params['subject'];
  199. $authCode = $params['authCode'];
  200. $capitalType = $params['capitalType'];
  201. $ip = $this->getClientIp();
  202. $location_info = ['request_ip' => $ip];
  203. $arr = [
  204. 'merchant_no' => $this->merchant_no,
  205. 'term_no' => $this->term_no,
  206. 'out_trade_no' => $orderSn,
  207. 'total_amount' => bcmul($amount, 100),
  208. 'auth_code' => $authCode,
  209. 'location_info' => $location_info,
  210. 'subject' => $subject,
  211. 'notify_url' => $notifyUrl,
  212. 'remark' => '{"capitalType":' . $capitalType . '}',
  213. ];
  214. $baseRequestVO = [
  215. 'req_data' => $arr,
  216. 'req_time' => date("YmdHis"),
  217. 'version' => $this->version,
  218. ];
  219. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  220. $authorization = $this->getAuthorization($body);
  221. $url = 'https://s2.lakala.com/api/v3/labs/trans/micropay';
  222. return $this->post($url, $body, $authorization);
  223. }
  224. //关掉 ssh 20231009
  225. public function close($params)
  226. {
  227. $orderSn = $params['orderSn'];
  228. $ip = $this->getClientIp();
  229. $location_info = ['request_ip' => $ip];
  230. $arr = [
  231. 'merchant_no' => $this->merchant_no,
  232. 'term_no' => $this->term_no,
  233. 'location_info' => $location_info,
  234. 'origin_out_trade_no' => $orderSn,
  235. ];
  236. $baseRequestVO = [
  237. 'req_data' => $arr,
  238. 'req_time' => date("YmdHis"),
  239. 'version' => $this->version,
  240. ];
  241. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  242. $authorization = $this->getAuthorization($body);
  243. $lklUrl = 'https://s2.lakala.com/api/v3/labs/relation/close';
  244. return $this->post($lklUrl, $body, $authorization);
  245. }
  246. public function cashClose($params)
  247. {
  248. $orderSn = $params['orderSn'];
  249. $ip = $this->getClientIp();
  250. $location_info = ['request_ip' => $ip];
  251. $arr = [
  252. 'merchant_no' => $this->merchant_no,
  253. 'term_no' => $this->term_no,
  254. 'location_info' => $location_info,
  255. 'out_order_no' => $orderSn,
  256. ];
  257. $baseRequestVO = [
  258. 'req_data' => $arr,
  259. 'req_time' => date("YmdHis"),
  260. 'version' => '1.0',
  261. ];
  262. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  263. $authorization = $this->getAuthorization($body);
  264. $lklUrl = 'https://s2.lakala.com/api/v3/ccss/counter/order/close';
  265. return $this->post($lklUrl, $body, $authorization);
  266. }
  267. //退款 ssh 20231009
  268. public function refund($params)
  269. {
  270. $orderSn = $params['orderSn'] ?? '';
  271. $refundSn = $params['refundSn'];
  272. $refundReason = $params['refundReason'];
  273. $refundAmount = $params['refundAmount'];
  274. $thirdNo = $params['thirdNo'] ?? '';
  275. $ip = $this->getClientIp();
  276. $location_info = ['request_ip' => $ip];
  277. $arr = [
  278. 'merchant_no' => $this->merchant_no,
  279. 'term_no' => $this->term_no,
  280. 'out_trade_no' => $refundSn,
  281. 'refund_amount' => $refundAmount,
  282. 'location_info' => $location_info,
  283. 'refund_reason' => $refundReason,
  284. 'origin_out_trade_no' => $orderSn,
  285. 'origin_trade_no' => $thirdNo,
  286. ];
  287. //原拉卡拉交易流水号,收银台接口下单的,发起退款时必传
  288. if (isset($params['originTradeNo'])) {
  289. $arr['origin_trade_no'] = $params['originTradeNo'];
  290. }
  291. if (isset($params['originLogNo'])) {
  292. $arr['origin_log_no'] = $params['originLogNo'];
  293. }
  294. $baseRequestVO = [
  295. 'req_data' => $arr,
  296. 'req_time' => date("YmdHis"),
  297. 'version' => $this->version,
  298. ];
  299. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  300. $authorization = $this->getAuthorization($body);
  301. $lklUrl = 'https://s2.lakala.com/api/v3/labs/relation/refund';
  302. return $this->post($lklUrl, $body, $authorization);
  303. }
  304. //主扫交易,微信
  305. public function driveWxPay($params)
  306. {
  307. $orderSn = $params['orderSn'];
  308. $amount = $params['amount'];
  309. $notifyUrl = $params['notifyUrl'];
  310. $subject = $params['subject'];
  311. $capitalType = $params['capitalType'];
  312. $wxAppId = $params['wxAppId'] ?? '';
  313. $openId = $params['openId'] ?? '';
  314. $ip = $this->getClientIp();
  315. $location_info = ['request_ip' => $ip];
  316. $arr = [
  317. 'merchant_no' => $this->merchant_no,
  318. 'term_no' => $this->term_no,
  319. 'out_trade_no' => $orderSn,
  320. 'account_type' => 'WECHAT',
  321. 'trans_type' => 71,
  322. 'total_amount' => bcmul($amount, 100),
  323. 'location_info' => $location_info,
  324. 'subject' => $subject,
  325. 'notify_url' => $notifyUrl,
  326. 'remark' => '{"capitalType":' . $capitalType . '}',
  327. 'acc_busi_fields' => [
  328. 'sub_appid' => $wxAppId,
  329. 'user_id' => $openId,
  330. 'timeout_express' => 30,
  331. ],
  332. ];
  333. $baseRequestVO = [
  334. 'req_data' => $arr,
  335. 'req_time' => date("YmdHis"),
  336. 'version' => $this->version,
  337. ];
  338. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  339. $authorization = $this->getAuthorization($body);
  340. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  341. }
  342. public function driveNativeWxPay($params)
  343. {
  344. $orderSn = $params['orderSn'];
  345. $amount = $params['amount'];
  346. $notifyUrl = $params['notifyUrl'];
  347. $subject = $params['subject'];
  348. $capitalType = $params['capitalType'];
  349. $wxAppId = $params['wxAppId'] ?? '';
  350. $ip = $this->getClientIp();
  351. $location_info = ['request_ip' => $ip];
  352. $arr = [
  353. 'merchant_no' => $this->merchant_no,
  354. 'term_no' => $this->term_no,
  355. 'out_trade_no' => $orderSn,
  356. 'account_type' => 'WECHAT',
  357. 'trans_type' => 71,
  358. 'total_amount' => bcmul($amount, 100),
  359. 'location_info' => $location_info,
  360. 'subject' => $subject,
  361. 'notify_url' => $notifyUrl,
  362. 'remark' => '{"capitalType":' . $capitalType . '}',
  363. 'acc_busi_fields' => [
  364. 'timeout_express' => 30,
  365. 'sub_appid' => $wxAppId,
  366. ],
  367. ];
  368. $baseRequestVO = [
  369. 'req_data' => $arr,
  370. 'req_time' => date("YmdHis"),
  371. 'version' => $this->version,
  372. ];
  373. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  374. $authorization = $this->getAuthorization($body);
  375. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  376. }
  377. //主扫交易,支付宝
  378. public function driveAliPay($params)
  379. {
  380. $orderSn = $params['orderSn'];
  381. $amount = $params['amount'];
  382. $notifyUrl = $params['notifyUrl'];
  383. $subject = $params['subject'];
  384. $capitalType = $params['capitalType'];
  385. $ip = $this->getClientIp();
  386. $location_info = ['request_ip' => $ip];
  387. $arr = [
  388. 'merchant_no' => $this->merchant_no,
  389. 'term_no' => $this->term_no,
  390. 'out_trade_no' => $orderSn,
  391. 'account_type' => 'ALIPAY',
  392. 'trans_type' => '41',
  393. 'total_amount' => bcmul($amount, 100),
  394. 'location_info' => $location_info,
  395. 'subject' => $subject,
  396. 'notify_url' => $notifyUrl,
  397. 'remark' => '{"capitalType":' . $capitalType . '}',
  398. ];
  399. $baseRequestVO = [
  400. 'req_data' => $arr,
  401. 'req_time' => date("YmdHis"),
  402. 'version' => $this->version,
  403. ];
  404. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  405. $authorization = $this->getAuthorization($body);
  406. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  407. }
  408. //验签
  409. public function signatureVerification($authorization, $body)
  410. {
  411. $authorization = str_replace($this->schema . " ", "", $authorization);
  412. $authorization = str_replace(",", "&", $authorization);
  413. $authorization = str_replace("\"", "", $authorization);
  414. $authorization = $this->convertUrlQuery($authorization);
  415. $authorization['signature'] = base64_decode($authorization['signature']);
  416. $message = $authorization['timestamp'] . "\n" . $authorization['nonce_str'] . "\n" . $body . "\n";
  417. $key = openssl_get_publickey(file_get_contents($this->lklCertificatePath));
  418. $flag = openssl_verify($message, $authorization['signature'], $key, OPENSSL_ALGO_SHA256);
  419. openssl_free_key($key);
  420. if ($flag) {
  421. return true;
  422. }
  423. return false;
  424. }
  425. //签名
  426. public function getAuthorization($body)
  427. {
  428. $nonceStr = $this->randString(12);
  429. $timestamp = time();
  430. $message = $this->appid . "\n" . $this->serial_no . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
  431. $key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));
  432. openssl_sign($message, $signature, $key, OPENSSL_ALGO_SHA256);
  433. openssl_free_key($key);
  434. return $this->schema . " appid=\"" . $this->appid . "\"," . "serial_no=\"" . $this->serial_no . "\"," . "timestamp=\"" . $timestamp . "\"," . "nonce_str=\"" . $nonceStr . "\"," . "signature=\"" . base64_encode($signature) . "\"";
  435. }
  436. //获取随机数
  437. public function randString($length)
  438. {
  439. $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  440. $randomString = "";
  441. for ($i = 0; $i < $length; $i++) {
  442. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  443. }
  444. return $randomString;
  445. }
  446. //请求
  447. public function post($url, $data, $authorization)
  448. {
  449. $headers = [
  450. "Authorization: " . $authorization,
  451. "Accept: application/json",
  452. "Content-Type:application/json",
  453. ];
  454. \Yii::info("请求参数:" . $data);
  455. $ch = curl_init();
  456. curl_setopt($ch, CURLOPT_POST, 1);
  457. curl_setopt($ch, CURLOPT_URL, $url);
  458. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  459. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  460. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  461. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
  462. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  463. curl_setopt($ch, CURLOPT_POST, 1);
  464. $res = curl_exec($ch);
  465. curl_close($ch);
  466. return json_decode($res, 1);
  467. }
  468. //签名参数转数组
  469. private function convertUrlQuery($query)
  470. {
  471. $queryParts = explode('&', $query);
  472. $params = array();
  473. foreach ($queryParts as $param) {
  474. $item = explode('=', $param);
  475. $params[$item[0]] = $item[1];
  476. }
  477. if ($params['signature']) {
  478. $params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
  479. }
  480. return $params;
  481. }
  482. public function getClientIp()
  483. {
  484. if (isset($_SERVER['REMOTE_ADDR'])) {
  485. $cip = $_SERVER['REMOTE_ADDR'];
  486. } elseif (getenv("REMOTE_ADDR")) {
  487. $cip = getenv("REMOTE_ADDR");
  488. } elseif (getenv("HTTP_CLIENT_IP")) {
  489. $cip = getenv("HTTP_CLIENT_IP");
  490. } else {
  491. if (getenv('YII_ENV') == 'production') {
  492. $cip = "114.55.169.20";
  493. } else {
  494. $cip = "118.178.193.23";
  495. }
  496. }
  497. return $cip;
  498. }
  499. }