封装微信小程序提现到零钱
生活随笔
收集整理的這篇文章主要介紹了
封装微信小程序提现到零钱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:小程序提現需要申請的東西這里不詳細介紹了,大伙應該都知道,咱們直接上代碼;
/*** 提現類 商戶號提現至微信零錢* Class Withdrawal* @package app\api\controller*/ class Withdrawal{function __construct() {$this->appid = '';//你的小程序appid// 商戶號$this->mch_id = ''; //你的商戶號。找不到的在你的小程序里邊的微信支付里邊找,前提是你必須先開啟你的微信支付// 支付秘鑰$this->key = ''; //這個是你商戶號的api秘鑰,在產品中心里邊找,}/*** [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();//初始化curlcurl_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,$uid,$withdrawal_id,$pic_id,$re_openid,$partner_trade_no='',$desc='第三巷提現到賬',$check_name=''){$total_amount = (100) * $amount;$data=array('mch_appid'=>$this->appid,//商戶賬號appid'mchid'=> $this->mch_id,//商戶號'nonce_str'=>$this->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地址);//生成簽名算法$secrect_key=$this->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=$this->arraytoxml($data);$url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //調用接口$res=$this->curl_post_ssl($url,$xml);$ret=$this->xmltoarray($res);if($ret['return_code'] == 'SUCCESS' && $ret['result_code'] == 'SUCCESS'){Db::startTrans();try {//扣除自提點余額$sql = Db('ns_pickup_point')->where(['uid'=>$uid,'id'=>$pic_id])->setDec('balance_money',$amount);//修改提現狀態$sql_1 = Db('ns_merchants_balance_withdraw')->where('id',$withdrawal_id)->update(['status'=>3]);Db::commit();if ($sql && $sql_1){return 200;}else{return 400;}}catch(\Exception $e){// 回滾事務Db::rollback();return 402;}}else{ // $this->errorLog('微信付款到銀行卡失敗,appid:'.$this->appid,$ret);return 401;} // $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA); // echo $res= $responseObj->return_code; //SUCCESS 如果返回來SUCCESS,則發生成功,處理自己的邏輯}/*** [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;}/** 將xml轉換成數組* @params xml $xml : xml數據* return array $data : 返回數組*/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;}}直接調用提現類中的sendMoney()即可實現,返回成功內的那段業務代碼你們可以自行封裝看自己的具體需求調用即可總結
以上是生活随笔為你收集整理的封装微信小程序提现到零钱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京移动CM311-5s-ZG_GK63
- 下一篇: Python3自定义栈类