参考:添加分账接收方API - 分账 | 微信支付服务商文档中心
所需信息:
【微信支付平台证书、商户API证书、商户API私钥】【V3密钥、公众账号ID、运营方商户号、运营方商户api证书序列号、运营方微信支付平台证书序列号、随机串、当前时间戳、由签名值计算的签名串、分账接收方商户号、加密后的分账接收方商户名】
1、查看证书序列号 地址
2、微信支付平台证书
该证书只能通过接口获得,首次下载用命令下载比较好一些 下载工具
/*首次下载*/
java -jar CertificateDownloader.jar -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}
/*更新*/
java -jar CertificateDownloader.jar -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath} -c ${wechatpayCertificateFilePath}
/*参数*/
必需参数有:
-f <privateKeyFilePath>,商户API私钥文件路径
-k <apiV3Key>,证书解密的密钥
-m <merchantId>,商户号
-o <outputFilePath>,保存证书的路径
-s <merchantSerialNo>,商户API证书的序列号
非必需参数有:
-c <wechatpayCertificatePath>,微信支付平台证书的路径。如果你还没有证书,请先不传该参数。
3、商户API证书、密钥 参考
4、加密后的分账接收方商户名
function getEncrypt($str) {
//$str是待加密字符串
$public_key_path = "平台证书地址";
$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;
}
5、由签名值计算的签名串
POST\n
/v3/profitsharing/receivers/add\n
时间戳\n
随机串\n
json串{"appid" : "公众账号ID","type" : "MERCHANT_ID","account" : "分账接收方商户号","name" : "加密后的分账接收方商户名","relation_type" : "PARTNER"}\n
6、添加请求
curl -X POST \
https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add \
-H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid="运营方商户号",nonce_str="随机串",signature="由签名值计算的签名串",timestamp="当前时间戳",serial_no="运营方商户api证书序列号"\
-H "Accept: application/json" \
-H "Wechatpay-Serial: 运营方微信支付平台证书序列号" \
-H "Content-Type: application/json" \
-d '{
"appid" : "公众账号ID",
"type" : "MERCHANT_ID",
"account" : "分账接收方商户号",
"name" : "加密后的分账接收方商户名",
"relation_type" : "PARTNER"
}'
完整代码
<?php
header("Access-Control-Allow-Origin: *");
$appid = '公众号appid';
$nonce=getNonceStr();//"随机串"
$serial_no = '商户API证书系列号';
$mchid="运营方商户号";
$timestamp = time();
$private_key_path = "商户API密钥地址";
$account = '分账接收方商户号';
$str = "分账接收方商户名";
$name = getEncrypt($str);//加密后的分账接收方商户名
$url = 'https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add';
$data = '{"appid":"'.$appid.'","type":"MERCHANT_ID","account":"'.$account.'","name":"'.$name.'","relation_type":"PARTNER"}';
/*由签名值计算的签名串*/
$private_key = file_get_contents($private_key_path);//商户API密钥内容
$url_parts = parse_url($url);
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$message = "POST".chr(10).$canonical_url.chr(10).$timestamp.chr(10).$nonce.chr(10).$data.chr(10);
openssl_sign($message, $raw_sign, $private_key, 'sha256WithRSAEncryption');
$sign = base64_encode($raw_sign);
/*添加分账方*/
$schema = 'WECHATPAY2-SHA256-RSA2048';
$token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $mchid, $nonce, $timestamp, $serial_no, $sign);
$ret = posturl($url,$data,$schema.' '.$token);
print_r($ret);
function posturl($url, $data, $Authorization){
$wechatpay_no = "平台证书序列号";
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json","Authorization:".$Authorization,"User-Agent:".$_SERVER['HTTP_USER_AGENT'],"Wechatpay-Serial:".$wechatpay_no);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function getEncrypt($str) {
//$str是待加密字符串
$public_key_path = "平台证书地址";
$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;
}
/**
*
* 产生随机字符串,不长于32位
* @param int $length
* @return 产生的随机字符串
*/
function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
?>