API申请地址:https:
public function getzhengshu(){
$url="https://api.mch.weixin.qq.com/v3/certificates";
$timestamp = time();
$nonce = $this->nonce_str();
$body = "";
$mch_private_key = $this->getPrivateKey($this->keyPath);
$merchant_id = $this->mc_id;
$serial_no = $this->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 = $this->APIKeyV3;
$result = $this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key);
if(!is_file(EASYSWOOLE_ROOT.'/cert/partner/temp_cert.pem')) {
file_put_contents(EASYSWOOLE_ROOT.'/cert/partner/temp_cert.pem',$result);
}
return $serial_no;
}
public function add($param){
$url = 'https://api.mch.weixin.qq.com/v3/ecommerce/applyments/';
$merchant_id = $this->mc_id;
$serial_no = $this->serial_no;
$mch_private_key = $this->getPrivateKey($this->keyPath);
$timestamp = time();
$nonce = $this->nonce_str();
$sign = $this->sign($url,'POST',$timestamp,$nonce,json_encode($param),$mch_private_key,$merchant_id,$serial_no);
$header = [
'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
'Accept:application/json',
'User-Agent:' . $merchant_id,
'Content-Type:application/json',
'Wechatpay-Serial:' . $this->getzhengshu()
];
$result = $this->curl($url,json_encode($param),$header);
$result = json_decode($result, true);
return $result;
}
public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){
$ciphertext= base64_decode($ciphertext);
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');
}
public function getEncryptS($str) {
$public_key_path = EASYSWOOLE_ROOT.'/cert/partner/temp_cert.pem';
$public_key = file_get_contents($public_key_path);
$encrypted = '';
if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)){
$sign = base64_encode($encrypted);
}else{
throw new \Exception('encrypt failed');
}
return $sign;
}
Array
(
[code] => PARAM_ERROR
[message] => 证书类型格式错误,请重新选择
)
检查一下你加载的API证书