nonce_str();//获取一个随机字符串 $body=""; $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 $merchant_id=config('wechat.payment.default.mch_id');//商户号 $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号 $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no); $header=[ 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign, 'Accept:application/json', 'User-Agent:'.$merchant_id ]; $result=$this->curl($url,'',$header,'GET'); $result=json_decode($result,true); $serial_no=$result['data'][0]['serial_no'];//获取的平台证书序列号 $encrypt_certificate=$result['data'][0]['encrypt_certificate']; $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取 $result=$this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key); file_put_contents(config('wechat.payment.default.cert_pem'),$result);//获取的文件临时保存到服务器 return $serial_no;//返回平台证书序列号 } //解密返回的信息 public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){ if (strlen($aesKey) != 32) { throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节'); } // $this->aesKey = $aesKey; $ciphertext=\base64_decode($ciphertext); if (strlen($ciphertext) <= 16) { return false; } if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) { return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey); return '1111'; } // ext-libsodium (need install libsodium-php 1.x via pecl) if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) { return '222'; return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey); } // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){ // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey); // } if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){ $ctext=substr($ciphertext,0,-16); $authTag=substr($ciphertext,-16); return \openssl_decrypt( $ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData ); } throw new \RuntimeException('php7.1'); } //curl提交 public function curl($url,$data=[],$header,$method='POST'){ $curl=curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_HTTPHEADER,$header); curl_setopt($curl,CURLOPT_HEADER,false); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); if($method=="POST"){ curl_setopt($curl,CURLOPT_POST,TRUE); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); } $result=curl_exec($curl); curl_close($curl); return $result; } }