服务账号共享功能的实现1、开启服务商账号共享功能2、添加子商户3、开启账号共享,需要先添加可以共享账号的用户,然后才能共享。注意事项:1.支付时添加profit_sharing='Y'2.签名请求参数签名类型为HMAC-SHA2563,需要证书*服务商计费功能**/classwxDivideAccount{protected$CertPem='/home/wwwroot/xixxxxm/comxxxmon/mch/apiclient_cert.pem';protected$KeyPem='/home/wwwroot/xixxxxm/comxxxmon/mch/apiclient_key.pem';受保护的$key='lwLyC2AWYPwwewet4ddkX2vhO5q';//支付密钥key/*服务商账户共享*/publicfunctiondivideMoney(){$params=array();$params['mch_id']='1406xx6102';//服务提供者mch_id$params['sub_mch_id']='1553xx6371';//特殊商户mch_id$params['appid']='wx18dbxxx55773c05';//服务提供者对应的appid$params['sub_appid']='wx7814xxaddbeb8';//对应的子appid$params['nonce_str']=$this->createNoncestr();$params['sign_type']='HMAC-SHA256';$params['transaction_id']='4200000450201910195680876190';//微信支付订单号$params['out_order_no']='157146517126_580624809017106007';//交易订单号$receivers=array(['type'=>'PERSONAL_SUB_OPENID','account'=>'owf034yN-8MdAFNgaNm4qbxDw',//sub_openid'amount,'=>2description'=>'分发给发起人']);$params['receivers']=json_encode($receivers);$params['sign']=$this->createSign($params);//将数组转换成xml格式$xmlData=$this->arrayToXml($params);$resXml=$this->postXmlCurl('https://api.mch.weixin.qq.com/secapi/pay/profitsharing',$xmlData,true);$resXml=$this->xmlSafeStr(html_entity_decode($resXml));$resData=json_decode(json_encode(simplexml_load_string($resXml,'SimpleXMLElement',LIBXML_NOCDATA)),true);如果($resData['return_code']=='SUCCESS'&&$resData['result_code']=='SUCCESS')返回真;write_log('账号拆分失败,msg:'.$resData['return_msg'],'profitshare');返回假;}//添加可分割账户publicfunctionprofitSharingAddreceiver(){$url='https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver';$参数=数组();$params['mch_id']='1406xx6102';//服务提供者mch_id$params['sub_mch_id']='1553xx6371';//特殊商户mch_id$params['appid']='wx18dbxxx55773c05';//服务提供者的appid$params['sub_appid']='wx7814xxaddbeb8';//对应的子appid$params['nonce_str']=$this->createNoncestr();$params['sign_type']='HMAC-SHA256';$receiver=['type'=>'PERSONAL_SUB_OPENID','account'=>'owf0349GNSmFBkvJpxlG-92N62ZM','relation_type'=>'DISTRIBUTOR'];$params['receiver']=json_encode($receiver);$params['sign']=$this->createSign($params);//将数组转换成xml格式$xmlData=$this->arrayToXml($params);$resXml=$this->postXmlCurl($url,$xmlData,true);$resData=json_decode(json_encode(simplexml_load_string($resXml,'SimpleXMLElement',LIBXML_NOCDATA)),1);如果($resData['return_code']=='SUCCESS'&&$resData['result_code']=='SUCCESS')返回真;write_log('分账户用户添加失败,msg:'.$resData['return_msg'],'profitshare');返回假;}/**查询分账结果*/publicfunctionprofitSharingQuery(){$url='https://api.mch.weixin.qq.com/pay/profitsharingquery';$参数=数组();$params['mch_id']='1406xx6102';$params['sub_mch_id']='1553xx6371';$params['nonce_str']=$this->createNoncestr();$params['sign_type']='HMAC-SHA256';$params['transaction_id']='4200000450201910195680876190';$params['out_order_no']='157146517126_580624809017106007';$params['sign']=$this->createSign($params);//将数组转换成xml格式$xmlData=$this->arrayToXml($params);$resXml=$this->postXmlCurl($url,$xmlData,true);$resXml=$this->xmlSafeStr(html_entity_decode($resXml));$resData=json_decode(json_encode(simplexml_load_string($resXml,'SimpleXMLElement',LIBXML_NOCDATA)),1);如果($resData['return_code']=='SUCCESS'&&$resData['result_code']=='SUCCESS')返回真;write_log('股息查询失败,msg:'.$resData['return_msg'],'profitshare');返回假;}//生成签名privatefunctioncreateSign($params){//签名步骤1:按字典顺序对参数进行排序ksort($params);$String=$this->formatBizQueryParaMap($params,false);//签名步骤2:在字符串后添加KEY$String=$String."&key=".$this->key;//签名步骤3:加密方式HMAC-SHA256//$字符串=md5($字符串);$String=hash_hmac("sha256",$String,$this->key);//签名步骤4:将所有字符转换为大写$result_=strtoupper($String);返回$结果_;}//按字典顺序对参数进行排序privatefunctionformatBizQueryParaMap($params,$urlencode=false){ksort($params);$buff='';foreach($paramsas$key=>$val){$buff.=$key.“=”。$瓦尔。"&";}$reqPar=substr($buff,0,strlen($buff)-1);返回$reqPar;}//随机字符串privatefunctioncreateNoncestr($length=32){$chars='abcdefghijklmnopqrstuvwxyz0123456789';$str='';for($i=0;$i<$length;$i++){$str.=substr($chars,mt_rand(0,strlen($chars)-1),1);}返回$str;}//将数组转换成xml格式privatefunctionarrayToXml($arr){$xml="
