AdaPay.php 6.7 KB

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