BotImPayService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Services;
  3. use App\Util;
  4. class BotImPayService
  5. {
  6. public $config;
  7. public $merNo;
  8. public $apiUrl;
  9. public $privateKey;
  10. public $platformPublicKey;
  11. public function __construct($configKey = 'BotImPay')
  12. {
  13. $payConfigService = new PayConfig();
  14. $this->config = $payConfigService->getConfig($configKey);
  15. $this->merNo = $this->config['mer_no'] ?? '';
  16. $this->apiUrl = $this->config['apiUrl'] ?? 'https://api.botimpay.top';
  17. $this->privateKey = $this->config['private_key'] ?? '';
  18. $this->platformPublicKey = $this->config['platform_public_key'] ?? '';
  19. }
  20. /**
  21. * 构建待签名数据(ASCII升序、QueryString格式、跳过sign和空值)
  22. */
  23. public function buildSignData(array $params): string
  24. {
  25. $signData = [];
  26. foreach ($params as $key => $value) {
  27. if ($key === 'sign') {
  28. continue;
  29. }
  30. if ($value === null || $value === '' || (is_array($value) && empty($value))) {
  31. continue;
  32. }
  33. if (is_array($value) || is_object($value)) {
  34. $signData[$key] = json_encode($value, JSON_UNESCAPED_UNICODE);
  35. } else {
  36. $signData[$key] = (string)$value;
  37. }
  38. }
  39. ksort($signData);
  40. $result = '';
  41. foreach ($signData as $key => $value) {
  42. $result .= '&' . $key . '=' . $value;
  43. }
  44. return substr($result, 1);
  45. }
  46. /**
  47. * RSA SHA256 签名(使用商户私钥)
  48. */
  49. public function sign(array $params): array
  50. {
  51. $signData = $this->buildSignData($params);
  52. $prvKeyPem = $this->convertPrivateKeyToPem($this->privateKey);
  53. $signature = '';
  54. $signed = openssl_sign($signData, $signature, $prvKeyPem, OPENSSL_ALGO_SHA256);
  55. if (!$signed) {
  56. throw new \Exception('BotImPay RSA签名失败');
  57. }
  58. $params['sign'] = base64_encode($signature);
  59. Util::WriteLog('BotImPay_sign', "待签名字符串: " . $signData);
  60. Util::WriteLog('BotImPay_sign', "签名结果: " . $params['sign']);
  61. return $params;
  62. }
  63. /**
  64. * RSA SHA256 验签(使用平台公钥)
  65. */
  66. public function verifySign(array $params): bool
  67. {
  68. $receivedSign = $params['sign'] ?? '';
  69. if (empty($receivedSign)) {
  70. Util::WriteLog('BotImPay', '验签失败:sign字段为空');
  71. return false;
  72. }
  73. $signData = $this->buildSignData($params);
  74. $pubKeyPem = $this->convertPublicKeyToPem($this->platformPublicKey);
  75. $isVerified = openssl_verify(
  76. $signData,
  77. base64_decode($receivedSign),
  78. $pubKeyPem,
  79. OPENSSL_ALGO_SHA256
  80. );
  81. Util::WriteLog('BotImPay_verify', "验签数据: " . $signData);
  82. Util::WriteLog('BotImPay_verify', "验签结果: " . ($isVerified === 1 ? '通过' : '失败'));
  83. return $isVerified === 1;
  84. }
  85. /**
  86. * 转换私钥为PEM格式
  87. */
  88. private function convertPrivateKeyToPem($privateKeyString)
  89. {
  90. if (strpos($privateKeyString, '-----BEGIN PRIVATE KEY-----') !== false) {
  91. return $privateKeyString;
  92. }
  93. $key = trim($privateKeyString);
  94. if (!preg_match('/^-----BEGIN/', $key)) {
  95. if (base64_decode($key, true)) {
  96. $key = base64_decode($key);
  97. }
  98. $key = "-----BEGIN PRIVATE KEY-----\n" . chunk_split(base64_encode($key), 64, "\n") . "-----END PRIVATE KEY-----";
  99. }
  100. return $key;
  101. }
  102. /**
  103. * 转换公钥为PEM格式
  104. */
  105. private function convertPublicKeyToPem($publicKeyString)
  106. {
  107. if (strpos($publicKeyString, '-----BEGIN PUBLIC KEY-----') !== false) {
  108. return $publicKeyString;
  109. }
  110. $key = trim($publicKeyString);
  111. if (!preg_match('/^-----BEGIN/', $key)) {
  112. if (base64_decode($key, true)) {
  113. $key = base64_decode($key);
  114. }
  115. $key = "-----BEGIN PUBLIC KEY-----\n" . chunk_split(base64_encode($key), 64, "\n") . "-----END PUBLIC KEY-----";
  116. }
  117. return $key;
  118. }
  119. /**
  120. * POST JSON请求
  121. */
  122. public function curlPost($url, $payload)
  123. {
  124. $timeout = 20;
  125. $data = json_encode($payload, JSON_UNESCAPED_UNICODE);
  126. $headers = [
  127. 'Content-Type: application/json',
  128. ];
  129. $ch = curl_init();
  130. curl_setopt($ch, CURLOPT_URL, $url);
  131. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  133. curl_setopt($ch, CURLOPT_POST, 1);
  134. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  135. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  136. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  137. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  138. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  139. $result = curl_exec($ch);
  140. if (curl_errno($ch)) {
  141. $error = curl_error($ch);
  142. Util::WriteLog('BotImPay_error', 'CURL Error: ' . $error);
  143. curl_close($ch);
  144. return false;
  145. }
  146. curl_close($ch);
  147. return $result;
  148. }
  149. }