BaseClient.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\wechat\v3pay;
  14. use crmeb\exceptions\PayException;
  15. use crmeb\services\wechat\WechatException;
  16. use think\exception\InvalidArgumentException;
  17. use EasyWeChat\Kernel\BaseClient as EasyWeChatBaseClient;
  18. /**
  19. * Class BaseClient
  20. * @author 等风来
  21. * @email 136327134@qq.com
  22. * @date 2022/9/30
  23. * @package crmeb\services\wechat\v3pay
  24. */
  25. class BaseClient extends EasyWeChatBaseClient
  26. {
  27. use Certficates;
  28. const BASE_URL = 'https://api.mch.weixin.qq.com/';
  29. const KEY_LENGTH_BYTE = 32;
  30. const AUTH_TAG_LENGTH_BYTE = 16;
  31. /**
  32. * request.
  33. *
  34. * @param string $endpoint
  35. * @param string $method
  36. * @param array $options
  37. * @param bool $returnResponse
  38. */
  39. public function request(string $endpoint, string $method = 'POST', array $options = [], $serial = true)
  40. {
  41. $body = $options['body'] ?? '';
  42. if (isset($options['json'])) {
  43. $body = json_encode($options['json']);
  44. $options['body'] = $body;
  45. unset($options['json']);
  46. }
  47. $headers = [
  48. 'Content-Type' => 'application/json',
  49. 'User-Agent' => 'curl',
  50. 'Accept' => 'application/json',
  51. 'Authorization' => $this->getAuthorization($endpoint, $method, $body),
  52. ];
  53. $options['headers'] = array_merge($headers, ($options['headers'] ?? []));
  54. if ($serial) {
  55. $options['headers']['Wechatpay-Serial'] = $this->getCertficatescAttr('serial_no');
  56. }
  57. return $this->_doRequestCurl($method, self::BASE_URL . $endpoint, $options);
  58. }
  59. /**
  60. * @param $method
  61. * @param $location
  62. * @param array $options
  63. * @return mixed
  64. */
  65. private function _doRequestCurl($method, $location, $options = [])
  66. {
  67. $curl = curl_init();
  68. // POST数据设置
  69. if (strtolower($method) === 'post') {
  70. curl_setopt($curl, CURLOPT_POST, true);
  71. curl_setopt($curl, CURLOPT_POSTFIELDS, $options['data'] ?? $options['body'] ?? '');
  72. }
  73. // CURL头信息设置
  74. if (!empty($options['headers'])) {
  75. $headers = [];
  76. foreach ($options['headers'] as $k => $v) {
  77. $headers[] = "$k: $v";
  78. }
  79. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  80. }
  81. curl_setopt($curl, CURLOPT_URL, $location);
  82. curl_setopt($curl, CURLOPT_HEADER, true);
  83. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  84. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  85. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  86. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  87. $content = curl_exec($curl);
  88. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  89. curl_close($curl);
  90. return json_decode(substr($content, $headerSize), true);
  91. }
  92. /**
  93. * To id card, mobile phone number and other fields sensitive information encryption.
  94. *
  95. * @param string $string
  96. *
  97. * @return string
  98. */
  99. protected function encryptSensitiveInformation(string $string)
  100. {
  101. $certificates = $this->app->certficates->get()['certificates'];
  102. if (null === $certificates) {
  103. throw new WechatException('config certificate connot be empty.');
  104. }
  105. $encrypted = '';
  106. if (openssl_public_encrypt($string, $encrypted, $certificates, OPENSSL_PKCS1_OAEP_PADDING)) {
  107. //base64编码
  108. $sign = base64_encode($encrypted);
  109. } else {
  110. throw new WechatException('Encryption of sensitive information failed');
  111. }
  112. return $sign;
  113. }
  114. /**
  115. * @param string $url
  116. * @param string $method
  117. * @param string $body
  118. * @return string
  119. */
  120. protected function getAuthorization(string $url, string $method, string $body)
  121. {
  122. $nonceStr = uniqid();
  123. $timestamp = time();
  124. $message = $method . "\n" .
  125. '/' . $url . "\n" .
  126. $timestamp . "\n" .
  127. $nonceStr . "\n" .
  128. $body . "\n";
  129. openssl_sign($message, $raw_sign, $this->getPrivateKey(), 'sha256WithRSAEncryption');
  130. $sign = base64_encode($raw_sign);
  131. $schema = 'WECHATPAY2-SHA256-RSA2048 ';
  132. $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
  133. $this->app['config']['v3_payment']['mch_id'], $nonceStr, $timestamp, $this->app['config']['v3_payment']['serial_no'], $sign);
  134. return $schema . $token;
  135. }
  136. /**
  137. * 获取商户私钥
  138. * @return bool|resource
  139. */
  140. protected function getPrivateKey()
  141. {
  142. $key_path = $this->app['config']['key_path'];
  143. if (!file_exists($key_path)) {
  144. throw new \InvalidArgumentException(
  145. "SSL certificate not found: {$key_path}"
  146. );
  147. }
  148. return openssl_pkey_get_private(file_get_contents($key_path));
  149. }
  150. /**
  151. * 获取商户公钥
  152. * @return bool|resource
  153. */
  154. protected function getPublicKey()
  155. {
  156. $key_path = $this->app['config']['cert_path'];
  157. if (!file_exists($key_path)) {
  158. throw new \InvalidArgumentException(
  159. "SSL certificate not found: {$key_path}"
  160. );
  161. }
  162. return openssl_pkey_get_public(file_get_contents($key_path));
  163. }
  164. /**
  165. * 替换url
  166. * @param string $url
  167. * @param $search
  168. * @param $replace
  169. * @return array|string|string[]
  170. */
  171. public function getApiUrl(string $url, $search, $replace)
  172. {
  173. $newSearch = [];
  174. foreach ($search as $key) {
  175. $newSearch[] = '{' . $key . '}';
  176. }
  177. return str_replace($newSearch, $replace, $url);
  178. }
  179. /**
  180. * @param int $padding
  181. */
  182. private static function paddingModeLimitedCheck(int $padding): void
  183. {
  184. if (!($padding === OPENSSL_PKCS1_OAEP_PADDING || $padding === OPENSSL_PKCS1_PADDING)) {
  185. throw new PayException(sprintf("Doesn't supported padding mode(%d), here only support OPENSSL_PKCS1_OAEP_PADDING or OPENSSL_PKCS1_PADDING.", $padding));
  186. }
  187. }
  188. /**
  189. * 加密数据
  190. * @param string $plaintext
  191. * @param int $padding
  192. * @return string
  193. */
  194. public function encryptor(string $plaintext, int $padding = OPENSSL_PKCS1_OAEP_PADDING)
  195. {
  196. self::paddingModeLimitedCheck($padding);
  197. if (!openssl_public_encrypt($plaintext, $encrypted, $this->getPublicKey(), $padding)) {
  198. throw new PayException('Encrypting the input $plaintext failed, please checking your $publicKey whether or nor correct.');
  199. }
  200. return base64_encode($encrypted);
  201. }
  202. /**
  203. * decrypt ciphertext.
  204. *
  205. * @param array $encryptCertificate
  206. *
  207. * @return string
  208. */
  209. public function decrypt(array $encryptCertificate)
  210. {
  211. $ciphertext = base64_decode($encryptCertificate['ciphertext'], true);
  212. $associatedData = $encryptCertificate['associated_data'];
  213. $nonceStr = $encryptCertificate['nonce'];
  214. $aesKey = $this->app['config']['v3_payment']['key'];
  215. try {
  216. // ext-sodium (default installed on >= PHP 7.2)
  217. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) {
  218. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  219. }
  220. // ext-libsodium (need install libsodium-php 1.x via pecl)
  221. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) {
  222. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  223. }
  224. // openssl (PHP >= 7.1 support AEAD)
  225. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  226. $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
  227. $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);
  228. return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
  229. }
  230. } catch (\Exception $exception) {
  231. throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
  232. } catch (\SodiumException $exception) {
  233. throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
  234. }
  235. throw new InvalidArgumentException('AEAD_AES_256_GCM 需要 PHP 7.1 以上或者安装 libsodium-php');
  236. }
  237. }