Lakala.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. //这个参数必传,不然聚合收银合支付宝付款的退款不会成功
  275. $thirdNo = $params['thirdNo'] ?? '';
  276. $ip = $this->getClientIp();
  277. $location_info = ['request_ip' => $ip];
  278. $arr = [
  279. 'merchant_no' => $this->merchant_no,
  280. 'term_no' => $this->term_no,
  281. 'out_trade_no' => $refundSn,
  282. 'refund_amount' => $refundAmount,
  283. 'location_info' => $location_info,
  284. 'refund_reason' => $refundReason,
  285. 'origin_out_trade_no' => $orderSn,
  286. 'origin_trade_no' => $thirdNo,
  287. ];
  288. //原拉卡拉交易流水号,收银台接口下单的,发起退款时必传
  289. if (isset($params['originTradeNo'])) {
  290. $arr['origin_trade_no'] = $params['originTradeNo'];
  291. }
  292. if (isset($params['originLogNo'])) {
  293. $arr['origin_log_no'] = $params['originLogNo'];
  294. }
  295. $baseRequestVO = [
  296. 'req_data' => $arr,
  297. 'req_time' => date("YmdHis"),
  298. 'version' => $this->version,
  299. ];
  300. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  301. $authorization = $this->getAuthorization($body);
  302. $lklUrl = 'https://s2.lakala.com/api/v3/labs/relation/refund';
  303. return $this->post($lklUrl, $body, $authorization);
  304. }
  305. //主扫交易,微信
  306. public function driveWxPay($params)
  307. {
  308. $orderSn = $params['orderSn'];
  309. $amount = $params['amount'];
  310. $notifyUrl = $params['notifyUrl'];
  311. $subject = $params['subject'];
  312. $capitalType = $params['capitalType'];
  313. $wxAppId = $params['wxAppId'] ?? '';
  314. $openId = $params['openId'] ?? '';
  315. $ip = $this->getClientIp();
  316. $location_info = ['request_ip' => $ip];
  317. $arr = [
  318. 'merchant_no' => $this->merchant_no,
  319. 'term_no' => $this->term_no,
  320. 'out_trade_no' => $orderSn,
  321. 'account_type' => 'WECHAT',
  322. 'trans_type' => 71,
  323. 'total_amount' => bcmul($amount, 100),
  324. 'location_info' => $location_info,
  325. 'subject' => $subject,
  326. 'notify_url' => $notifyUrl,
  327. 'remark' => '{"capitalType":' . $capitalType . '}',
  328. 'acc_busi_fields' => [
  329. 'sub_appid' => $wxAppId,
  330. 'user_id' => $openId,
  331. 'timeout_express' => 30,
  332. ],
  333. ];
  334. $baseRequestVO = [
  335. 'req_data' => $arr,
  336. 'req_time' => date("YmdHis"),
  337. 'version' => $this->version,
  338. ];
  339. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  340. $authorization = $this->getAuthorization($body);
  341. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  342. }
  343. public function driveNativeWxPay($params)
  344. {
  345. $orderSn = $params['orderSn'];
  346. $amount = $params['amount'];
  347. $notifyUrl = $params['notifyUrl'];
  348. $subject = $params['subject'];
  349. $capitalType = $params['capitalType'];
  350. $wxAppId = $params['wxAppId'] ?? '';
  351. $ip = $this->getClientIp();
  352. $location_info = ['request_ip' => $ip];
  353. $arr = [
  354. 'merchant_no' => $this->merchant_no,
  355. 'term_no' => $this->term_no,
  356. 'out_trade_no' => $orderSn,
  357. 'account_type' => 'WECHAT',
  358. 'trans_type' => 71,
  359. 'total_amount' => bcmul($amount, 100),
  360. 'location_info' => $location_info,
  361. 'subject' => $subject,
  362. 'notify_url' => $notifyUrl,
  363. 'remark' => '{"capitalType":' . $capitalType . '}',
  364. 'acc_busi_fields' => [
  365. 'timeout_express' => 30,
  366. 'sub_appid' => $wxAppId,
  367. ],
  368. ];
  369. $baseRequestVO = [
  370. 'req_data' => $arr,
  371. 'req_time' => date("YmdHis"),
  372. 'version' => $this->version,
  373. ];
  374. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  375. $authorization = $this->getAuthorization($body);
  376. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  377. }
  378. //主扫交易,支付宝
  379. public function driveAliPay($params)
  380. {
  381. $orderSn = $params['orderSn'];
  382. $amount = $params['amount'];
  383. $notifyUrl = $params['notifyUrl'];
  384. $subject = $params['subject'];
  385. $capitalType = $params['capitalType'];
  386. $ip = $this->getClientIp();
  387. $location_info = ['request_ip' => $ip];
  388. $arr = [
  389. 'merchant_no' => $this->merchant_no,
  390. 'term_no' => $this->term_no,
  391. 'out_trade_no' => $orderSn,
  392. 'account_type' => 'ALIPAY',
  393. 'trans_type' => '41',
  394. 'total_amount' => bcmul($amount, 100),
  395. 'location_info' => $location_info,
  396. 'subject' => $subject,
  397. 'notify_url' => $notifyUrl,
  398. 'remark' => '{"capitalType":' . $capitalType . '}',
  399. ];
  400. $baseRequestVO = [
  401. 'req_data' => $arr,
  402. 'req_time' => date("YmdHis"),
  403. 'version' => $this->version,
  404. ];
  405. $body = json_encode($baseRequestVO, JSON_UNESCAPED_UNICODE);
  406. $authorization = $this->getAuthorization($body);
  407. return $this->post($this->preUrl . 'preorder', $body, $authorization);
  408. }
  409. //验签
  410. public function signatureVerification($authorization, $body)
  411. {
  412. $authorization = str_replace($this->schema . " ", "", $authorization);
  413. $authorization = str_replace(",", "&", $authorization);
  414. $authorization = str_replace("\"", "", $authorization);
  415. $authorization = $this->convertUrlQuery($authorization);
  416. $authorization['signature'] = base64_decode($authorization['signature']);
  417. $message = $authorization['timestamp'] . "\n" . $authorization['nonce_str'] . "\n" . $body . "\n";
  418. $key = openssl_get_publickey(file_get_contents($this->lklCertificatePath));
  419. $flag = openssl_verify($message, $authorization['signature'], $key, OPENSSL_ALGO_SHA256);
  420. openssl_free_key($key);
  421. if ($flag) {
  422. return true;
  423. }
  424. return false;
  425. }
  426. //签名
  427. public function getAuthorization($body)
  428. {
  429. $nonceStr = $this->randString(12);
  430. $timestamp = time();
  431. $message = $this->appid . "\n" . $this->serial_no . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";
  432. $key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));
  433. openssl_sign($message, $signature, $key, OPENSSL_ALGO_SHA256);
  434. openssl_free_key($key);
  435. return $this->schema . " appid=\"" . $this->appid . "\"," . "serial_no=\"" . $this->serial_no . "\"," . "timestamp=\"" . $timestamp . "\"," . "nonce_str=\"" . $nonceStr . "\"," . "signature=\"" . base64_encode($signature) . "\"";
  436. }
  437. //获取随机数
  438. public function randString($length)
  439. {
  440. $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  441. $randomString = "";
  442. for ($i = 0; $i < $length; $i++) {
  443. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  444. }
  445. return $randomString;
  446. }
  447. //请求
  448. public function post($url, $data, $authorization)
  449. {
  450. $headers = [
  451. "Authorization: " . $authorization,
  452. "Accept: application/json",
  453. "Content-Type:application/json",
  454. ];
  455. \Yii::info("请求参数:" . $data);
  456. $ch = curl_init();
  457. curl_setopt($ch, CURLOPT_POST, 1);
  458. curl_setopt($ch, CURLOPT_URL, $url);
  459. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  460. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  461. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  462. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
  463. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  464. curl_setopt($ch, CURLOPT_POST, 1);
  465. $res = curl_exec($ch);
  466. curl_close($ch);
  467. return json_decode($res, 1);
  468. }
  469. //签名参数转数组
  470. private function convertUrlQuery($query)
  471. {
  472. $queryParts = explode('&', $query);
  473. $params = array();
  474. foreach ($queryParts as $param) {
  475. $item = explode('=', $param);
  476. $params[$item[0]] = $item[1];
  477. }
  478. if ($params['signature']) {
  479. $params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
  480. }
  481. return $params;
  482. }
  483. public function getClientIp()
  484. {
  485. if (isset($_SERVER['REMOTE_ADDR'])) {
  486. $cip = $_SERVER['REMOTE_ADDR'];
  487. } elseif (getenv("REMOTE_ADDR")) {
  488. $cip = getenv("REMOTE_ADDR");
  489. } elseif (getenv("HTTP_CLIENT_IP")) {
  490. $cip = getenv("HTTP_CLIENT_IP");
  491. } else {
  492. if (getenv('YII_ENV') == 'production') {
  493. $cip = "114.55.169.20";
  494. } else {
  495. $cip = "118.178.193.23";
  496. }
  497. }
  498. return $cip;
  499. }
  500. }