企業付款的應用情境: 公眾號向已關注使用者付款,比如處理退款、財務結算等,本文主要和大家分享php開發支付企業付款執行個體代碼,希望能協助到大家。
說明
1.認證需要用自己的商戶裡面的認證(注意:憑證路徑必須是絕對路徑,如果使用相對路徑會報下面的錯誤。
unable to use client certificate (no key found or wrong pass phrase?)
2.appid,secret以及key填寫自己的即可。
先說一下實現思路:
1.首先擷取openid,具體方法見下面。
2.填寫必填參數,產生簽名等,具體方法見下面。
/** * API 參數 * @var array * ‘mch_appid’ # 公眾號APPID * ‘mchid’ # 商戶號 * ‘device_info’ # 裝置號 * ‘nonce_str’ # 隨機字串 * ‘partner_trade_no’ # 商戶訂單號 * ‘openid’ # 收款使用者openid * ‘check_name’ # 校正使用者姓名選項 針對實名認證的使用者 * ‘re_user_name’ # 收款使用者姓名 * ‘amount’ # 付款金額 * ‘desc’ # 企業付款描述資訊 * ‘spbill_create_ip’ # Ip地址 * ‘sign’ # 簽名 */
參數參考: 企業付款API的文檔
1.擷取CODE(index.php頁面)
<?php //資訊回調檔案所在的伺服器位置$str="http://www.xxx.com/company_pay/getInfo.php";$str_url=urlencode($str);$appid = "xxxx3e5273505e";$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$str_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';header("Location:".$url);?>
2.資訊回調頁面代碼處理(getInfo.php)
<?php$appid = "wxxxxx3505e";//你的公眾平台的appid$secret = "fxxxxx71xxx4cda2a671";//你公眾平台的secret$code = $_GET["code"];$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$get_token_url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);$res = curl_exec($ch);curl_close($ch);$json_obj = json_decode($res,true);//根據openid和access_token查詢使用者資訊$access_token = $json_obj['access_token'];$openid = $json_obj['openid'];$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$get_user_info_url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);$res = curl_exec($ch);curl_close($ch);//解析json$user_obj = json_decode($res,true);//var_dump($user_obj);echo "<br/>"."-----".$openid."*****";$mch_appid=$appid;$mchid='10000401';//商戶號$nonce_str='vhmake'.rand(100000, 999999);//隨機數$partner_trade_no='VH'.time().rand(10000, 99999);//商戶訂單號$openid=$openid;//使用者唯一標識$check_name='NO_CHECK';//校正使用者姓名選項,NO_CHECK:不校正真實姓名 FORCE_CHECK:強校正真實姓名(未實名認證的使用者會校正失敗,無法轉賬)OPTION_CHECK:針對已實名認證的使用者才校正真實姓名(未實名認證使用者不校正,可以轉賬成功)$re_user_name='[北京微函工坊科技有限公司](http://www.vhmake.com)';//使用者姓名$amount=100;//金額(以分為單位,必須大於100)$desc='[北京微函工坊科技有限公司](http://www.vhmake.com)';//描述$spbill_create_ip=$_SERVER["REMOTE_ADDR"];//請求ip//封裝成資料$dataArr=array();$dataArr['amount']=$amount;$dataArr['check_name']=$check_name;$dataArr['desc']=$desc;$dataArr['mch_appid']=$mch_appid;$dataArr['mchid']=$mchid;$dataArr['nonce_str']=$nonce_str;$dataArr['openid']=$openid;$dataArr['partner_trade_no']=$partner_trade_no;$dataArr['re_user_name']=$re_user_name;$dataArr['spbill_create_ip']=$spbill_create_ip;require 'api.php';$sign=getSign($dataArr);echo "-----<br/>簽名:".$sign."<br/>*****";//die;$data="<xml><mch_appid>".$mch_appid."</mch_appid><mchid>".$mchid."</mchid><nonce_str>".$nonce_str."</nonce_str><partner_trade_no>".$partner_trade_no."</partner_trade_no><openid>".$openid."</openid><check_name>".$check_name."</check_name><re_user_name>".$re_user_name."</re_user_name><amount>".$amount."</amount><desc>".$desc."</desc><spbill_create_ip>".$spbill_create_ip."</spbill_create_ip><sign>".$sign."</sign></xml>";//var_dump($data);$ch = curl_init ();$MENU_URL="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";curl_setopt ( $ch, CURLOPT_URL, $MENU_URL );curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );$zs1="/xxxx/xxx/xxxxxx/apiclient_cert.pem";//注意:填寫的路徑必須為絕對路徑,不可以填寫相對路徑$zs2="/xxxx/xxx/xxxxx/apiclient_key.pem";curl_setopt($ch,CURLOPT_SSLCERT,$zs1);curl_setopt($ch,CURLOPT_SSLKEY,$zs2);// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01;// Windows NT 5.0)');curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );$info = curl_exec ( $ch );$infos=simplexml_load_string($info);if (curl_errno ( $ch )) { echo 'Errno:::' . curl_error ( $ch );}curl_close ( $ch );echo "-----<br/>請求傳回值:";echo $infos->return_code;echo "<br/>*****";?>
3.產生簽名函數檔案(api.php)
<?php/** * 作用:格式化參數,簽名過程需要使用 */function formatBizQueryParaMap($paraMap, $urlencode){ $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if($urlencode) { $v = urlencode($v); } $buff .= $k . "=" . $v . "&"; } if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar;}/** * 作用:產生簽名 */function getSign($Obj){ foreach ($Obj as $k => $v) { $Parameters[$k] = $v; } //簽名步驟一:按字典序排序參數 ksort($Parameters); $String = formatBizQueryParaMap($Parameters, false); //echo '【string1】'.$String.'</br>'; //簽名步驟二:在string後加入KEY $String = $String."&key=vhmake666vhmake666vhmake666vhmak"; //echo "【string2】".$String."</br>"; //簽名步驟三:MD5加密 $String = md5($String); //echo "【string3】 ".$String."</br>"; //簽名步驟四:所有字元轉為大寫 $result_ = strtoupper($String); //echo "【result】 ".$result_."</br>"; return $result_;}
成功之後返回xml格式的返回參數(具體的請參考開發文檔)
<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[]]></return_msg><mch_appid><![CDATA[wxec38b8ff840bd989]]></mch_appid><mchid><![CDATA[10013274]]></mchid><device_info><![CDATA[]]></device_info><nonce_str><![CDATA[lxuDzMnRjpcXzxLx0q]]></nonce_str><result_code><![CDATA[SUCCESS]]></result_code><partner_trade_no><![CDATA[10013574201505191526582441]]></partner_trade_no><payment_no><![CDATA[1000018301201505190181489473]]></payment_no><payment_time><![CDATA[2015-05-19 15:26:59]]></payment_time></xml>