PHP实现微信提现功能

提现必须得用双向证书、所以大家一定要在微信的商户平台找到相应的地方去设置、因为做这个提现已经有一段时间了、所以设置微信商户平台的那几个地方没有图的情况、也说不清楚、下次再做提现的时候、给大家分享如何设置商户平台那几个地方、不是很难、下面贴代码

注意事项:商户打款时是从商户可用余额中减钱,所以确保商户可用余额充足,同时注意官方文档中的付款规则;

 

封装提现的方法

 1 function tixian($money){
 2     $appid = "################";//商户账号appid
 3     $secret = "##########";//api密码
 4     $mch_id = "#######";//商户号
 5     $mch_no = "#######";
 6     $openid="123456789";//授权用户openid
 7 
 8     $arr = array();
 9     $arr['mch_appid'] = $appid;
10     $arr['mchid'] = $mch_id;
11     $arr['nonce_str'] = ugv::randomid(20);//随机字符串,不长于32位
12     $arr['partner_trade_no'] = '1298016501' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商户订单号
13     $arr['openid'] = $openid;
14     $arr['check_name'] = 'NO_CHECK';//是否验证用户真实姓名,这里不验证
15     $arr['amount'] = $money;//付款金额,单位为分
16     $desc = "###提现";
17     $arr['desc'] = $desc;//描述信息
18     $arr['spbill_create_ip'] = '192.168.0.1';//获取服务器的ip
19     //封装的关于签名的算法
20     $notify = new Notify_pub();
21     $notify->weixin_app_config = array();
22     $notify->weixin_app_config['KEY'] = $mch_no;
23 
24     $arr['sign'] = $notify->getSign($arr);//签名
25 
26     $var = $notify->arrayToXml($arr);
27     $xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $var, 30, array(), 1);
28     $rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
29     $return_code = (string)$rdata->return_code;
30     $result_code = (string)$rdata->result_code;
31     $return_code = trim(strtoupper($return_code));
32     $result_code = trim(strtoupper($result_code));
33 
34     if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
35       $isrr = array(
36         'con'=>'ok',
37         'error' => 0,
38       );
39     } else {
40       $returnmsg = (string)$rdata->return_msg;
41       $isrr = array(
42         'error' => 1,
43         'errmsg' => $returnmsg,
44       );
45 
46     }
47     return json_encode($isrr);
48 } 

 

 

用到的curl_post_ssl()

 1 function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
 2   {
 3     $isdir = "/cert/";//证书位置
 4 
 5     $ch = curl_init();//初始化curl
 6 
 7     curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
 8     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
 9     curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
10     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
11     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
12     curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
13     curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
14     curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
15     curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
16     curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
17     curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
18     if (count($aHeader) >= 1) {
19       curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
20     }
21     curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
22     curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送
23 
24     $data = curl_exec($ch);//执行回话
25     if ($data) {
26       curl_close($ch);
27       return $data;
28     } else {
29       $error = curl_errno($ch);
30       echo "call faild, errorCode:$error\n";
31       curl_close($ch);
32       return false;
33     }
34 }

 

关于具体签名算法,可参考微信官方文档;

简单示范签名算法:

 1 //将要发送的数据整理为$data
 2 
 3 ksort($data);//排序
 4 //使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串
 5 $str='';
 6 foreach($data as $k=>$v) {
 7   $str.=$k.'='.$v.'&';
 8 }
 9 //拼接API密钥
10 $str.='key='.$secrect;
11 $data['sign']=md5($str);//加密

 

 

将数组转换成xml格式(简单方法):

1 //遍历数组方法
2 function arraytoxml($data){
3   $str='<xml>';
4   foreach($data as $k=>$v) {
5     $str.='<'.$k.'>'.$v.'</'.$k.'>';
6   }
7   $str.='</xml>';
8   return $str;
9 }

 

 

将xml格式转换为数组:

1 function xmltoarray($xml) { 
2    //禁止引用外部xml实体 
3   libxml_disable_entity_loader(true); 
4   $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
5   $val = json_decode(json_encode($xmlstring),true); 
6   return $val;
7 }
8  

 

 

下面来看看ThinkPHP5封装的提现类。

  1 <?php
  2 namespace Home\Controller;
  3 use Think\Controller;
  4 class TixianController extends Controller{
  5 
  6   //高级功能-》开发者模式-》获取
  7   private $app_id1 = '';   //appid
  8   private $app_secret1 = ''; //secreat
  9   private $apikey1 = ''; //支付秘钥
 10   private $mchid1 = 's';    //商户号
 11 
 12     private $app_id=null;
 13     private $app_secret=null;
 14     private $apikey=null;
 15     private $mchid=null;
 16 
 17 
 18   public $error=0;
 19   public $state = '';
 20   //金额,需在实例化时传入
 21   public $amount = '0';
 22   //用户订单号,需在实例化时传入
 23   public $order_sn = '';
 24   //用户openid,需在实例化时传入
 25   public $openid = '';
 26 
 27 
 28 
 29   //微信提现操作接口-------》
 30   public function actionAct_tixian()
 31   {
 32 
 33    $this->state=md5(uniqid(rand(), TRUE));
 34    $this->amount=I('amount');//设置POST过来钱数
 35    $this->order_sn=rand(100,999).date('YmdHis'); //随机数可以作为单号
 36    $this->openid= I('openid'); //设置获取POST过来用户的OPENID
 37     $user_id = I('user_id');
 38 
 39    $this->app_id=$this->app_id1;
 40    $this->app_secret=$this->app_secret1;
 41    $this->apikey=$this->apikey1;
 42    $this->mchid=$this->mchid1;
 43    $xml=$this->tiXianAction();
 44    $result=simplexml_load_string($xml);
 45 
 46    if($result->return_code=='SUCCESS' && $result->result_code=='SUCCESS') {
 47 
 48         $cash = D('cash');
 49         $data['user_id'] = $user_id;
 50         $data['amount'] = $this->amount;
 51         $res = $cash->where('user_id="'.$user_id.'"')->find();
 52         if($res){
 53           $res2 = $cash->where('user_id="'.$user_id.'"')->setInc('amount',$this->amount);
 54           $res4 = D('member')->where('user_id="'.$user_id.'"')->setDec('user_balance',$this->amount);
 55         }else{
 56           $res3 = $cash->add($data);
 57         }
 58 
 59       $output = array('code' => 1, 'data' => $result->result_code, 'info' => '提现成功');
 60       exit(json_encode($output));
 61    }else{
 62 
 63       $output = array('code' => 2, 'data' => $xml, 'info' => '提现失败');
 64       exit(json_encode($output));
 65    }
 66   }
 67   /**
 68   * 提现接口操作,控制器调用
 69   * @param $openid 用户openid 唯一标示
 70   * @return
 71   */
 72   //提现接口操作
 73   public function tiXianAction(){
 74    //获取xml数据
 75    $data=$this->getdataXml($this->openid);
 76    $ch = curl_init ();
 77    //接口地址
 78    $MENU_URL="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
 79 
 80    curl_setopt ( $ch, CURLOPT_URL, $MENU_URL );
 81    curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
 82    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
 83    curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
 84 
 85    //证书地址,微信支付下面
 86 
 87     curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
 88     curl_setopt($ch,CURLOPT_SSLCERT, 'C:\web\www\Home\wx_pay\apiclient_cert.pem'); //证书这块大家把文件放到哪都行、
 89     curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
 90     curl_setopt($ch,CURLOPT_SSLKEY, 'C:\web\www\Home\wx_pay\apiclient_key.pem');//注意证书名字千万别写错、
 91 
 92    //$zs1=dirname(dirname(__FILE__)).'\wx_pay\apiclient_cert.pem';
 93    //$zs2=dirname(dirname(__FILE__)).'\wx_pay\apiclient_key.pem';
 94    //show_bug($zs1);
 95 
 96    //curl_setopt($ch,CURLOPT_SSLCERT,$zs1);
 97    //curl_setopt($ch,CURLOPT_SSLKEY,$zs2);
 98    // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01;
 99    // Windows NT 5.0)');
100    //curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
101    curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
102    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
103    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
104    $info = curl_exec ( $ch );
105     //返回结果
106     if($info){
107       curl_close($ch);
108       return $info;
109     } else {
110       $error = curl_errno($ch);
111       curl_close($ch);
112       return "curl出错,错误码:$error";
113     }
114   }
115   /**
116   * 获取数据封装为数组
117   * @param $openid 用户openid 唯一标示
118   * @return xml
119   */
120 
121   private function getdataXml($openid){
122    //封装成数据
123    $dataArr=array(
124      'amount'=>$this->amount*100,//金额(以分为单位,必须大于100)
125      'check_name'=>'NO_CHECK',//校验用户姓名选项,NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
126      'desc'=>'提现',//描述
127      'mch_appid'=>$this->app_id,
128      'mchid'=>$this->mchid,//商户号
129      'nonce_str'=>rand(100000, 999999),//不长于32位的随机数
130      'openid'=>$openid,//用户唯一标识
131      'partner_trade_no'=>$this->order_sn,//商户订单号
132      're_user_name'=>'',//用户姓名,check_name为NO_CHECK时为可选项
133      'spbill_create_ip'=>$_SERVER["REMOTE_ADDR"],//服务器ip
134    );
135    //获取签名
136    $sign=$this->getSign($dataArr);
137    //xml数据
138    $data="<xml>
139      <mch_appid>".$dataArr['mch_appid']."</mch_appid>
140      <mchid>".$dataArr['mchid']."</mchid>
141      <nonce_str>".$dataArr['nonce_str']."</nonce_str>
142      <partner_trade_no>".$dataArr['partner_trade_no']."</partner_trade_no>
143      <openid>".$dataArr['openid']."</openid>
144      <check_name>".$dataArr['check_name']."</check_name>
145      <re_user_name>".$dataArr['re_user_name']."</re_user_name>
146      <amount>".$dataArr['amount']."</amount>
147      <desc>".$dataArr['desc']."</desc>
148      <spbill_create_ip>".$dataArr['spbill_create_ip']."</spbill_create_ip>
149      <sign>".$sign."</sign>
150      </xml>";
151    return $data;
152 
153   }
154   /**
155   *   作用:格式化参数,签名过程需要使用
156   */
157   private function formatBizQueryParaMap($paraMap, $urlencode)
158   {
159 
160    $buff = "";
161    ksort($paraMap);
162    foreach ($paraMap as $k => $v)
163    {
164      if($v){
165       if($urlencode)
166       {
167         $v = urlencode($v);
168       }
169 
170       $buff .= $k . "=" . $v . "&";
171      }
172 
173    }
174    $reqPar=NULL;
175    if (strlen($buff) > 0)
176    {
177      $reqPar = substr($buff, 0, strlen($buff)-1);
178    }
179 
180    return $reqPar;
181   }
182 
183   /**
184   *   作用:生成签名
185   */
186   private function getSign($Obj)
187   {
188 
189    foreach ($Obj as $k => $v)
190    {
191      $Parameters[$k] = $v;
192    }
193    //签名步骤一:按字典序排序参数
194    ksort($Parameters);
195    $String = $this->formatBizQueryParaMap($Parameters, false);
196    //echo '【string1】'.$String.'</br>';
197    //签名步骤二:在string后加入KEY
198    $String = $String."&key=".$this->apikey;
199    //echo "【string2】".$String."</br>";
200    //签名步骤三:MD5加密
201    $String = md5($String);
202    //echo "【string3】 ".$String."</br>";
203    //签名步骤四:所有字符转为大写
204    $result_ = strtoupper($String);
205    //echo "【result】 ".$result_."</br>";
206    return $result_;
207   }
208   //-----------
209   private function http($url, $method='POST', $postfields = null, $headers = array())
210   {
211    header("Content-Type:text/html;charset=utf-8");
212    $ch = curl_init();
213    /* Curl settings */
214    curl_setopt($ch, CURLOPT_URL, $url);
215    curl_setopt($ch, CURLOPT_POSTFIELDS, "");
216    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
217    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
218    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
219    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
220    switch ($method){
221      case 'POST':
222       curl_setopt($ch,CURLOPT_POST, true);
223       break;
224    }
225    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
226    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
227    $response = curl_exec($ch);
228    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //返回请求状态码
229    curl_close($ch);
230    return array($http_code, $response);
231   }
232 
233 }

 很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的加群(点击→)677079770

posted @ 2019-11-20 21:03  八重樱  阅读(2604)  评论(0编辑  收藏  举报