主頁 > 知識庫 > PHP實現(xiàn)微信提現(xiàn)功能(微信商城)

PHP實現(xiàn)微信提現(xiàn)功能(微信商城)

熱門標簽:江門回撥外呼系統(tǒng) 泊頭在哪里辦理400電話 高德地圖標注店鋪收費嗎 高德地圖標注位置怎么標注 欣思維地圖標注 江西電銷機器人收費 天潤融通外呼系統(tǒng)好嗎 電銷機器人沒有效果怎么樣 杭州語音電銷機器人

提現(xiàn)必須得用雙向證書、所以大家一定要在微信的商戶平臺找到相應的地方去設置、因為做這個提現(xiàn)已經(jīng)有一段時間了、所以設置微信商戶平臺的那幾個地方?jīng)]有圖的情況、也說不清楚、下次再做提現(xiàn)的時候、給大家分享如何設置商戶平臺那幾個地方、不是很難、下面貼代碼

注意事項:商戶打款時是從商戶可用余額中減錢,所以確保商戶可用余額充足,同時注意官方文檔中的付款規(guī)則;

封裝提現(xiàn)的方法

function tixian($money){
  $appid = "################";//商戶賬號appid
  $secret = "##########";//api密碼
  $mch_id = "#######";//商戶號
  $mch_no = "#######";
  $openid="123456789";//授權用戶openid

  $arr = array();
  $arr['mch_appid'] = $appid;
  $arr['mchid'] = $mch_id;
  $arr['nonce_str'] = ugv::randomid(20);//隨機字符串,不長于32位
  $arr['partner_trade_no'] = '1298016501' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商戶訂單號
  $arr['openid'] = $openid;
  $arr['check_name'] = 'NO_CHECK';//是否驗證用戶真實姓名,這里不驗證
  $arr['amount'] = $money;//付款金額,單位為分
  $desc = "###提現(xiàn)";
  $arr['desc'] = $desc;//描述信息
  $arr['spbill_create_ip'] = '192.168.0.1';//獲取服務器的ip
  //封裝的關于簽名的算法
  $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);
}

用到的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);//設置執(zhí)行最長秒數(shù)
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上
  curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網(wǎng)頁
  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中規(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);//設置頭部
  }
  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;
  }
}

關于具體簽名算法,可參考微信官方文檔;

簡單示范簽名算法:

//將要發(fā)送的數(shù)據(jù)整理為$data
ksort($data);//排序
//使用URL鍵值對的格式(即key1=value1key2=value2…)拼接成字符串
$str='';
foreach($data as $k=>$v) {
 $str.=$k.'='.$v.'';
}
//拼接API密鑰
$str.='key='.$secrect;
$data['sign']=md5($str);//加密

將數(shù)組轉換成xml格式(簡單方法):

//遍歷數(shù)組方法
function arraytoxml($data){
 $str='xml>';
 foreach($data as $k=>$v) {
  $str.=''.$k.'>'.$v.'/'.$k.'>';
 }
 $str.='/xml>';
 return $str;
}

將xml格式轉換為數(shù)組:

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;
}

下面來看看ThinkPHP5封裝的提現(xiàn)類。

?php
namespace Home\Controller;
use Think\Controller;
class TixianController extends Controller{
 //高級功能-》開發(fā)者模式-》獲取
 private $app_id1 = '';  //appid
 private $app_secret1 = ''; //secreat
 private $apikey1 = ''; //支付秘鑰
 private $mchid1 = 's';  //商戶號
  private $app_id=null;
  private $app_secret=null;
  private $apikey=null;
  private $mchid=null;
 public $error=0;
 public $state = '';
 //金額,需在實例化時傳入
 public $amount = '0';
 //用戶訂單號,需在實例化時傳入
 public $order_sn = '';
 //用戶openid,需在實例化時傳入
 public $openid = '';
 //微信提現(xiàn)操作接口-------》
 public function actionAct_tixian()
 {
  $this->state=md5(uniqid(rand(), TRUE));
  $this->amount=I('amount');//設置POST過來錢數(shù)
  $this->order_sn=rand(100,999).date('YmdHis'); //隨機數(shù)可以作為單號
  $this->openid= I('openid'); //設置獲取POST過來用戶的OPENID
  $user_id = I('user_id');
  $this->app_id=$this->app_id1;
  $this->app_secret=$this->app_secret1;
  $this->apikey=$this->apikey1;
  $this->mchid=$this->mchid1;
  $xml=$this->tiXianAction();
  $result=simplexml_load_string($xml);
  if($result->return_code=='SUCCESS'  $result->result_code=='SUCCESS') {
    $cash = D('cash');
    $data['user_id'] = $user_id;
    $data['amount'] = $this->amount;
    $res = $cash->where('user_id="'.$user_id.'"')->find();
    if($res){
     $res2 = $cash->where('user_id="'.$user_id.'"')->setInc('amount',$this->amount);
     $res4 = D('member')->where('user_id="'.$user_id.'"')->setDec('user_balance',$this->amount);
    }else{
     $res3 = $cash->add($data);
    }
   $output = array('code' => 1, 'data' => $result->result_code, 'info' => '提現(xiàn)成功');
   exit(json_encode($output));
  }else{
   $output = array('code' => 2, 'data' => $xml, 'info' => '提現(xiàn)失敗');
   exit(json_encode($output));
  }
 }
 /**
 * 提現(xiàn)接口操作,控制器調(diào)用
 * @param $openid 用戶openid 唯一標示
 * @return
 */
 //提現(xiàn)接口操作
 public function tiXianAction(){
  //獲取xml數(shù)據(jù)
  $data=$this->getdataXml($this->openid);
  $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 );
  //證書地址,微信支付下面
  curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  curl_setopt($ch,CURLOPT_SSLCERT, 'C:\web\www\Home\wx_pay\apiclient_cert.pem'); //證書這塊大家把文件放到哪都行、
  curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  curl_setopt($ch,CURLOPT_SSLKEY, 'C:\web\www\Home\wx_pay\apiclient_key.pem');//注意證書名字千萬別寫錯、
  //$zs1=dirname(dirname(__FILE__)).'\wx_pay\apiclient_cert.pem';
  //$zs2=dirname(dirname(__FILE__)).'\wx_pay\apiclient_key.pem';
  //show_bug($zs1);
  //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 );
  //返回結果
  if($info){
   curl_close($ch);
   return $info;
  } else {
   $error = curl_errno($ch);
   curl_close($ch);
   return "curl出錯,錯誤碼:$error";
  }
 }
 /**
 * 獲取數(shù)據(jù)封裝為數(shù)組
 * @param $openid 用戶openid 唯一標示
 * @return xml
 */
 private function getdataXml($openid){
  //封裝成數(shù)據(jù)
  $dataArr=array(
   'amount'=>$this->amount*100,//金額(以分為單位,必須大于100)
   'check_name'=>'NO_CHECK',//校驗用戶姓名選項,NO_CHECK:不校驗真實姓名 FORCE_CHECK:強校驗真實姓名(未實名認證的用戶會校驗失敗,無法轉賬)OPTION_CHECK:針對已實名認證的用戶才校驗真實姓名(未實名認證用戶不校驗,可以轉賬成功)
   'desc'=>'提現(xiàn)',//描述
   'mch_appid'=>$this->app_id,
   'mchid'=>$this->mchid,//商戶號
   'nonce_str'=>rand(100000, 999999),//不長于32位的隨機數(shù)
   'openid'=>$openid,//用戶唯一標識
   'partner_trade_no'=>$this->order_sn,//商戶訂單號
   're_user_name'=>'',//用戶姓名,check_name為NO_CHECK時為可選項
   'spbill_create_ip'=>$_SERVER["REMOTE_ADDR"],//服務器ip
  );
  //獲取簽名
  $sign=$this->getSign($dataArr);
  //xml數(shù)據(jù)
  $data="xml>
   mch_appid>".$dataArr['mch_appid']."/mch_appid>
   mchid>".$dataArr['mchid']."/mchid>
   nonce_str>".$dataArr['nonce_str']."/nonce_str>
   partner_trade_no>".$dataArr['partner_trade_no']."/partner_trade_no>
   openid>".$dataArr['openid']."/openid>
   check_name>".$dataArr['check_name']."/check_name>
   re_user_name>".$dataArr['re_user_name']."/re_user_name>
   amount>".$dataArr['amount']."/amount>
   desc>".$dataArr['desc']."/desc>
   spbill_create_ip>".$dataArr['spbill_create_ip']."/spbill_create_ip>
   sign>".$sign."/sign>
   /xml>";
  return $data;
 }
 /**
 *  作用:格式化參數(shù),簽名過程需要使用
 */
 private function formatBizQueryParaMap($paraMap, $urlencode)
 {
  $buff = "";
  ksort($paraMap);
  foreach ($paraMap as $k => $v)
  {
   if($v){
   if($urlencode)
   {
    $v = urlencode($v);
   }
   $buff .= $k . "=" . $v . "";
   }
  }
  $reqPar=NULL;
  if (strlen($buff) > 0)
  {
   $reqPar = substr($buff, 0, strlen($buff)-1);
  }
  return $reqPar;
 }
 /**
 *  作用:生成簽名
 */
 private function getSign($Obj)
 {
  foreach ($Obj as $k => $v)
  {
   $Parameters[$k] = $v;
  }
  //簽名步驟一:按字典序排序參數(shù)
  ksort($Parameters);
  $String = $this->formatBizQueryParaMap($Parameters, false);
  //echo '【string1】'.$String.'/br>';
  //簽名步驟二:在string后加入KEY
  $String = $String."key=".$this->apikey;
  //echo "【string2】".$String."/br>";
  //簽名步驟三:MD5加密
  $String = md5($String);
  //echo "【string3】 ".$String."/br>";
  //簽名步驟四:所有字符轉為大寫
  $result_ = strtoupper($String);
  //echo "【result】 ".$result_."/br>";
  return $result_;
 }
 //-----------
 private function http($url, $method='POST', $postfields = null, $headers = array())
 {
  header("Content-Type:text/html;charset=utf-8");
  $ch = curl_init();
  /* Curl settings */
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https請求 不驗證證書和hosts
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  switch ($method){
   case 'POST':
   curl_setopt($ch,CURLOPT_POST, true);
   break;
  }
  curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  $response = curl_exec($ch);
  $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //返回請求狀態(tài)碼
  curl_close($ch);
  return array($http_code, $response);
 }
}

總結

以上所述是小編給大家介紹的PHP實現(xiàn)微信提現(xiàn)功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

您可能感興趣的文章:
  • PHP實現(xiàn)微信提現(xiàn)(企業(yè)付款到零錢)
  • PHP APP微信提現(xiàn)接口代碼
  • PHP實現(xiàn)微信提現(xiàn)功能

標簽:石嘴山 駐馬店 深圳 雙鴨山 江門 內(nèi)江 大同

巨人網(wǎng)絡通訊聲明:本文標題《PHP實現(xiàn)微信提現(xiàn)功能(微信商城)》,本文關鍵詞  PHP,實現(xiàn),微信,提現(xiàn),功能,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP實現(xiàn)微信提現(xiàn)功能(微信商城)》相關的同類信息!
  • 本頁收集關于PHP實現(xiàn)微信提現(xiàn)功能(微信商城)的相關信息資訊供網(wǎng)民參考!
  • 推薦文章