1. 程式人生 > >PHP微信商戶支付企業付款到零錢功能(完)

PHP微信商戶支付企業付款到零錢功能(完)

微信支付開發文件

一、開通條件

企業付款為企業提供付款至使用者零錢的能力,支援通過API介面付款,或通過微信支付商戶平臺(pay.weixin.qq.com)網頁操作付款。

使用條件

  • 商戶號(或同主體其他非服務商商戶號)已入駐90日
  • 商戶號(或同主體其他非服務商商戶號)有30天連續正常交易
  • 登入微信支付商戶平臺-產品中心,開通企業付款。

1

具體的可以看微信支付開發文件

二、程式碼展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

//企業付款到微信零錢,PHP介面呼叫方法
define("APPID", "wxe062425f740c30d8"); // 商戶賬號appid
define("MCHID", "10000098");      // 商戶號
define("SECRECT_KEY", "453436425252"
); //支付金鑰簽名
define("IP", "xxx.xxx.xx.xx"); //IP /** * [xmltoarray xml格式轉換為陣列] * @param [type] $xml [xml] * @return [type] [xml 轉化為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 將陣列轉換成xml格式(簡單方法):] * @param [type] $data [陣列] * @return [type] [array 轉 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] [字母大小寫加數字] */ 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 傳送curl_post資料] * @param [type] $url [傳送地址] * @param [type] $xmldata [傳送檔案格式] * @param [type] $second [設定執行最長秒數] * @param [type] $aHeader [設定頭部] * @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);//設定執行最長秒數 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字串且輸出到螢幕上 curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網頁 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務端進行驗證 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中規定的私鑰的加密型別 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);//設定頭部 } curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部資料使用HTTP協議中的"POST"操作來發送 $data = curl_exec($ch);//執行回話 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } /** * [sendMoney 企業付款到零錢] * @param [type] $amount [傳送的金額(分)目前傳送金額不能少於1元] * @param [type] $re_openid [傳送人的 openid] * @param string $desc [企業付款描述資訊 (必填)] * @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,//企業付款描述資訊 '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'; //呼叫介面 $res=curl_post_ssl($url,$xml); $return=xmltoarray($res); print_r($return); //返回來的結果是xml,最後轉換成陣列 /* 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,則發生成功,處理自己的邏輯 return $res; }

三、呼叫方法

1
sendMoney(1,'gdgfdg56456223423','xxxx測試','xxx');

呼叫的話,稍微在修改下,加上自己的業務邏輯就行了。

四、呼叫效果

2

3

也可以下載檔案,我寫的是一個類檔案:
PHP微信商戶支付企業付款到零錢功能