SignHandler.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\Store;
  4. use App\Models\DwbsUser;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Log;
  8. Class SignHandler
  9. {
  10. //读取商户api证书公钥
  11. public function getPublicKey(){
  12. return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
  13. }
  14. //生成随机字符串
  15. public function nonce_str($length=32){
  16. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  17. $str ="";
  18. for ( $i = 0; $i < $length; $i++ ) {
  19. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  20. }
  21. return $str;
  22. }
  23. //签名
  24. public function sign($url,$http_method,$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no){
  25. $url_parts=parse_url($url);
  26. $canonical_url=($url_parts['path'].(!empty($url_parts['query'])?"?${url_parts['query']}":""));
  27. $message=
  28. $http_method . "\n".
  29. $canonical_url . "\n".
  30. $timestamp . "\n".
  31. $nonce . "\n".
  32. $body . "\n";
  33. openssl_sign($message,$raw_sign,$mch_private_key,'sha256WithRSAEncryption');
  34. $sign=base64_encode($raw_sign);
  35. $schema='WECHATPAY2-SHA256-RSA2048';
  36. $token=sprintf(
  37. 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
  38. $merchant_id,
  39. $nonce,
  40. $sign,
  41. $timestamp,
  42. $serial_no
  43. );
  44. return $token;
  45. }
  46. //获取平台证书
  47. public function getzhengshu(){
  48. $url="https://api.mch.weixin.qq.com/v3/certificates";//获取地址
  49. $timestamp=time();//时间戳
  50. $nonce=$this->nonce_str();//获取一个随机字符串
  51. $body="";
  52. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥
  53. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  54. $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号
  55. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);
  56. $header=[
  57. 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
  58. 'Accept:application/json',
  59. 'User-Agent:'.$merchant_id
  60. ];
  61. $result=$this->curl($url,'',$header,'GET');
  62. $result=json_decode($result,true);
  63. $serial_no=$result['data'][0]['serial_no'];//获取的平台证书序列号
  64. $encrypt_certificate=$result['data'][0]['encrypt_certificate'];
  65. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  66. $result=$this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key);
  67. file_put_contents(config('wechat.payment.default.cert_pem'),$result);//获取的文件临时保存到服务器
  68. return $serial_no;//返回平台证书序列号
  69. }
  70. //解密返回的信息
  71. public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){
  72. if (strlen($aesKey) != 32) {
  73. throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
  74. }
  75. // $this->aesKey = $aesKey;
  76. $ciphertext=\base64_decode($ciphertext);
  77. if (strlen($ciphertext) <= 16) {
  78. return false;
  79. }
  80. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
  81. \sodium_crypto_aead_aes256gcm_is_available()) {
  82. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  83. return '1111';
  84. }
  85. // ext-libsodium (need install libsodium-php 1.x via pecl)
  86. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
  87. \Sodium\crypto_aead_aes256gcm_is_available()) {
  88. return '222';
  89. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  90. }
  91. // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){
  92. // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey);
  93. // }
  94. if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){
  95. $ctext=substr($ciphertext,0,-16);
  96. $authTag=substr($ciphertext,-16);
  97. return \openssl_decrypt(
  98. $ctext,
  99. 'aes-256-gcm',
  100. $aesKey,
  101. \OPENSSL_RAW_DATA,
  102. $nonceStr,
  103. $authTag,
  104. $associatedData
  105. );
  106. }
  107. throw new \RuntimeException('php7.1');
  108. }
  109. //curl提交
  110. public function curl($url,$data=[],$header,$method='POST'){
  111. $curl=curl_init();
  112. curl_setopt($curl,CURLOPT_URL,$url);
  113. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  114. curl_setopt($curl,CURLOPT_HEADER,false);
  115. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  116. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  117. if($method=="POST"){
  118. curl_setopt($curl,CURLOPT_POST,TRUE);
  119. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  120. }
  121. $result=curl_exec($curl);
  122. curl_close($curl);
  123. return $result;
  124. }
  125. }