BsPay.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace BsPaySdk\core;
  3. use BsPaySdk\config\MerConfig;
  4. use Exception;
  5. class BsPay extends BsPayRequestV2
  6. {
  7. # 生产环境接口基础地址 v2
  8. const BASE_API_URL_V2 = "https://api.huifu.com";
  9. # 集成环境接口基础地址 v2
  10. const BASE_API_TEST_URL_V2 = "https://spin-test.cloudpnr.com";
  11. # 调试模式
  12. public static $isDebug;
  13. # 生产模式
  14. public static $isProdMode;
  15. # 日志路径
  16. public static $logDir = "";
  17. # 商户配置信息
  18. private static $merConfigMap = array();
  19. public static function init($config_info, $is_object=false, $merchantKey = 'default'){
  20. if (empty($config_info)){
  21. try {
  22. throw new Exception('缺少SDK配置信息');
  23. } catch (Exception $e) {
  24. echo $e->getMessage();
  25. }
  26. }
  27. if ($is_object){
  28. $config_obj = $config_info;
  29. }else{
  30. if (!file_exists($config_info)){
  31. try {
  32. throw new Exception('SDK配置文件不存在');
  33. } catch (Exception $e) {
  34. echo $e->getMessage();
  35. }
  36. }
  37. $cfg_file_str = file_get_contents($config_info);
  38. $config_obj = json_decode($cfg_file_str, true);
  39. // 处理 json 文件格式异常
  40. if(!$config_obj)
  41. {
  42. try {
  43. throw new Exception('SDK配置文件格式异常');
  44. } catch (Exception $e) {
  45. echo $e->getMessage();
  46. }
  47. }
  48. }
  49. // 是否调试模式
  50. self::$isDebug = defined("DEBUG") && DEBUG;
  51. // 日志路径
  52. self::$logDir = defined("DEBUG") ? LOG: dirname(__FILE__)."/log";
  53. // 是否生产模式
  54. self::$isProdMode = defined("PROD_MODE") && PROD_MODE;
  55. $merConfig = new MerConfig();
  56. $merConfig->product_id = isset($config_obj['product_id']) ? $config_obj['product_id'] : '';
  57. $merConfig->sys_id = isset($config_obj['sys_id']) ? $config_obj['sys_id'] : '';
  58. $merConfig->huifu_id = isset($config_obj['huifu_id']) ? $config_obj['huifu_id'] : '';
  59. $merConfig->rsa_merch_private_key = isset($config_obj['rsa_merch_private_key']) ? $config_obj['rsa_merch_private_key'] : '';
  60. $merConfig->rsa_huifu_public_key = isset($config_obj['rsa_huifu_public_key']) ? $config_obj['rsa_huifu_public_key'] : '';
  61. self::addMerConfig($merchantKey, $merConfig);
  62. }
  63. protected function filterEmptyData($req_params){
  64. return array_filter($req_params, function($v){
  65. if (!empty($v) || $v == '0') {
  66. return true;
  67. }
  68. return false;
  69. });
  70. }
  71. public static function writeLog($message, $level = "INFO"){
  72. if (self::$isDebug){
  73. if (!is_dir(self::$logDir)){
  74. mkdir(self::$logDir, 0777, true);
  75. }
  76. $log_file = self::$logDir."/bspay_".date("Ymd").".log";
  77. $server_addr = "127.0.0.1";
  78. if (isset($_SERVER["REMOTE_ADDR"])){
  79. $server_addr = $_SERVER["REMOTE_ADDR"];
  80. }
  81. $message_format = "[". $level ."] [".gmdate("Y-m-d\TH:i:s\Z")."] ". $server_addr." ". $message. "\n";
  82. $fp = fopen($log_file, "a+");
  83. fwrite($fp, $message_format);
  84. fclose($fp);
  85. }
  86. }
  87. /**
  88. * 根据功能编码获取实际请求路径
  89. * @param $funcCode
  90. * @return string $req_url
  91. */
  92. public function getRequestUrl($funcCode) {
  93. // 判断请求环境确定接口域名地址
  94. $req_url = self::$isProdMode ? self::BASE_API_URL_V2 : self::BASE_API_TEST_URL_V2;
  95. // 如果末尾不是/结尾,则拼接/在最后
  96. if (!BsPayTools::endWith($req_url, "/")) {
  97. $req_url = $req_url . "/";
  98. }
  99. # 根据功能编码获取实际接口请求地址
  100. $actualUrl = str_replace(".", "/", $funcCode);
  101. # 拼接得到完整请求地址
  102. return $req_url . $actualUrl;
  103. }
  104. /**
  105. * @throws Exception
  106. */
  107. public function post($funcCode, $params, $file, $merchantKey){
  108. # 获取商户配置信息
  109. $merchantConfig = self::getConfig($merchantKey);
  110. if(empty($merchantConfig)){
  111. throw new Exception('系统公私密钥配置不存在');
  112. }
  113. # 根据功能编码获取实际请求路径
  114. $req_url = $this->getRequestUrl($funcCode);
  115. $request_params = $this->filterEmptyData($params);
  116. # 获取请求头
  117. if (empty($file)){
  118. $header = array('Content-Type:application/json');
  119. $is_json = true;
  120. }else{
  121. $header = array('Content-Type:multipart/form-data');
  122. $is_json = false;
  123. }
  124. // 实例化 http 请求执行
  125. $bs_request = new BsPayRequestV2();
  126. return $bs_request->curlRequest($merchantConfig, $req_url, $request_params, $file, $header, $is_json);
  127. }
  128. public function get($funcCode, $params, $merchantKey){
  129. ksort($params);
  130. # 获取商户配置信息
  131. $merchantConfig = $this->getConfig($merchantKey);
  132. # 根据功能编码获取实际请求路径
  133. $req_url = $this->getRequestUrl($funcCode);
  134. $request_params = $this->filterEmptyData($params);
  135. # 获取请求头
  136. $header = array('Content-Type:text/html');
  137. // 实例化 http 请求执行
  138. $bs_request = new BsPayRequestV2();
  139. return $bs_request->curlRequest($merchantConfig, $req_url . "?" . http_build_query($request_params), "", $header, false);
  140. }
  141. public static function addMerConfig($merchantKey, MerConfig $merConfig) {
  142. self::$merConfigMap[$merchantKey] = $merConfig;
  143. }
  144. public static function getConfig($merChantKey = "default")
  145. {
  146. return isset(self::$merConfigMap[$merChantKey]) ? self::$merConfigMap[$merChantKey] : array();
  147. }
  148. }