Test.php 7.4 KB

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