Test.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\TencentCloudService;
  4. use app\api\service\WxService;
  5. use app\common\controller\Api;
  6. class Test extends Api
  7. {
  8. protected $noNeedLogin = ["*"];
  9. function phpinfo()
  10. {
  11. echo phpinfo();
  12. }
  13. function payment()
  14. {
  15. $SR = (new WxService())->appPay("oQ_977SN3KhBetHY9OPckGP9DNNc", "G" . time(), "商品购买", 0.01);
  16. $SR->code() ? $this->success($SR->data()) : $this->error($SR->msg());
  17. }
  18. function sms()
  19. {
  20. $valid = TencentCloudService::tencent_cloud_sms_send("2280835", "15574920253", [random_int(1000, 9999)]);
  21. $valid->code() ? $this->success($valid->data()) : $this->error($valid->msg());
  22. }
  23. function test1()
  24. {
  25. dump(decbin(5));
  26. // dump(5^5);
  27. }
  28. /**
  29. * 获取证书
  30. * @return mixed
  31. */
  32. public static function certificates()
  33. {
  34. //请求参数(报文主体)
  35. $headers = self::sign('GET', 'https://api.mch.weixin.qq.com/v3/certificates', '');
  36. $result = self::curl_get('https://api.mch.weixin.qq.com/v3/certificates', $headers);
  37. $result = json_decode($result, true);
  38. dump($result);// 解密后的内容,就是证书内容
  39. $aa = self::decryptToString($result['data'][0]['encrypt_certificate']['associated_data'], $result['data'][0]['encrypt_certificate']['nonce'], $result['data'][0]['encrypt_certificate']['ciphertext']);
  40. var_dump($aa);// 解密后的内容,就是证书内容
  41. }
  42. /**
  43. * 签名
  44. * @param string $http_method 请求方式GET|POST
  45. * @param string $url url
  46. * @param string $body 报文主体
  47. * @return array
  48. */
  49. public static function sign($http_method = 'POST', $url = '', $body = '')
  50. {
  51. $mch_private_key = self::getMchKey();//私钥
  52. $timestamp = time();//时间戳
  53. $nonce = self::getRandomStr(32);//随机串
  54. $url_parts = parse_url($url);
  55. $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
  56. //构造签名串
  57. $message = $http_method . "\n" .
  58. $canonical_url . "\n" .
  59. $timestamp . "\n" .
  60. $nonce . "\n" .
  61. $body . "\n";//报文主体
  62. //计算签名值
  63. openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
  64. $sign = base64_encode($raw_sign);
  65. //设置HTTP头
  66. $config = self::config();
  67. $token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
  68. $config['mchid'], $nonce, $timestamp, $config['serial_no'], $sign);
  69. return [
  70. 'Accept: application/json',
  71. 'User-Agent: */*',
  72. 'Content-Type: application/json; charset=utf-8',
  73. 'Authorization: ' . $token,
  74. ];
  75. }
  76. //私钥
  77. public static function getMchKey()
  78. {
  79. //path->私钥文件存放路径
  80. return openssl_get_privatekey(file_get_contents("file://D:/project_c/billiards-server/certificate/wx/apiclient_key.pem"));
  81. }
  82. /**
  83. * 获得随机字符串
  84. * @param $len integer 需要的长度
  85. * @param $special bool 是否需要特殊符号
  86. * @return string 返回随机字符串
  87. */
  88. public static function getRandomStr($len, bool $special = false)
  89. {
  90. $chars = array(
  91. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  92. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  93. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  94. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  95. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  96. "3", "4", "5", "6", "7", "8", "9"
  97. );
  98. if ($special) {
  99. $chars = array_merge($chars, array(
  100. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  101. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  102. "}", "<", ">", "~", "+", "=", ",", "."
  103. ));
  104. }
  105. $charsLen = count($chars) - 1;
  106. shuffle($chars); //打乱数组顺序
  107. $str = '';
  108. for ($i = 0; $i < $len; $i++) {
  109. $str .= $chars[mt_rand(0, $charsLen)]; //随机取出一位
  110. }
  111. return $str;
  112. }
  113. /**
  114. * 配置
  115. */
  116. public static function config()
  117. {
  118. return [
  119. 'appid' => '',
  120. 'mchid' => '1638097896',//商户号
  121. 'serial_no' => '13A336433257E2C58CB5A6BC469B7040EF7E7456',//证书序列号
  122. 'description' => '',//应用名称(随意)
  123. 'notify' => '',//支付回调
  124. ];
  125. }
  126. //get请求
  127. public static function curl_get($url, $headers = array())
  128. {
  129. $info = curl_init();
  130. curl_setopt($info, CURLOPT_RETURNTRANSFER, true);
  131. curl_setopt($info, CURLOPT_HEADER, 0);
  132. curl_setopt($info, CURLOPT_NOBODY, 0);
  133. curl_setopt($info, CURLOPT_SSL_VERIFYPEER, false);
  134. curl_setopt($info, CURLOPT_SSL_VERIFYPEER, false);
  135. curl_setopt($info, CURLOPT_SSL_VERIFYHOST, false);
  136. //设置header头
  137. curl_setopt($info, CURLOPT_HTTPHEADER, $headers);
  138. curl_setopt($info, CURLOPT_URL, $url);
  139. $output = curl_exec($info);
  140. curl_close($info);
  141. return $output;
  142. }
  143. const KEY_LENGTH_BYTE = 32;
  144. const AUTH_TAG_LENGTH_BYTE = 16;
  145. /**
  146. * Decrypt AEAD_AES_256_GCM ciphertext
  147. *
  148. * @param string $associatedData AES GCM additional authentication data
  149. * @param string $nonceStr AES GCM nonce
  150. * @param string $ciphertext AES GCM cipher text
  151. *
  152. * @return string|bool Decrypted string on success or FALSE on failure
  153. */
  154. public static function decryptToString($associatedData, $nonceStr, $ciphertext)
  155. {
  156. $aesKey = '5a523a148c428bf8c4af91000fc307a6';
  157. $ciphertext = \base64_decode($ciphertext);
  158. if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {
  159. return false;
  160. }
  161. // ext-sodium (default installed on >= PHP 7.2)
  162. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) {
  163. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  164. }
  165. // ext-libsodium (need install libsodium-php 1.x via pecl)
  166. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) {
  167. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  168. }
  169. // openssl (PHP >= 7.1 support AEAD)
  170. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  171. $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
  172. $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);
  173. return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr,
  174. $authTag, $associatedData);
  175. }
  176. throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
  177. }
  178. }
  179. /**
  180. * 各省定价不相同 获取服务时候 根据位置获取服务
  181. * 抽成比例更换
  182. */