AiPay.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Services;
  3. use App\Util;
  4. use Exception;
  5. class AiPay
  6. {
  7. protected $config;
  8. protected $key;
  9. protected $baseUrl;
  10. public function __construct(string $configKey = 'AiPay')
  11. {
  12. $payConfigService = new PayConfig();
  13. $this->config = $payConfigService->getConfig($configKey);
  14. $this->key = $this->config['key'] ?? '';
  15. $this->baseUrl = rtrim($this->config['apiUrl'] ?? 'https://api.7aipay.com', '/');
  16. }
  17. public function getConfig(): array
  18. {
  19. return $this->config;
  20. }
  21. public function sign(array $params): array
  22. {
  23. $params['sign'] = $this->generateSign($params);
  24. return $params;
  25. }
  26. public function verify(array $params): bool
  27. {
  28. if (!isset($params['sign'])) {
  29. return false;
  30. }
  31. $sign = strtoupper($params['sign']);
  32. unset($params['sign']);
  33. return $sign === $this->generateSign($params);
  34. }
  35. public function post(string $path, array $payload)
  36. {
  37. $url = $this->baseUrl . $path;
  38. $body = json_encode($payload, JSON_UNESCAPED_UNICODE);
  39. $headers = [
  40. 'Content-Type: application/json;charset=UTF-8',
  41. 'version: 2',
  42. ];
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  47. curl_setopt($ch, CURLOPT_POST, 1);
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  49. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  50. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  52. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  53. $result = curl_exec($ch);
  54. if (curl_errno($ch)) {
  55. Util::WriteLog('AiPay_error', 'CURL Error: ' . curl_error($ch));
  56. }
  57. curl_close($ch);
  58. return $result;
  59. }
  60. protected function generateSign(array $params): string
  61. {
  62. $signParams = [];
  63. foreach ($params as $key => $value) {
  64. if ($key === 'sign' || $value === null || $value === '' || (is_array($value) && empty($value))) {
  65. continue;
  66. }
  67. if (is_array($value)) {
  68. // $signParams[$key] = $this->buildObjectString($value);
  69. $signParams[$key] = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);;
  70. } else {
  71. $signParams[$key] = $value;
  72. }
  73. }
  74. ksort($signParams);
  75. $pairs = [];
  76. foreach ($signParams as $key => $value) {
  77. $pairs[] = $key . '=' . $value;
  78. }
  79. $stringA = implode('&', $pairs);
  80. $stringSign = $stringA . '&key=' . $this->key;
  81. return strtoupper(md5($stringSign));
  82. }
  83. protected function buildObjectString($data): string
  84. {
  85. if (is_array($data)) {
  86. ksort($data);
  87. $parts = [];
  88. foreach ($data as $k => $v) {
  89. if ($v === null || $v === '') {
  90. continue;
  91. }
  92. if (is_array($v)) {
  93. $parts[] = $k . '=' . $this->buildObjectString($v);
  94. } else {
  95. $parts[] = $k . '=' . $v;
  96. }
  97. }
  98. return implode('&', $parts);
  99. }
  100. return (string)$data;
  101. }
  102. }