AdaPay.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace AdaPay;
  3. use AdaPay\utils\AdaRequests;
  4. use AdaPay\utils\AdaTools;
  5. class AdaPay
  6. {
  7. public static $api_key = "";
  8. public static $rsaPrivateKeyFilePath = "";
  9. public static $rsaPrivateKey = "";
  10. # 不允许修改
  11. public static $rsaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwN6xgd6Ad8v2hIIsQVnbt8a3JituR8o4Tc3B5WlcFR55bz4OMqrG/356Ur3cPbc2Fe8ArNd/0gZbC9q56Eb16JTkVNA/fye4SXznWxdyBPR7+guuJZHc/VW2fKH2lfZ2P3Tt0QkKZZoawYOGSMdIvO+WqK44updyax0ikK6JlNQIDAQAB";
  12. public static $header = array('Content-Type:application/json');
  13. public static $headerText = array('Content-Type:text/html');
  14. public static $headerEmpty = array('Content-Type:multipart/form-data');
  15. public static $gateWayUrl = "";
  16. public static $gateWayType = "api";
  17. public static $mqttAddress = "post-cn-0pp18zowf0m.mqtt.aliyuncs.com:1883";
  18. public static $mqttInstanceId = "post-cn-0pp18zowf0m";
  19. public static $mqttGroupId = "GID_CRHS_ASYN";
  20. public static $mqttAccessKey = "LTAIOP5RkeiuXieW";
  21. public static $isDebug;
  22. public static $logDir = "";
  23. public $postCharset = "utf-8";
  24. public $signType = "RSA2";
  25. public $ada_request = "";
  26. public $ada_tools = "";
  27. public $statusCode= 200;
  28. public $result = array();
  29. public function __construct()
  30. {
  31. $this->ada_request = new AdaRequests();
  32. $this->ada_tools = new AdaTools();
  33. self::getGateWayUrl(self::$gateWayType);
  34. $this->__init_params();
  35. }
  36. public static function init($config_info, $prod_mode="live", $is_object=false){
  37. if (empty($config_info)){
  38. try {
  39. throw new \Exception('缺少SDK配置信息');
  40. } catch (\Exception $e) {
  41. echo $e->getMessage();
  42. }
  43. }
  44. if ($is_object){
  45. $config_obj = $config_info;
  46. }else{
  47. if (!file_exists($config_info)){
  48. try {
  49. throw new \Exception('SDK配置文件不存在');
  50. } catch (\Exception $e) {
  51. echo $e->getMessage();
  52. }
  53. }
  54. $cfg_file_str=null;
  55. $cfg_file_str = file_get_contents($config_info);
  56. $config_obj = json_decode($cfg_file_str, true);
  57. }
  58. $sdk_version = defined("SDK_VERSION") ? SDK_VERSION : "v1.0.0";
  59. array_push(self::$header, "sdk_version:". $sdk_version);
  60. array_push(self::$headerText, "sdk_version:". $sdk_version);
  61. array_push(self::$headerEmpty, "sdk_version:". $sdk_version);
  62. self::$isDebug = defined("DEBUG") ? DEBUG: false;
  63. self::$logDir = defined("DEBUG") ? LOG: dirname(__FILE__)."/log";
  64. $project_env = defined("ENV") ? ENV : "prod";
  65. self::init_mqtt($project_env);
  66. if ($prod_mode == 'live'){
  67. self::$api_key = isset($config_obj['api_key_live']) ? $config_obj['api_key_live'] : '';
  68. }
  69. if ( $prod_mode == 'test'){
  70. self::$api_key = isset($config_obj['api_key_test']) ? $config_obj['api_key_test'] : '';
  71. }
  72. if (isset($config_obj['rsa_public_key']) && $config_obj['rsa_public_key']){
  73. self::$rsaPublicKey = $config_obj['rsa_public_key'];
  74. }
  75. if (isset($config_obj['rsa_private_key']) && $config_obj['rsa_private_key']){
  76. self::$rsaPrivateKey = $config_obj['rsa_private_key'];
  77. }
  78. }
  79. public static function getGateWayUrl($type){
  80. self::$gateWayUrl = defined("GATE_WAY_URL") ? sprintf(GATE_WAY_URL, $type) : "https://api.adapay.tech";
  81. }
  82. public static function setApiKey($api_key){
  83. self::$api_key =$api_key;
  84. }
  85. public static function setRsaPublicKey($pub_key){
  86. self::$rsaPublicKey = $pub_key;
  87. }
  88. public static function setRsaPrivateKey($PrivateKey){
  89. self::$rsaPrivateKey = $PrivateKey;
  90. }
  91. public function SetParams()
  92. {
  93. $this->ada_tools->rsaPrivateKey = self::$rsaPrivateKey;
  94. $this->ada_tools->rsaPublicKey = self::$rsaPublicKey;
  95. }
  96. public function __init_params(){
  97. $this->ada_tools->rsaPrivateKey = self::$rsaPrivateKey;
  98. $this->ada_tools->rsaPublicKey = self::$rsaPublicKey;
  99. }
  100. protected function get_request_header($req_url, $post_data, $header=array()){
  101. array_push($header, 'Authorization:'.self::$api_key);
  102. array_push($header, 'Signature:'.$this->ada_tools->generateSignature($req_url, $post_data));
  103. return $header;
  104. }
  105. protected function handleResult(){
  106. $json_result_data = json_decode($this->result[1], true);
  107. if (isset($json_result_data['data'])){
  108. return json_decode($json_result_data['data'], true);
  109. }
  110. return [];
  111. }
  112. protected function do_empty_data($req_params){
  113. $req_params = array_filter($req_params, function($v){
  114. if (!empty($v) || $v == '0') {
  115. return true;
  116. }
  117. return false;
  118. });
  119. return $req_params;
  120. }
  121. public static function writeLog($message, $level = "INFO"){
  122. if (self::$isDebug){
  123. if (!is_dir(self::$logDir)){
  124. mkdir(self::$logDir, 0777, true);
  125. }
  126. $log_file = self::$logDir."/adapay_".date("Ymd").".log";
  127. $server_addr = "127.0.0.1";
  128. if (isset($_SERVER["REMOTE_ADDR"])){
  129. $server_addr = $_SERVER["REMOTE_ADDR"];
  130. }
  131. $message_format = "[". $level ."] [".gmdate("Y-m-d\TH:i:s\Z")."] ". $server_addr." ". $message. "\n";
  132. $fp = fopen($log_file, "a+");
  133. fwrite($fp, $message_format);
  134. fclose($fp);
  135. }
  136. }
  137. public static function init_mqtt($project_env){
  138. if (isset($project_env) && $project_env == "test"){
  139. self::$mqttAddress = "post-cn-459180sgc02.mqtt.aliyuncs.com:1883";
  140. self::$mqttGroupId = "GID_CRHS_ASYN";
  141. self::$mqttInstanceId = "post-cn-459180sgc02";
  142. self::$mqttAccessKey = "LTAILQZEm73RcxhY";
  143. }
  144. }
  145. public function isError(){
  146. if (empty( $this->result )){
  147. return true;
  148. }
  149. $this->statusCode = $this->result[0];
  150. $resp_str = $this->result[1];
  151. $resp_arr = json_decode($resp_str, true);
  152. $resp_data = isset($resp_arr['data']) ? $resp_arr['data'] : '';
  153. $resp_sign = isset($resp_arr['signature']) ? $resp_arr['signature'] : '';
  154. $resp_data_decode = json_decode($resp_data, true);
  155. if ($resp_sign && $this->statusCode != 401){
  156. if ($this->ada_tools->verifySign($resp_sign, $resp_data)){
  157. if ($this->statusCode != 200){
  158. $this->result = $resp_data_decode;
  159. return true;
  160. }else{
  161. $this->result = $resp_data_decode;
  162. return false;
  163. }
  164. }else{
  165. $this->result = [
  166. 'failure_code'=> 'resp_sign_verify_failed',
  167. 'failure_msg'=> '接口结果返回签名验证失败',
  168. 'status'=> 'failed'
  169. ];
  170. return true;
  171. }
  172. }else{
  173. $this->result = $resp_arr;
  174. return true;
  175. }
  176. }
  177. }