Lakala.php 18 KB

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