本文實(shí)例為大家分享了PHP實(shí)現(xiàn)微信提現(xiàn)功能的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)功能
這幾天在小程序里要實(shí)現(xiàn)用戶從系統(tǒng)中提現(xiàn)到零錢的功能,查了一下文檔可以使用 企業(yè)付款到用戶零錢 來實(shí)現(xiàn);
官方文檔
注意事項(xiàng):商戶打款時(shí)是從商戶可用余額中減錢,所以確保商戶可用余額充足,同時(shí)注意官方文檔中的付款規(guī)則;
二、PHP實(shí)現(xiàn)
//封裝提現(xiàn)方法
function tixian($money){
$appid = "################";//商戶賬號(hào)appid
$secret = "##########";//api密碼
$mch_id = "#######";//商戶號(hào)
$mch_no = "#######";
$openid="123456789";//授權(quán)用戶openid
$arr = array();
$arr['mch_appid'] = $appid;
$arr['mchid'] = $mch_id;
$arr['nonce_str'] = ugv::randomid(20);//隨機(jī)字符串,不長(zhǎng)于32位
$arr['partner_trade_no'] = '1298016501' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商戶訂單號(hào)
$arr['openid'] = $openid;
$arr['check_name'] = 'NO_CHECK';//是否驗(yàn)證用戶真實(shí)姓名,這里不驗(yàn)證
$arr['amount'] = $money;//付款金額,單位為分
$desc = "###提現(xiàn)";
$arr['desc'] = $desc;//描述信息
$arr['spbill_create_ip'] = '192.168.0.1';//獲取服務(wù)器的ip
//封裝的關(guān)于簽名的算法
$notify = new Notify_pub();
$notify->weixin_app_config = array();
$notify->weixin_app_config['KEY'] = $mch_no;
$arr['sign'] = $notify->getSign($arr);//簽名
$var = $notify->arrayToXml($arr);
$xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $var, 30, array(), 1);
$rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_code = (string)$rdata->return_code;
$result_code = (string)$rdata->result_code;
$return_code = trim(strtoupper($return_code));
$result_code = trim(strtoupper($result_code));
if ($return_code == 'SUCCESS' $result_code == 'SUCCESS') {
$isrr = array(
'con'=>'ok',
'error' => 0,
);
} else {
$returnmsg = (string)$rdata->return_msg;
$isrr = array(
'error' => 1,
'errmsg' => $returnmsg,
);
}
return json_encode($isrr);
}
//上個(gè)方法中用到的curl_post_ssl()
function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
{
$isdir = "/cert/";//證書位置
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設(shè)置執(zhí)行最長(zhǎng)秒數(shù)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網(wǎng)頁
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務(wù)端進(jìn)行驗(yàn)證
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, $vars);//全部數(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;
}
}
三、補(bǔ)充
關(guān)于具體簽名算法,可參考微信官方文檔;
簡(jiǎn)單示范簽名算法:
//將要發(fā)送的數(shù)據(jù)整理為$data
ksort($data);//排序
//使用URL鍵值對(duì)的格式(即key1=value1key2=value2…)拼接成字符串
$str='';
foreach($data as $k=>$v) {
$str.=$k.'='.$v.'';
}
//拼接API密鑰
$str.='key='.$secrect;
$data['sign']=md5($str);//加密
將數(shù)組轉(zhuǎn)換成xml格式(簡(jiǎn)單方法):
//遍歷數(shù)組方法
function arraytoxml($data){
$str='xml>';
foreach($data as $k=>$v) {
$str.=''.$k.'>'.$v.'/'.$k.'>';
}
$str.='/xml>';
return $str;
}
將xml格式轉(zhuǎn)換為數(shù)組:
function xmltoarray($xml) {
//禁止引用外部xml實(shí)體
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- php實(shí)現(xiàn)微信支付之退款功能
- PHP實(shí)現(xiàn)微信申請(qǐng)退款流程實(shí)例代碼
- PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解
- PHP開發(fā)實(shí)現(xiàn)微信退款功能示例
- 微信企業(yè)轉(zhuǎn)賬之入口類分裝php代碼
- PHP實(shí)現(xiàn)微信對(duì)賬單處理
- php提取微信賬單的有效信息
- php實(shí)現(xiàn)微信公眾號(hào)企業(yè)轉(zhuǎn)賬功能
- PHP APP微信提現(xiàn)接口代碼
- PHP實(shí)現(xiàn)微信申請(qǐng)退款功能