FdsSdk.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. namespace Lakala\sdk;
  3. class FdsSdk {
  4. //应用ID
  5. public static $appId = "6633965341557002240";
  6. //DSA私钥文件路径
  7. public static $DSAPrivateKeyFilePath = __DIR__ . "/cert2/dsa_private_key.pfx";
  8. //DSA私钥密碼
  9. public static $DSAPrivateKeyFilePassword = "PXommGBW9gj0XWSq";
  10. //DSA公钥文件路径
  11. public static $DSAPublicKeyFilePath = __DIR__ ."/cert2/dsa_public_key.cer";
  12. //RSA私钥文件路径
  13. public static $RSAPrivateKeyFilePath = __DIR__ ."/cert2/rsa_private_key.pfx";
  14. //RSA私钥密碼
  15. public static $RSAPrivateKeyFilePassword = "uqouGAKgs4iuxOkm";
  16. //RSA公钥文件路径
  17. public static $RSAPublicKeyFilePath = __DIR__ ."/cert2/cloud_rsa_public.pem";
  18. //网关
  19. public static $gatewayUrl = "http://47.105.146.241:9510/service/soa";
  20. //api版本
  21. public static $apiVersion = "3.0";
  22. // 表单提交字符集编码
  23. public static $postCharset = "UTF-8";
  24. // 文件字符集编码
  25. private static $fileCharset = "UTF-8";
  26. //签名类型
  27. public static $signType = "DSA";
  28. const STATUS_OK = 'OK';
  29. const ERR_MESSAGE = 'message';
  30. const SIGNED_VALUE = 'result';
  31. private $bizContent;
  32. private $apiMethodName;
  33. static $bop;
  34. public static function getCashier() {
  35. if (empty(self::$bop)) {
  36. self::$bop = new FdsSdk();
  37. }
  38. return self::$bop;
  39. }
  40. public static function getService() {
  41. if (empty(self::$bop)) {
  42. self::$bop = new FdsSdk();
  43. }
  44. return self::$bop;
  45. }
  46. /**
  47. * 创建会员
  48. * @param $param
  49. * @return bool|mixed|string
  50. */
  51. public function createPersonMember($param)
  52. {
  53. $this->bizContent = json_encode($param);
  54. $this->apiMethodName = "member.create";
  55. $result = $this->execute();
  56. return $result;
  57. }
  58. /**
  59. * 会员更新
  60. * @param $param
  61. * @return bool|mixed|string
  62. */
  63. public function memberUpdate($param)
  64. {
  65. $this->bizContent = json_encode($param);
  66. $this->apiMethodName = "member.update";
  67. $result = $this->execute();
  68. return $result;
  69. }
  70. /**
  71. * 会员手机绑定申请
  72. * @param $param
  73. * @return bool|mixed|string
  74. */
  75. public function createMobileMember($param)
  76. {
  77. $this->bizContent = json_encode($param);
  78. $this->apiMethodName = "member.mobile.request";
  79. $result = $this->execute();
  80. return $result;
  81. }
  82. /**
  83. * 会员手机绑定确认
  84. * @param $param
  85. * @return bool|mixed|string
  86. */
  87. public function createMepdember($param)
  88. {
  89. $this->bizContent = json_encode($param);
  90. $this->apiMethodName = "mepdember.mobile.confirm";
  91. $result = $this->execute();
  92. return $result;
  93. }
  94. /**
  95. * 企业信息认证
  96. * @param $param
  97. * @return bool|mixed|string
  98. */
  99. public function createCompanyVerify(&$param)
  100. {
  101. $param["identity_no"]=$this->rsaEncrypt($param["identity_no"]);
  102. $this->bizContent = json_encode($param);
  103. $this->apiMethodName = "member.company.verify";
  104. $result = $this->execute();
  105. return $result;
  106. }
  107. /**
  108. * 首笔支付认证_对公卡
  109. * @param $param
  110. * @return bool|mixed|string
  111. */
  112. public function memberBankBind($param)
  113. {
  114. $param["bank_card_no"]=$this->rsaEncrypt($param["bank_card_no"]);
  115. $this->bizContent = json_encode($param);
  116. $this->apiMethodName = "member.bank.company.account.bind";
  117. $result = $this->execute();
  118. return $result;
  119. }
  120. /**
  121. * 首笔支付认证确认_对公卡
  122. * @param $param
  123. * @return bool|mixed|string
  124. */
  125. public function memberBankVerify($param)
  126. {
  127. $this->bizContent = json_encode($param);
  128. $this->apiMethodName = "member.bank.company.account.verify";
  129. $result = $this->execute();
  130. return $result;
  131. }
  132. /**
  133. * 会员开通商户
  134. * @param $param
  135. * @return bool|mixed|string
  136. */
  137. public function createMerchant($param)
  138. {
  139. $this->bizContent = json_encode($param);
  140. $this->apiMethodName = "member.merchant.open";
  141. $result = $this->execute();
  142. return $result;
  143. }
  144. /**
  145. * 开通商户确认
  146. * @param $param
  147. * @return bool|mixed|string
  148. */
  149. public function createMerchantConfirm($param)
  150. {
  151. $this->bizContent = json_encode($param);
  152. $this->apiMethodName = "member.merchant.open.confirm";
  153. $result = $this->execute();
  154. return $result;
  155. }
  156. /**
  157. * 设置结算卡
  158. * @param $param
  159. * @return bool|mixed|string
  160. */
  161. public function setSettleCard($param)
  162. {
  163. $this->bizContent = json_encode($param);
  164. $this->apiMethodName = "member.set.settle.card";
  165. $result = $this->execute();
  166. return $result;
  167. }
  168. /**
  169. * 修改分账比例申请
  170. * @param $param
  171. * @return bool|mixed|string
  172. */
  173. public function memberSplitScaleApply($param)
  174. {
  175. $this->bizContent = json_encode($param);
  176. $this->apiMethodName = "member.split.scale.apply";
  177. $result = $this->execute();
  178. return $result;
  179. }
  180. /**
  181. * 修改分账比例确认
  182. * @param $param
  183. * @return bool|mixed|string
  184. */
  185. public function memberSplitScaleQuery($param)
  186. {
  187. $this->bizContent = json_encode($param);
  188. $this->apiMethodName = "member.split.scale.query";
  189. $result = $this->execute();
  190. return $result;
  191. }
  192. /**
  193. * 商户绑定
  194. * @param $param
  195. * @return bool|mixed|string
  196. */
  197. public function memberMerchantBind($param)
  198. {
  199. $this->bizContent = json_encode($param);
  200. $this->apiMethodName = "member.merchant.bind";
  201. $result = $this->execute();
  202. return $result;
  203. }
  204. /**
  205. * 开通支付账户(URL)
  206. * @param $param
  207. * @return bool|mixed|string
  208. */
  209. public function payAccount($param)
  210. {
  211. $this->bizContent = json_encode($param);
  212. $this->apiMethodName = "member.pay.account.url.get";
  213. $result = $this->execute();
  214. return $result;
  215. }
  216. /**
  217. * 消费
  218. * @param $param
  219. * @return bool|mixed|string
  220. */
  221. public function orderConsume($param)
  222. {
  223. $this->bizContent = json_encode($param);
  224. $this->apiMethodName = "order.consume.request";
  225. $result = $this->execute();
  226. return $result;
  227. }
  228. public function getNotifyResult($request)
  229. {
  230. //获取签名
  231. $sign = $request['sign'];
  232. //组装签名原串
  233. $signStr = $request['app_id'].$request['timestamp'].$request['version'].$request['event_type'].$request['event'];
  234. //验签
  235. $seccess = $this->verify($signStr, $sign, self::$signType);
  236. if(!$seccess) {
  237. throw new Exception("返回值签名验证失败");
  238. }
  239. return $request;
  240. }
  241. public static function setAppId($appId) {
  242. return self::$appId = $appId;
  243. }
  244. public static function setServerUrl($gatewayUrl) {
  245. return self::$gatewayUrl = $gatewayUrl;
  246. }
  247. public static function getServerUrl() {
  248. return self::$gatewayUrl;
  249. }
  250. public static function setApiVersion($apiVersion) {
  251. return self::$apiVersion = $apiVersion;
  252. }
  253. public static function setDSAKey($DSAPublicKeyFilePath, $DSAPrivateKeyFilePath, $DSAPrivateKeyFilePassword) {
  254. self::$DSAPublicKeyFilePath = $DSAPublicKeyFilePath;
  255. self::$DSAPrivateKeyFilePath = $DSAPrivateKeyFilePath;
  256. self::$DSAPrivateKeyFilePassword = $DSAPrivateKeyFilePassword;
  257. }
  258. public static function setRSAKey($RSAPublicKeyFilePath, $RSAPrivateKeyFilePath, $RSAPrivateKeyFilePassword) {
  259. self::$RSAPublicKeyFilePath = $RSAPublicKeyFilePath;
  260. self::$RSAPrivateKeyFilePath = $RSAPrivateKeyFilePath;
  261. self::$RSAPrivateKeyFilePassword = $RSAPrivateKeyFilePassword;
  262. }
  263. public function execute($authToken = null, $appInfoAuthtoken = null) {
  264. // 如果两者编码不一致,会出现签名验签或者乱码
  265. if (strcasecmp(self::$fileCharset, self::$postCharset)) {
  266. // writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!");
  267. throw new Exception("文件编码:[" . self::$fileCharset . "] 与表单提交编码:[" . self::$postCharset . "]两者不一致!");
  268. }
  269. $iv = self::$apiVersion;
  270. //组装系统参数
  271. $sysParams["app_id"] = self::$appId;
  272. $sysParams["version"] = $iv;
  273. $sysParams["service"] = $this->apiMethodName;
  274. $sysParams["timestamp"] = time();
  275. //获取业务参数
  276. $sysParams["params"] = $this->bizContent;
  277. //签名
  278. $sysParams["sign"] = $this->generateSign($sysParams, self::$signType);
  279. $result = $this->request($sysParams);
  280. $result = $this->checkResult($result);
  281. return $result;
  282. }
  283. public function execute2($authToken = null, $appInfoAuthtoken = null) {
  284. // 如果两者编码不一致,会出现签名验签或者乱码
  285. if (strcasecmp(self::$fileCharset, self::$postCharset)) {
  286. // writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!");
  287. throw new Exception("文件编码:[" . self::$fileCharset . "] 与表单提交编码:[" . self::$postCharset . "]两者不一致!");
  288. }
  289. $iv = self::$apiVersion;
  290. //组装系统参数
  291. $sysParams["app_id"] = self::$appId;
  292. $sysParams["version"] = $iv;
  293. $sysParams["service"] = $this->apiMethodName;
  294. $sysParams["timestamp"] = time();
  295. //获取业务参数
  296. foreach ($this->bizContent as $key=>$value) {
  297. $sysParams[$key] = $value;
  298. }
  299. //签名
  300. $sysParams["sign"] = $this->generateSign($sysParams, self::$signType);
  301. $result = $this->request2($sysParams);
  302. $result = $this->checkResult($result);
  303. return $result;
  304. }
  305. public function execute3($authToken = null, $appInfoAuthtoken = null) {
  306. // 如果两者编码不一致,会出现签名验签或者乱码
  307. if (strcasecmp(self::$fileCharset, self::$postCharset)) {
  308. // writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!");
  309. throw new Exception("文件编码:[" . self::$fileCharset . "] 与表单提交编码:[" . self::$postCharset . "]两者不一致!");
  310. }
  311. $iv = self::$apiVersion;
  312. //组装系统参数
  313. $sysParams["app_id"] = self::$appId;
  314. $sysParams["version"] = $iv;
  315. $sysParams["service"] = $this->apiMethodName;
  316. $sysParams["timestamp"] = time();
  317. //获取业务参数
  318. $sysParams["params"] = $this->bizContent;
  319. //签名
  320. $sysParams["sign"] = $this->generateSign($sysParams, self::$signType);
  321. return $sysParams;
  322. }
  323. private function request($args){
  324. global $log;
  325. $ch = curl_init () ;
  326. curl_setopt($ch, CURLOPT_URL, self::$gatewayUrl);
  327. $sb = '';
  328. $reqbody = array();
  329. foreach($args as $entry_key => $entry_value){
  330. $sb .= $entry_key.'='.urlencode($entry_value).'&';
  331. }
  332. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  333. curl_setopt($ch, CURLOPT_HEADER, 0);
  334. curl_setopt($ch, CURLOPT_POST, 1);
  335. curl_setopt($ch, CURLOPT_POSTFIELDS, $sb);
  336. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  337. curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
  338. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-length', count($reqbody)));
  339. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  340. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  341. $result = curl_exec($ch);
  342. curl_close($ch);
  343. return $result;
  344. }
  345. private function request2($args){
  346. global $log;
  347. $ch = curl_init () ;
  348. curl_setopt($ch, CURLOPT_URL, self::$gatewayUrl);
  349. $sb2 = array();
  350. $reqbody = array();
  351. foreach($args as $entry_key => $entry_value){
  352. $sb2[$entry_key] = $entry_value;
  353. }
  354. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  355. curl_setopt($ch, CURLOPT_HEADER, 0);
  356. curl_setopt($ch, CURLOPT_POST, 1);
  357. curl_setopt($ch, CURLOPT_POSTFIELDS, $sb2);
  358. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  359. curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
  360. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-length', count($reqbody)));
  361. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  362. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  363. $result = curl_exec($ch);
  364. curl_close($ch);
  365. return $result;
  366. }
  367. /**
  368. * 检查服务调用结果是否正确,如果服务调用失败,则抛出失败异常
  369. * @param result 服务调用的返回结果对象
  370. * @throws Exception
  371. */
  372. private function checkResult($result){
  373. global $log;
  374. $arr = json_decode($result, true);
  375. if (is_string($arr['response'])) {
  376. $response = json_decode($arr['response'], true);
  377. } else {
  378. $response = $arr['response'];
  379. }
  380. $sign = $arr['sign'];
  381. if($sign != null){
  382. $seccess = $this->verify($arr['response'], $sign, self::$signType);
  383. if(!$seccess)
  384. throw new Exception("返回值签名验证失败");
  385. }
  386. return $response;
  387. }
  388. /**
  389. * 校验$value是否非空
  390. * if not set ,return true;
  391. * if is null , return true;
  392. **/
  393. protected function checkEmpty($value) {
  394. if (!isset($value))
  395. return true;
  396. if ($value === null)
  397. return true;
  398. if (trim($value) === "")
  399. return true;
  400. return false;
  401. }
  402. //验签
  403. public function verify($data, $sign, $signType = 'RSA') {
  404. //读取公钥文件
  405. $pubKey = file_get_contents(self::$DSAPublicKeyFilePath);
  406. //转换为openssl格式密钥
  407. $res = openssl_get_publickey($pubKey);
  408. // ($res) or die('公钥错误。请检查公钥文件格式是否正确');
  409. if (!$res){
  410. return '公钥错误。请检查公钥文件格式是否正确';
  411. }
  412. //调用openssl内置方法验签,返回bool值
  413. if (!is_string($data)) {
  414. $data = json_encode($data);
  415. }
  416. if ("RSA2" == $signType) {
  417. $result = openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
  418. } else if ("DSA" == $signType) {
  419. $result = openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_DSS1);
  420. } else {
  421. $result = openssl_verify($data, base64_decode($sign), $res);
  422. }
  423. if ($result == 1) {
  424. $result = true;
  425. } else {
  426. $result = false;
  427. }
  428. if(!$this->checkEmpty(self::$DSAPublicKeyFilePath)){
  429. openssl_free_key($res);
  430. }
  431. return $result;
  432. }
  433. public function generateSign($params, $signType = "DSA") {
  434. return $this->sign($this->getSignContent($params), $signType);
  435. }
  436. protected function getSignContent($params) {
  437. $stringToBeSigned = '';
  438. if(array_key_exists("app_id", $params)) {
  439. $stringToBeSigned .= $params['app_id'];
  440. }
  441. if(array_key_exists("timestamp", $params)) {
  442. $stringToBeSigned .= $params['timestamp'];
  443. }
  444. if(array_key_exists("version", $params)) {
  445. $stringToBeSigned .= $params['version'];
  446. }
  447. if(array_key_exists("service", $params)) {
  448. $stringToBeSigned .= $params['service'];
  449. }
  450. if(array_key_exists("params", $params)) {
  451. $stringToBeSigned .= $params['params'];
  452. }
  453. if(array_key_exists("image_type", $params)) {
  454. $stringToBeSigned .= $params['image_type'];
  455. }
  456. if(array_key_exists("image_name", $params)) {
  457. $stringToBeSigned .= $params['image_name'];
  458. }
  459. //$stringToBeSigned = $params['app_id'] . $params['timestamp'] . $params['version'] . $params['service'] . $params['params'];
  460. $stringToBeSigned = str_replace("\r\n", "", $stringToBeSigned);
  461. return $stringToBeSigned;
  462. }
  463. protected function sign($data, $signType = "DSA") {
  464. if ("DSA" == $signType) {
  465. $file = file_get_contents(self::$DSAPrivateKeyFilePath);
  466. openssl_pkcs12_read($file, $res, self::$DSAPrivateKeyFilePassword);
  467. } else {
  468. $priKey = file_get_contents(self::$RSAPrivateKeyFilePath);
  469. $res = openssl_get_privatekey($priKey, self::$RSAPrivateKeyFilePassword);
  470. }
  471. // ($res) or die('您使用的私钥格式错误,请检查私钥配置');
  472. if (!$res){
  473. return '您使用的私钥格式错误,请检查私钥配置';
  474. }
  475. if ("RSA2" == $signType) {
  476. openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
  477. } else if ("DSA" == $signType) {
  478. openssl_sign($data, $sign, $res['pkey'], OPENSSL_ALGO_DSS1);
  479. } else {
  480. openssl_sign($data, $sign, $res);
  481. }
  482. if("DSA" != $signType && !$this->checkEmpty(self::$DSAPrivateKeyFilePath)){
  483. openssl_free_key($res);
  484. }
  485. $sign = base64_encode($sign);
  486. return $sign;
  487. }
  488. public function rsaEncrypt($data) {
  489. //读取公钥文件
  490. $pubKey = file_get_contents(self::$RSAPublicKeyFilePath);
  491. //转换为openssl格式密钥
  492. $res = openssl_get_publickey($pubKey);
  493. if(false === $res){
  494. throw new \Exception("open pubkey error");
  495. }
  496. if (!openssl_public_encrypt($data, $chrtext, $res, OPENSSL_PKCS1_PADDING)) {
  497. echo "<br/>" . openssl_error_string() . "<br/>";
  498. }
  499. return base64_encode($chrtext);
  500. }
  501. public function rsaDecrypt($data) {
  502. //读取私钥文件
  503. $priKey = file_get_contents(self::$RSAPrivateKeyFilePath);
  504. //转换为openssl格式密钥
  505. openssl_pkcs12_read($priKey, $res, self::$RSAPrivateKeyFilePassword);
  506. $res1 = openssl_pkey_get_private($res['pkey']);
  507. if (!openssl_private_decrypt(base64_decode($data), $dcyCont, $res1, OPENSSL_PKCS1_PADDING)) {
  508. echo "<br/>---" . openssl_error_string() . "<br/>";
  509. }
  510. return $dcyCont;
  511. }
  512. }