BaseClient.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\easywechat;
  12. use EasyWeChat\Core\AbstractAPI;
  13. use EasyWeChat\Core\AccessToken;
  14. use EasyWeChat\Core\Exceptions\HttpException;
  15. use EasyWeChat\Core\Exceptions\InvalidConfigException;
  16. use EasyWeChat\Core\Http;
  17. use EasyWeChat\Encryption\EncryptionException;
  18. use think\exception\InvalidArgumentException;
  19. class BaseClient extends AbstractAPI
  20. {
  21. protected $app;
  22. const KEY_LENGTH_BYTE = 32;
  23. const AUTH_TAG_LENGTH_BYTE = 16;
  24. public function __construct(AccessToken $accessToken, $app)
  25. {
  26. parent::__construct($accessToken);
  27. $this->app = $app;
  28. }
  29. /**
  30. * @param $api
  31. * @param $params
  32. * @return \EasyWeChat\Support\Collection|null
  33. * @throws \EasyWeChat\Core\Exceptions\HttpException
  34. */
  35. protected function httpPostJson($api, $params)
  36. {
  37. try {
  38. return $this->parseJSON('json', [$api, $params]);
  39. } catch (HttpException $e) {
  40. $code = $e->getCode();
  41. throw new HttpException("接口异常[$code]" . ($e->getMessage()), $code);
  42. }
  43. }
  44. /**
  45. * @param $api
  46. * @param $params
  47. * @return \EasyWeChat\Support\Collection|null
  48. * @throws \EasyWeChat\Core\Exceptions\HttpException
  49. */
  50. protected function httpPost($api, $params)
  51. {
  52. try {
  53. return $this->parseJSON('post', [$api, $params]);
  54. } catch (HttpException $e) {
  55. $code = $e->getCode();
  56. throw new HttpException("接口异常[$code]" . ($e->getMessage()), $code);
  57. }
  58. }
  59. /**
  60. * @param $api
  61. * @param $params
  62. * @return \EasyWeChat\Support\Collection|null
  63. * @throws \EasyWeChat\Core\Exceptions\HttpException
  64. */
  65. protected function httpGet($api, $params)
  66. {
  67. try {
  68. return $this->parseJSON('get', [$api, $params]);
  69. } catch (HttpException $e) {
  70. $code = $e->getCode();
  71. throw new HttpException("接口异常[$code]" . ($e->getMessage()), $code);
  72. }
  73. }
  74. /**
  75. * request.
  76. *
  77. * @param string $endpoint
  78. * @param string $method
  79. * @param array $options
  80. * @param bool $returnResponse
  81. */
  82. public function request(string $endpoint, string $method = 'POST', array $options = [], $serial = true)
  83. {
  84. $sign_body = $options['sign_body'] ?? '';
  85. $headers = [
  86. 'Content-Type' => 'application/json',
  87. 'User-Agent' => 'curl',
  88. 'Accept' => 'application/json',
  89. 'Authorization' => $this->getAuthorization($endpoint, $method, $sign_body),
  90. // 'Wechatpay-Serial' => $this->app['config']['payment']['serial_no']
  91. ];
  92. $options['headers'] = array_merge($headers, ($options['headers'] ?? []));
  93. if ($serial) $options['headers']['Wechatpay-Serial'] = $this->app->certficates->get()['serial_no'];
  94. Http::setDefaultOptions($options);
  95. return $this->_doRequestCurl($method, 'https://api.mch.weixin.qq.com' . $endpoint, $options);
  96. }
  97. private function _doRequestCurl($method, $location, $options = [])
  98. {
  99. $curl = curl_init();
  100. // POST数据设置
  101. if (strtolower($method) === 'post') {
  102. curl_setopt($curl, CURLOPT_POST, true);
  103. curl_setopt($curl, CURLOPT_POSTFIELDS, $options['data'] ?? $options['sign_body'] ?? '');
  104. }
  105. // CURL头信息设置
  106. if (!empty($options['headers'])) {
  107. $headers = [];
  108. foreach ($options['headers'] as $k => $v) {
  109. $headers[] = "$k: $v";
  110. }
  111. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  112. }
  113. curl_setopt($curl, CURLOPT_URL, $location);
  114. curl_setopt($curl, CURLOPT_HEADER, true);
  115. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  116. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  117. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  118. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  119. $content = curl_exec($curl);
  120. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  121. curl_close($curl);
  122. return json_decode(substr($content, $headerSize), true);
  123. }
  124. /**
  125. * get sensitive fields name.
  126. *
  127. * @return array
  128. */
  129. protected function getSensitiveFieldsName()
  130. {
  131. return [
  132. 'contact_name',
  133. 'contact_id_number',
  134. 'mobile_phone',
  135. 'contact_email',
  136. 'id_card_name',
  137. 'id_card_number',
  138. 'id_doc_name',
  139. 'id_doc_number',
  140. 'name',
  141. 'id_number',
  142. 'account_name',
  143. 'account_number',
  144. 'id_card_name',
  145. 'id_card_number',
  146. 'account_name',
  147. 'contact_name',
  148. 'contact_id_card_number',
  149. 'mobile_phone',
  150. 'contact_email'
  151. ];
  152. }
  153. /**
  154. * To id card, mobile phone number and other fields sensitive information encryption.
  155. *
  156. * @param string $string
  157. *
  158. * @return string
  159. */
  160. protected function encryptSensitiveInformation(string $string)
  161. {
  162. $certificates = $this->app->certficates->get()['certificates'];
  163. if (null === $certificates) {
  164. throw new InvalidConfigException('config certificate connot be empty.');
  165. }
  166. $encrypted = '';
  167. if (openssl_public_encrypt($string, $encrypted, $certificates, OPENSSL_PKCS1_OAEP_PADDING)) {
  168. //base64编码
  169. $sign = base64_encode($encrypted);
  170. } else {
  171. throw new EncryptionException('Encryption of sensitive information failed');
  172. }
  173. return $sign;
  174. }
  175. /**
  176. * processing parameters contain fields that require sensitive information encryption.
  177. *
  178. * @param array $params
  179. *
  180. * @return array
  181. */
  182. protected function processParams(array $params)
  183. {
  184. $sensitive_fields = $this->getSensitiveFieldsName();
  185. foreach ($params as $k => $v) {
  186. if (is_array($v)) {
  187. $params[$k] = $this->processParams($v);
  188. } else {
  189. if (in_array($k, $sensitive_fields, true)) {
  190. $params[$k] = $this->encryptSensitiveInformation($v);
  191. }
  192. }
  193. }
  194. return $params;
  195. }
  196. /**
  197. * @param string $url
  198. * @param string $method
  199. * @param string $body
  200. * @return string
  201. */
  202. protected function getAuthorization(string $url, string $method, string $body)
  203. {
  204. $nonce_str = uniqid();
  205. $timestamp = time();
  206. $message = $method . "\n" .
  207. $url . "\n" .
  208. $timestamp . "\n" .
  209. $nonce_str . "\n" .
  210. $body . "\n";
  211. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  212. $sign = base64_encode($raw_sign);
  213. $schema = 'WECHATPAY2-SHA256-RSA2048 ';
  214. $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
  215. $this->app['config']['service_payment']['merchant_id'], $nonce_str, $timestamp, $this->app['config']['service_payment']['serial_no'], $sign);
  216. return $schema . $token;
  217. }
  218. /**
  219. * 获取商户私钥
  220. * @return bool|resource
  221. */
  222. protected function getPrivateKey()
  223. {
  224. $key_path = $this->app['config']['service_payment']['key_path'];
  225. if (!file_exists($key_path)) {
  226. throw new \InvalidArgumentException(
  227. "SSL certificate not found: {$key_path}"
  228. );
  229. }
  230. return openssl_pkey_get_private(file_get_contents($key_path));
  231. }
  232. /**
  233. * decrypt ciphertext.
  234. *
  235. * @param array $encryptCertificate
  236. *
  237. * @return string
  238. */
  239. public function decrypt(array $encryptCertificate)
  240. {
  241. $ciphertext = base64_decode($encryptCertificate['ciphertext'], true);
  242. $associatedData = $encryptCertificate['associated_data'];
  243. $nonceStr = $encryptCertificate['nonce'];
  244. $aesKey = $this->app['config']['service_payment']['apiv3_key'];
  245. try {
  246. // ext-sodium (default installed on >= PHP 7.2)
  247. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) {
  248. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  249. }
  250. // ext-libsodium (need install libsodium-php 1.x via pecl)
  251. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) {
  252. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  253. }
  254. // openssl (PHP >= 7.1 support AEAD)
  255. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  256. $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
  257. $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);
  258. return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
  259. }
  260. } catch (\Exception $exception) {
  261. throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
  262. } catch (\SodiumException $exception) {
  263. throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
  264. }
  265. throw new InvalidArgumentException('AEAD_AES_256_GCM 需要 PHP 7.1 以上或者安装 libsodium-php');
  266. }
  267. }