Lakala.php 18 KB

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