BaseClient.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\Payment\Kernel;
  11. use EasyWeChat\Kernel\Support;
  12. use EasyWeChat\Kernel\Traits\HasHttpRequests;
  13. use EasyWeChat\Payment\Application;
  14. use GuzzleHttp\MessageFormatter;
  15. use GuzzleHttp\Middleware;
  16. use Psr\Http\Message\ResponseInterface;
  17. /**
  18. * Class BaseClient.
  19. *
  20. * @author overtrue <i@overtrue.me>
  21. */
  22. class BaseClient
  23. {
  24. use HasHttpRequests { request as performRequest; }
  25. /**
  26. * @var \EasyWeChat\Payment\Application
  27. */
  28. protected $app;
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct(Application $app)
  33. {
  34. $this->app = $app;
  35. $this->setHttpClient($this->app['http_client']);
  36. }
  37. /**
  38. * Extra request params.
  39. *
  40. * @return array
  41. */
  42. protected function prepends()
  43. {
  44. return [];
  45. }
  46. /**
  47. * Make a API request.
  48. *
  49. * @param string $method
  50. * @param bool $returnResponse
  51. *
  52. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  53. *
  54. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  55. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  56. * @throws \GuzzleHttp\Exception\GuzzleException
  57. */
  58. protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false)
  59. {
  60. $base = [
  61. 'mch_id' => $this->app['config']['mch_id'],
  62. 'nonce_str' => uniqid(),
  63. 'sub_mch_id' => $this->app['config']['sub_mch_id'],
  64. 'sub_appid' => $this->app['config']['sub_appid'],
  65. ];
  66. if ($endpoint=='papay/deletecontract'){
  67. unset($base['nonce_str']);
  68. }
  69. $params = array_filter(array_merge($base, $this->prepends(), $params), 'strlen');
  70. @file_put_contents("quanju.txt", json_encode($params) . "-参数解析2\r\n", 8);
  71. $secretKey = $this->app->getKey($endpoint);
  72. $encryptMethod = Support\get_encrypt_method(Support\Arr::get($params, 'sign_type', 'MD5'), $secretKey);
  73. $params['sign'] = Support\generate_sign($params, $secretKey, $encryptMethod);
  74. $options = array_merge([
  75. 'body' => Support\XML::build($params),
  76. ], $options);
  77. $this->pushMiddleware($this->logMiddleware(), 'log');
  78. $response = $this->performRequest($endpoint, $method, $options);
  79. return $returnResponse ? $response : $this->castResponseToType($response, $this->app->config->get('response_type'));
  80. }
  81. /**
  82. * Log the request.
  83. *
  84. * @return \Closure
  85. */
  86. protected function logMiddleware()
  87. {
  88. $formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG);
  89. return Middleware::log($this->app['logger'], $formatter);
  90. }
  91. /**
  92. * Make a request and return raw response.
  93. *
  94. * @param string $method
  95. *
  96. * @return ResponseInterface
  97. *
  98. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  99. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  100. * @throws \GuzzleHttp\Exception\GuzzleException
  101. */
  102. protected function requestRaw(string $endpoint, array $params = [], $method = 'post', array $options = [])
  103. {
  104. /** @var ResponseInterface $response */
  105. $response = $this->request($endpoint, $params, $method, $options, true);
  106. return $response;
  107. }
  108. /**
  109. * Make a request and return an array.
  110. *
  111. * @param string $method
  112. *
  113. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  114. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  115. * @throws \GuzzleHttp\Exception\GuzzleException
  116. */
  117. protected function requestArray(string $endpoint, array $params = [], $method = 'post', array $options = []): array
  118. {
  119. $response = $this->requestRaw($endpoint, $params, $method, $options);
  120. return $this->castResponseToType($response, 'array');
  121. }
  122. /**
  123. * Request with SSL.
  124. *
  125. * @param string $endpoint
  126. * @param string $method
  127. *
  128. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  129. *
  130. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  131. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  132. * @throws \GuzzleHttp\Exception\GuzzleException
  133. */
  134. protected function safeRequest($endpoint, array $params, $method = 'post', array $options = [])
  135. {
  136. $options = array_merge([
  137. 'cert' => $this->app['config']->get('cert_path'),
  138. 'ssl_key' => $this->app['config']->get('key_path'),
  139. ], $options);
  140. return $this->request($endpoint, $params, $method, $options);
  141. }
  142. /**
  143. * Wrapping an API endpoint.
  144. */
  145. protected function wrap(string $endpoint): string
  146. {
  147. return $this->app->inSandbox() ? "sandboxnew/{$endpoint}" : $endpoint;
  148. }
  149. }