//添加分帐用户 public function add(){ $type = "PERSONAL_OPENID"; $account = "oGUbk5Xkm91XL_gb-urq8JPrjdr8"; $name ="郑先生"; $relation_type = "DISTRIBUTOR"; list($Request,$body,$header) = SpliTheBill::add($type,$account,$name,$relation_type); $res = SpliTheBill::send($Request,$body,$header); print_r($res); } //SpliTheBill::__initializtion //配置证书 public function __initializtion(){ $cert = []; $cert['merchantId'] = C('PAY_YIGUO')['wxpay_mch_id']; // 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名 $cert['merchantPrivateKeyFilePath'] = 'file://'.APP.'Application/Library/Payment/Wxpay/fenzhang_cert/apiclient_key.pem'; $cert['merchantPrivateKeyInstance'] = Rsa::from($cert['merchantPrivateKeyFilePath'], Rsa::KEY_TYPE_PRIVATE); // 「商户API证书」的「证书序列号」 $cert['merchantCertificateSerial'] = '1CB071xxxx73B6B546C7D80AB3751D652974D0DD'; // 从本地文件中加载「微信支付平台证书」,可由内置CLI工具下载到,用来验证微信支付应答的签名 $cert['platformCertificateFilePath'] = 'file://'.APP.'Application/Library/Payment/Wxpay/fenzhang_cert/certificate.pem'; $cert['onePlatformPublicKeyInstance'] = Rsa::from($cert['platformCertificateFilePath'], Rsa::KEY_TYPE_PUBLIC); // 「微信支付平台证书」的「平台证书序列号」 // 可以从「微信支付平台证书」文件解析,也可以在 商户平台 -> 账户中心 -> API安全 查询到 $cert['platformCertificateSerial'] = '34E81xxxxED93477FA02ED40D51D2A7F8AC481E4'; // 从本地文件中加载「微信支付公钥」,用来验证微信支付应答的签名 $cert['platformPublicKeyFilePath'] = 'file://'.APP.'Application/Library/Payment/Wxpay/fenzhang_cert/pub_key.pem'; $cert['twoPlatformPublicKeyInstance'] = Rsa::from($cert['platformPublicKeyFilePath'], Rsa::KEY_TYPE_PUBLIC); // 「微信支付公钥」的「微信支付公钥ID」 // 需要在 商户平台 -> 账户中心 -> API安全 查询 $cert['platformPublicKeyId'] = 'PUB_KEY_ID_011xxxx841612025062200111535000000'; //wechatpay证书配置 $cert['wechatpayFilePath'] = 'file://'.APP.'Application/Library/Payment/Wxpay/fenzhang_cert/wechatpay.pem'; $cert['wechatpayKeyInstance'] = Rsa::from($cert['wechatpayFilePath'], Rsa::KEY_TYPE_PUBLIC); $cert['wechatpayKeyId'] = '34E8192B9ED9xxxxFA02ED40D51D2A7F8AC481E4'; return $cert; } /** * 添加分账接收方 SpliTheBill::add */ public function add($type,$account,$name="",$relation_type){ $method = 'POST'; $Request = "/v3/profitsharing/receivers/add"; $random = self::random(); //随机字符串 $time = self::unixtime(); //unix时间 $isheader = 0; $body = [ "appid" => C('PAY_YIGUO')['wxpay_app_id'], "type"=>$type,//MERCHANT_ID:商户ID PERSONAL_OPENID:个人openid, "account" => $account, "relation_type"=>$relation_type ]; if(!empty($name)): $isheader = 1; $body['name'] = self::getEncrypt($name); endif; $sign_res = json_decode(self::sign([$method,$Request,$random,$time,$body],$isheader),true);//签名 $header = self::setheader(['random'=>$random,"time"=>$time,"Signature"=>$sign_res['Signature'],"isheader"=>$isheader]);//设置header return [$Request,$body,$header]; } //SpliTheBill::getEncrypt //敏感信息加密 private function getEncrypt($str) { $cert = self::__initializtion(); $public_key_path = $cert['platformPublicKeyFilePath']; $public_key = file_get_contents($public_key_path); $encrypted = ''; if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) { //base64编码 $sign = base64_encode($encrypted); } else { throw new Exception('encrypt failed'); } return $sign; } //SpliTheBill::sign /** * 签名 */ public function sign($params,$isheader){ $cert = self::__initializtion(); $params += ['Signature' => Rsa::sign( Formatter::joinedByLineFeed(...array_values($params)), $cert['merchantPrivateKeyInstance']//$merchantPrivateKeyInstance ), 'signType' => 'RSA']; return json_encode($params); } //SpliTheBill::random /** * 随机字符串,签名需要 */ public function random($length=32){ $characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $index = rand(0, strlen($characters) - 1); $randomString .= $characters[$index]; } return $randomString; } //SpliTheBill::unixtime //当前时间 public function unixtime(){ return time(); } /** * 向腾讯云发送数据 */ public function send($Request,$body,$header){ $sert = SpliTheBill::__initializtion(); $instance = Builder::factory([ 'mchid' => $sert['merchantId'], 'serial' => $sert['merchantCertificateSerial'], 'privateKey' => $sert['merchantPrivateKeyInstance'], 'certs' => [ $sert['platformCertificateSerial'] => $sert['onePlatformPublicKeyInstance'], $sert['platformPublicKeyI'] => $sert['twoPlatformPublicKeyInstance'], ], ]); try { $resp = $instance ->chain($Request) ->post([ 'json' => $body, 'headers'=>$header, ]); //成功返回200 return ['code'=>$resp->getStatusCode(),"message"=>"成功","data"=>$resp->getBody()]; } catch (\Exception $e) { // 进行错误处理 $message = $e->getMessage(); if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); return ['code'=>$r->getStatusCode(),"message"=>$message,"data"=>$r->getReasonPhrase()]; } } } /** * 设置header */ public function setheader($param){ $cert = self::__initializtion(); switch ($param['isheader']){ case('1'): //添加分帐用户 $header = [ "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"{$cert['merchantId']}\",nonce_str=\"{$param['random']}\",signature=\"{$param['Signature']}\",timestamp=\"{$param['time']}\",serial_no=\"{$cert['merchantCertificateSerial']}\"", "Content-Type: application/json", "Accept: application/json", "Wechatpay-Serial:".$cert['platformPublicKeyId']."", //"User-Agent"=>"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1", ]; break; default: $header = [ "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"{$cert['merchantId']}\",nonce_str=\"{$param['random']}\",signature=\"{$param['Signature']}\",timestamp=\"{$param['time']}\",serial_no=\"{$cert['merchantPrivateKeyInstance']}\"", "Wechatpay-Serial:".$cert['merchantPrivateKeyInstance']."", "Content-Type: application/json", "Accept: application/json", //"User-Agent"=>"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1", ]; break; } return $header; }
使用 CertificateDownloader 获取 的 wechatpay.pem 不对?使用 以下命令 /data/www/server/php/72/bin/php /data/www/mall/vendor/wechatpay/wechatpay/bin/CertificateDownloader.php -k 9c7f4682xxxxx4e5 -m 1608184161 -f /data/wwww/mall/Application/Library/Payment/Wxpay/fenzhang_cert/apiclient_key.pem -s 1CB0716D6A7xxxxxxxxx0DD -o /data/wwww/mall/Application/Library/Payment/Wxpay/fenzhang_cert/ 生成了 wechatpay.pem 已经下载到本地,但是通过 openssl x509 -in wechatpay.pem -noout -serial 查看证书序列号时,和你们文档上的结果不一致。 文档地址:https://pay.weixin.qq.com/doc/v3/partner/4012365874 生成结果:Wechatpay-Serial: 4DF076AC5A7D968D4A8B0B9C599A74CB4CF8EE8A 而我用同样的命令,生成的是 [图片] 缺少 Wechatpay- 造成我添加分账接收方 (v3/profitsharing/receivers/add)出现 “HTTP header缺少微信支付平台证书序列号(Wechatpay-Serial)” 的错误 ,完全走不下去。
07-14几个证书都试了几次,都不行。
添加分账接收方 出现“HTTP header缺少微信支付平台证书序列号” 怎么回事 ?header Array( [0] => Authorization: WECHATPAY2-SHA256-RSA2048 mchid="160xxx4161",nonce_str="ha21pi4d3l0xxxxxxxxpzqvrn7a40n",signature="VqN4MZVOkTpc1MgcMClpUg+mKtt2W5HwdFUb+xZNEQXiQ26d9++S1FuFEiL86UEg/cQQJfyCOORKhJxWBHMakKfyhD95qHLYW1Q6NP8mtec6MFxc6wPYFGY9Jlmt/YpSgsS2Hx2IqaFT/mTDoa/zUt0O1EzEJOWH314L7hf31aCxlVJHmJjEPzA2XRgpmQzei4SYcfeKoAuyuUEaDBYEKXjt47ehffW/51gNNvgrGNSCyZuh6gDCsvhKGE+BGGe+fyne3Hbh5oU82O9nWz1/dL5g8YYlvb4LQM4yI5uWyAvupfg3mYF3OllQxw4u0rqAALVSya9R0JwOiGCa4KdNSQ==",timestamp="1751877228",serial_no="34E81xxxxxxxxx40D51D2A7F8AC481E4" [1] => Wechatpay-Serial:34E8192B9ED93477FA02ED40D51D2A7F8AC481E4 [2] => Content-Type: application/json [3] => Accept: application/json ) 请求参数 Array( [appid] => wx117xxxx90e23f60c [type] => MERCHANT_ID [account] => 166xxx4678 [relation_type] => SUPPLIER [name] => 上海xx电子商务有限公司 ) 已经在请求的header中有 Wechatpay-Serial 为什么还会提示 {"code":"PARAM_ERROR","message":"HTTP header缺少微信支付平台证书序列号(Wechatpay-Serial)"
07-09没有 人回答 ?
HTTP header中 倒底是哪个证书 序列号?相问问题页面:https://pay.weixin.qq.com/doc/v3/merchant/4012365346 倒底是使用哪个证书,把所有证书获取的序列号都试了,总是报错。 相关接口:v3/profitsharing/receivers/add [图片]
06-24