PHP實(shí)現(xiàn)微信提現(xiàn)(企業(yè)付款到零錢(qián))
怎么開(kāi)通企業(yè)付款到零錢(qián)?
有的商戶號(hào)的產(chǎn)品中心是沒(méi)有這個(gè)功能的,不過(guò),該功能的pid(product id)是5,只要隨便進(jìn)去某一個(gè)產(chǎn)品,在地址欄把pid改為5。
即可進(jìn)入該功能頁(yè)面,進(jìn)行開(kāi)通,不過(guò)要滿足條件。
用戶提現(xiàn)代碼:
//用戶微信提現(xiàn) private function withdrawals_weixin($id){ $falg = M('withdrawals')->where(['id'=>$id])->find(); $openid = M('users')->where('user_id', $falg['user_id'])->value('openid'); $data['openid'] = $openid; $data['pay_code'] = $falg['id'].$falg['user_id']; $data['desc'] = '提現(xiàn)ID'.$falg['id']; if($falg['taxfee'] >= $falg['money']){ return array('status'=>1, 'msg'=>"提現(xiàn)額度必須大于手續(xù)費(fèi)!" ); }else{ $data['money'] = bcsub($falg['money'], $falg['taxfee'], 2); } include_once PLUGIN_PATH . "payment/weixin/weixin.class.php"; $weixin_obj = new \weixin(); $result = $weixin_obj->transfer($data); return $result; }
其中pay_code在商戶號(hào)的提現(xiàn)功能是唯一的,所以為了防重放攻擊,這個(gè)值千萬(wàn)不能用隨機(jī)數(shù),最好用ID,具有提現(xiàn)記錄唯一。
提現(xiàn)邏輯代碼:
// 微信提現(xiàn)轉(zhuǎn)賬 function transfer($data){ header("Content-type: text/html; charset=utf-8"); //CA證書(shū)及支付信息 $wxchat['appid'] = WxPayConfig::$appid; $wxchat['mchid'] = WxPayConfig::$mchid; $wxchat['api_cert'] = PLUGIN_PATH.'/payment/weixin/cert/apiclient_cert.pem'; $wxchat['api_key'] = PLUGIN_PATH.'/payment/weixin/cert/apiclient_key.pem'; // $wxchat['api_ca'] = '/plugins/payment/weixin/cert/rootca.pem'; $webdata = array( 'mch_appid' => $wxchat['appid'], 'mchid' => $wxchat['mchid'], 'nonce_str' => md5(time()), //'device_info' => '1000', 'partner_trade_no'=> $data['pay_code'], //商戶訂單號(hào),需要唯一 'openid' => $data['openid'],//轉(zhuǎn)賬用戶的openid 'check_name'=> 'NO_CHECK', //OPTION_CHECK不強(qiáng)制校驗(yàn)真實(shí)姓名, FORCE_CHECK:強(qiáng)制 NO_CHECK: //'re_user_name' => 'jorsh', //收款人用戶姓名 'amount' => $data['money'] * 100, //付款金額單位為分 'desc' => $data['desc'], 'spbill_create_ip' => request()->ip(), ); foreach ($webdata as $k => $v) { $tarr[] =$k.'='.$v; } sort($tarr); $sign = implode($tarr, '&'); $sign .= '&key='.WxPayConfig::$key; $webdata['sign']=strtoupper(md5($sign)); $wget = $this->array2xml($webdata); $pay_url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $res = $this->http_post($pay_url, $wget, $wxchat); if(!$res){ return array('status'=>1, 'msg'=>"Can't connect the server" ); } $content = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA); if(strval($content->return_code) == 'FAIL'){ return array('status'=>1, 'msg'=>strval($content->return_msg)); } if(strval($content->result_code) == 'FAIL'){ return array('status'=>1, 'msg'=>strval($content->err_code),':'.strval($content->err_code_des)); } $rdata = array( 'mch_appid' => strval($content->mch_appid), 'mchid' => strval($content->mchid), 'device_info' => strval($content->device_info), 'nonce_str' => strval($content->nonce_str), 'result_code' => strval($content->result_code), 'partner_trade_no' => strval($content->partner_trade_no), 'payment_no' => strval($content->payment_no), 'payment_time' => strval($content->payment_time), ); return $rdata; }
其中 PLUGIN_PATH 是一個(gè)常量
define('PLUGIN_PATH', __DIR__ . '/plugins/');
定義插件目錄
/** * 將一個(gè)數(shù)組轉(zhuǎn)換為 XML 結(jié)構(gòu)的字符串 * @param array $arr 要轉(zhuǎn)換的數(shù)組 * @param int $level 節(jié)點(diǎn)層級(jí), 1 為 Root. * @return string XML 結(jié)構(gòu)的字符串 */ function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml>" : ''; foreach($arr as $tagname => $value) { if (is_numeric($tagname)) { $tagname = $value['TagName']; unset($value['TagName']); } if(!is_array($value)) { $s .= "<{$tagname}>".(!is_numeric($value) ? '<![CDATA[' : '').$value.(!is_numeric($value) ? ']]>' : '')."</{$tagname}>"; } else { $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>"; } } $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s); return $level == 1 ? $s."</xml>" : $s; } function http_post($url, $param, $wxchat) { $oCurl = curl_init(); if (stripos($url, "https://") !== FALSE) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); } if (is_string($param)) { $strPOST = $param; } else { $aPOST = array(); foreach ($param as $key => $val) { $aPOST[] = $key . "=" . urlencode($val); } $strPOST = join("&", $aPOST); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($oCurl, CURLOPT_POST, true); curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST); if($wxchat){ curl_setopt($oCurl,CURLOPT_SSLCERT,$wxchat['api_cert']); curl_setopt($oCurl,CURLOPT_SSLKEY,$wxchat['api_key']); curl_setopt($oCurl,CURLOPT_CAINFO,$wxchat['api_ca']); } $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) { return $sContent; } else { return false; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php+js實(shí)現(xiàn)圖片的上傳、裁剪、預(yù)覽、提交示例
首先用到的語(yǔ)言是php、插件imgareaselect附下載地址,沒(méi)有太多花哨的樣式,喜歡的朋友可以學(xué)習(xí)下2013-08-08php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié))
這篇文章主要介紹了php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12解析php中mysql_connect與mysql_pconncet的區(qū)別詳解
本篇文章是對(duì)php中mysql_connect與mysql_pconncet的區(qū)別進(jìn)行了分析介紹,需要的朋友參考下2013-05-05CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫(kù)增刪改查操作總結(jié)
這篇文章主要介紹了CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫(kù)增刪改查操作,結(jié)合實(shí)例形式總結(jié)分析了CI框架針對(duì)mysql數(shù)據(jù)庫(kù)增刪改查操作的模型、控制器及視圖相關(guān)定義與使用技巧,需要的朋友可以參考下2018-05-05php生成靜態(tài)頁(yè)面并實(shí)現(xiàn)預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了php生成靜態(tài)頁(yè)面并實(shí)現(xiàn)預(yù)覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06