KaroPay.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Services;
  3. use App\Util;
  4. use DateTime;
  5. use DateTimeZone;
  6. class KaroPay
  7. {
  8. /**
  9. * 生成随机字符串
  10. * @param int $length
  11. * @return string
  12. */
  13. public function getNonceStr($length = 16)
  14. {
  15. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  16. $str = "";
  17. for ($i = 0; $i < $length; $i++) {
  18. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  19. }
  20. return $str;
  21. }
  22. /**
  23. * 生成认证头信息
  24. * @param string $appId 应用ID
  25. * @param string $signKey 签名密钥
  26. * @return array 包含Authorization、AppId和UtcTime的头信息数组
  27. */
  28. public function buildAuthorizationHeader($appId, $signKey)
  29. {
  30. $utcTime = $this->getUtcTimeString();
  31. $md5SignKey = md5($signKey);
  32. // 连接appId、md5后的signKey和utcTime
  33. $value = $appId . ":" . $md5SignKey . ":" . $utcTime;
  34. $authorization = md5($value);
  35. Util::WriteLog('KaroPay', '认证信息生成: appId=' . $appId . ', utcTime=' . $utcTime . ', authorization=' . $authorization);
  36. return [
  37. 'Authorization' => $authorization,
  38. 'AppId' => $appId,
  39. 'UtcTime' => $utcTime
  40. ];
  41. }
  42. /**
  43. * 获取UTC时间字符串
  44. * @return string 格式为yyyyMMddHHmmss的UTC时间字符串
  45. */
  46. public function getUtcTimeString()
  47. {
  48. return gmdate('YmdHis');
  49. }
  50. /**
  51. * 发送POST请求
  52. * @param string $url 请求地址
  53. * @param array $post_data 请求参数
  54. * @param array $headers 请求头信息
  55. * @param int $timeout 超时时间
  56. * @return bool|string
  57. */
  58. public function curlPost($url, $post_data = array(), $headers = array(), $timeout = 20)
  59. {
  60. $post_string = json_encode($post_data);
  61. $header_array = [
  62. 'Content-Type: application/json',
  63. 'Content-Length: ' . strlen($post_string)
  64. ];
  65. if (!empty($headers)) {
  66. foreach ($headers as $key => $value) {
  67. $header_array[] = $key . ': ' . $value;
  68. }
  69. }
  70. Util::WriteLog('KaroPay', 'KaroPay请求: ' . $url . " | " . $post_string);
  71. Util::WriteLog('KaroPay', 'KaroPay请求头: ' . json_encode($header_array));
  72. $ch = curl_init();
  73. curl_setopt($ch, CURLOPT_URL, $url);
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  76. curl_setopt($ch, CURLOPT_POST, true);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  78. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  79. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  81. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
  82. $result = curl_exec($ch);
  83. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  84. if ($httpCode == 200) {
  85. Util::WriteLog('KaroPay', 'KaroPay响应: ' . $result);
  86. } else {
  87. Util::WriteLog('KaroPay_error', 'HTTP状态码错误: ' . $httpCode);
  88. Util::WriteLog('KaroPay_error', 'curl错误: ' . curl_error($ch));
  89. $result = false;
  90. }
  91. curl_close($ch);
  92. return $result;
  93. }
  94. /**
  95. * 发送GET请求
  96. * @param string $url 请求地址
  97. * @param array $headers 请求头信息
  98. * @param int $timeout 超时时间
  99. * @return bool|string
  100. */
  101. public function curlGet($url, $headers = array(), $timeout = 20)
  102. {
  103. $header_array = [
  104. 'Content-Type: application/json'
  105. ];
  106. if (!empty($headers)) {
  107. foreach ($headers as $key => $value) {
  108. $header_array[] = $key . ': ' . $value;
  109. }
  110. }
  111. Util::WriteLog('KaroPay', 'KaroPay GET请求: ' . $url);
  112. Util::WriteLog('KaroPay', 'KaroPay请求头: ' . json_encode($header_array));
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_URL, $url);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  116. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  117. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  118. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  119. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
  121. $result = curl_exec($ch);
  122. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  123. if ($httpCode == 200) {
  124. Util::WriteLog('KaroPay', 'KaroPay响应: ' . $result);
  125. } else {
  126. Util::WriteLog('KaroPay_error', 'HTTP状态码错误: ' . $httpCode);
  127. Util::WriteLog('KaroPay_error', 'curl错误: ' . curl_error($ch));
  128. $result = false;
  129. }
  130. curl_close($ch);
  131. return $result;
  132. }
  133. /**
  134. * 生成签名
  135. * @param array $params 参数数组
  136. * @param string $signKey 签名密钥
  137. * @return string 签名结果
  138. */
  139. public function sign($params, $signKey)
  140. {
  141. // 移除sign参数
  142. $newParams = $params;
  143. if (isset($newParams['sign'])) {
  144. unset($newParams['sign']);
  145. }
  146. // 按ASCII码升序排序
  147. ksort($newParams);
  148. // 拼接参数
  149. $stringA = '';
  150. foreach ($newParams as $k => $v) {
  151. if ($v !== '' && $v !== null && !is_array($v)) {
  152. $stringA .= $k . '=' . $v . '&';
  153. }
  154. }
  155. // 拼接key
  156. $stringSignTemp = $stringA . 'key=' . md5($signKey);
  157. // md5加密
  158. $sign = md5($stringSignTemp);
  159. return $sign;
  160. }
  161. /**
  162. * 验证签名
  163. * @param array $params 参数数组
  164. * @param string $signKey 签名密钥
  165. * @return bool 验证结果
  166. */
  167. public function verifySign($params, $signKey)
  168. {
  169. if (!isset($params['sign'])) {
  170. return false;
  171. }
  172. $sign = $params['sign'];
  173. $generatedSign = $this->sign($params, $signKey);
  174. return $sign === $generatedSign;
  175. }
  176. }