Withdrawal must use two-way certificate, so you must be in WeChat business platform to find the corresponding place Settings, because the withdrawal has been a long time, so setting WeChat merchants platforms without the several parts of the figure, also said not clearly, next time again to carry, and to share how to set up business platform that a few place, not very hard, stick a generation of below code

Note: The merchant will deduct the amount from the merchant’s available balance when making the payment, so make sure the merchant’s available balance is sufficient and pay attention to the payment rules in the official documents;

Encapsulated withdrawal methods

function tixian($money){ $appid = "################"; Appid $secret = "##########"; // API password $McH_id = "#######"; $McH_no = "#######"; $openid="123456789"; Openid $arr = array(); $arr['mch_appid'] = $appid; $arr['mchid'] = $mch_id; $arr['nonce_str'] = ugv::randomid(20); $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 = "### withdraw "; $arr['desc'] = $desc; $arr[' spbill_create_IP '] = '192.168.0.1'; $notify = new Notify_pub(); $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); }Copy the code

Use 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); Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); Curl_setopt ($ch, $url, $url); $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'); 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); Curl_setopt ($ch, CURLOPT_POSTFIELDS, $vars); $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; }}Copy the code

For specific signature algorithms, please refer to official wechat documents.

Simple demonstration signature algorithm:

$data ksort($data); // sort // use the format of URL key-value pairs (i.e. Key1 =value1&key2=value2...) Concatenate the string $STR = "; foreach($data as $k=>$v) { $str.=$k.'='.$v.'&'; $STR.='key='.$secrect; $data['sign']=md5($str); / / encryptionCopy the code

Convert an array to XML format (easy method) :

Function arrayToxml ($data){$STR ='< XML >'; foreach($data as $k=>$v) { $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str.='</xml>'; return $str; }Copy the code

Convert XML format to array:

Function xmltoarray($XML) {// Disallow references to external XML entities libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; }Copy the code

Let’s take a look at the withdrawal classes packaged in ThinkPHP5.

<?php
namespace Home\Controller;
use Think\Controller;
class TixianController extends Controller{

  //高级功能-》开发者模式-》获取
  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 = '';

  //微信提现操作接口-------》
  public function actionAct_tixian()
  {

   $this->state=md5(uniqid(rand(), TRUE));
   $this->amount=I('amount');//设置POST过来钱数
   $this->order_sn=rand(100,999).date('YmdHis'); //随机数可以作为单号
   $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' => '提现成功');
      exit(json_encode($output));
   }else{

      $output = array('code' => 2, 'data' => $xml, 'info' => '提现失败');
      exit(json_encode($output));
   }
  }
  /**
  * 提现接口操作,控制器调用
  * @param $openid 用户openid 唯一标示
  * @return
  */
  //提现接口操作
  public function tiXianAction(){
   //获取xml数据
   $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";
    }
  }
  /**
  * 获取数据封装为数组
  * @param $openid 用户openid 唯一标示
  * @return xml
  */

  private function getdataXml($openid){
   //封装成数据
   $dataArr=array(
     'amount'=>$this->amount*100,//金额(以分为单位,必须大于100)
     'check_name'=>'NO_CHECK',//校验用户姓名选项,NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
     'desc'=>'提现',//描述
     'mch_appid'=>$this->app_id,
     'mchid'=>$this->mchid,//商户号
     'nonce_str'=>rand(100000, 999999),//不长于32位的随机数
     '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数据
   $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;

  }
  /**
  *   作用:格式化参数,签名过程需要使用
  */
  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;
   }
   //签名步骤一:按字典序排序参数
   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); //返回请求状态码
   curl_close($ch);
   return array($http_code, $response);
  }

}
Copy the code

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you