本文為大家分享了PHP實現(xiàn)微信商戶支付企業(yè)付款到零錢的具體代碼,供大家參考,具體內(nèi)容如下
一、開通條件
企業(yè)付款為企業(yè)提供付款至用戶零錢的能力,支持通過API接口付款,或通過微信支付商戶平臺(pay.weixin.qq.com)網(wǎng)頁操作付款。
使用條件
- 商戶號(或同主體其他非服務(wù)商商戶號)已入駐90日
- 商戶號(或同主體其他非服務(wù)商商戶號)有30天連續(xù)正常交易
- 登錄微信支付商戶平臺-產(chǎn)品中心,開通企業(yè)付款。
具體的可以看微信支付開發(fā)文檔
二、代碼展示
//企業(yè)付款到微信零錢,PHP接口調(diào)用方法 define("APPID", "wxe062425f740c30d8"); // 商戶賬號appid define("MCHID", "10000098"); // 商戶號 define("SECRECT_KEY", "453436425252"); //支付密鑰簽名 define("IP", "xxx.xxx.xx.xx"); //IP /** * [xmltoarray xml格式轉(zhuǎn)換為數(shù)組] * @param [type] $xml [xml] * @return [type] [xml 轉(zhuǎn)化為array] */ function xmltoarray($xml) { //禁止引用外部xml實體 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; } /** * [arraytoxml 將數(shù)組轉(zhuǎn)換成xml格式(簡單方法):] * @param [type] $data [數(shù)組] * @return [type] [array 轉(zhuǎn) xml] */ function arraytoxml($data){ $str='<xml>'; foreach($data as $k=>$v) { $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str.='</xml>'; return $str; } /** * [createNoncestr 生成隨機字符串] * @param integer $length [長度] * @return [type] [字母大小寫加數(shù)字] */ function createNoncestr($length =32){ $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for($i=0;$i<$length;$i++){ $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } /** * [curl_post_ssl 發(fā)送curl_post數(shù)據(jù)] * @param [type] $url [發(fā)送地址] * @param [type] $xmldata [發(fā)送文件格式] * @param [type] $second [設(shè)置執(zhí)行最長秒數(shù)] * @param [type] $aHeader [設(shè)置頭部] * @return [type] [description] */ function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){ $isdir = $_SERVER['DOCUMENT_ROOT']."/cert/";//證書位置;絕對路徑 $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設(shè)置執(zhí)行最長秒數(shù) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網(wǎng)頁 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務(wù)端進行驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//證書類型 curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//證書位置 curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中規(guī)定的私鑰的加密類型 curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//證書位置 curl_setopt($ch, CURLOPT_CAINFO, 'PEM'); curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem'); if (count($aHeader) >= 1) { curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//設(shè)置頭部 } curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部數(shù)據(jù)使用HTTP協(xié)議中的"POST"操作來發(fā)送 $data = curl_exec($ch);//執(zhí)行回話 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } /** * [sendMoney 企業(yè)付款到零錢] * @param [type] $amount [發(fā)送的金額(分)目前發(fā)送金額不能少于1元] * @param [type] $re_openid [發(fā)送人的 openid] * @param string $desc [企業(yè)付款描述信息 (必填)] * @param string $check_name [收款用戶姓名 (選填)] * @return [type] [description] */ function sendMoney($amount,$re_openid,$desc='測試',$check_name=''){ $total_amount = (100) * $amount; $data=array( 'mch_appid'=>APPID,//商戶賬號appid 'mchid'=> MCHID,//商戶號 'nonce_str'=>createNoncestr(),//隨機字符串 'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商戶訂單號 'openid'=> $re_openid,//用戶openid 'check_name'=>'NO_CHECK',//校驗用戶姓名選項, 're_user_name'=> $check_name,//收款用戶姓名 'amount'=>$total_amount,//金額 'desc'=> $desc,//企業(yè)付款描述信息 'spbill_create_ip'=> IP,//Ip地址 ); //生成簽名算法 $secrect_key=SECRECT_KEY;///這個就是個API密碼。MD5 32位。 $data=array_filter($data); ksort($data); $str=''; foreach($data as $k=>$v) { $str.=$k.'='.$v.'&'; } $str.='key='.$secrect_key; $data['sign']=md5($str); //生成簽名算法 $xml=arraytoxml($data); $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //調(diào)用接口 $res=curl_post_ssl($url,$xml); $return=xmltoarray($res); print_r($return); //返回來的結(jié)果是xml,最后轉(zhuǎn)換成數(shù)組 /* array(9) { ["return_code"]=> string(7) "SUCCESS" ["return_msg"]=> array(0) { } ["mch_appid"]=> string(18) "wx57676786465544b2a5" ["mchid"]=> string(10) "143345612" ["nonce_str"]=> string(32) "iw6TtHdOySMAfS81qcnqXojwUMn8l8mY" ["result_code"]=> string(7) "SUCCESS" ["partner_trade_no"]=> string(18) "201807011410504098" ["payment_no"]=> string(28) "1000018301201807019357038738" ["payment_time"]=> string(19) "2018-07-01 14:56:35" } */ $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA); echo $res= $responseObj->return_code; //SUCCESS 如果返回來SUCCESS,則發(fā)生成功,處理自己的邏輯 return $res; }
三、調(diào)用方法
sendMoney(1,'gdgfdg56456223423','xxxx測試','xxx');
調(diào)用的話,稍微在修改下,加上自己的業(yè)務(wù)邏輯就行了。
四、調(diào)用效果
也可以下載文件,我寫的是一個類文件:PHP微信商戶支付企業(yè)付款到零錢功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。