首页 > 作文

PHP实现微信提现功能(微信商城)

更新时间:2023-04-08 05:31:32 阅读: 评论:0

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

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

封装提现的方法

function tixian($money){  $appid = "################";//商户账号appid  $cret = "##########";//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 = "###提现";  $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,   );  } el {   $returnmsg = (string)$rdata->return_msg;   $isrr = array(    'error' => 1,    'errmsg' => $returnmsg,   );  }  return json_encode($isrr);}

用到的curl_post_ssl()

function curl_post_ssl($url, $vars, $cond = 30, $aheader = array()) {  $isdir = "/cert/";//证书位置  $ch = curl_init();//初始化curl  curl_topt($ch, curlopt_timeout, $cond);//设置执行最长秒数  curl_topt($ch, curlopt_returntransfer, 1);//要求结果为字符串且输出到屏幕上  curl_topt($ch, curlopt_url, $url);//抓取指定网页  curl_topt($ch, curlopt_ssl_verifypeer, fal);// 终止从服务端进行验证  curl_topt($ch, curlopt_ssl_verifyhost, fal);//  curl_topt($ch, curlopt_sslcerttype, 'pem');//证书类型  curl_topt($ch, curlopt_sslcert, $isdir . 'apiclient_cert.pem');//证书位置  curl_topt($ch, curlopt_sslkeytype, 'pem');//curlopt_sslkey中规定的私钥的加密类型  curl_topt($ch, curlopt_sslkey, $isdir . 'apiclient_key.pem');//证书位置  curl_topt(少先队知识手抄报$ch, curlopt_cainfo, 'pem');  curl_topt($ch, curlopt_cainfo, $isdir . 'rootca.pem');  if (count($aheader) >= 1) {   curl_topt($ch, curlopt_httpheader, $aheader);//设置头部  }  curl_topt($ch, curlopt_post, 1);//post提交方式  curl_topt($ch, curlopt_postfields, $vars);//全部数据使用http协议中的"post"操作来发送  $data = curl_exec($ch);//执行回话  if ($data) {   curl_clo($ch);   return $data;  } el {   $error = curl_errno($ch);   echo "call faild, errorcode:$error\n";   curl_clo($ch);   return fal;  }}

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

简单示范签名算法:

//将要发送的数据整理为$dataksort($data);//排序//使用url键值对的格式(即key1=value1&key2=value2…)拼接成字符串$str='';foreach($data as $k=>$v) { $str.=$k.'='.$v.'&';}//拼接api密钥$str.='key='.$crect;$data['sign']=md5($str);//加密

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

//遍历数组方法function arraytoxml($data){ $str='<xml>'; foreach($data as $k=>$v) {  $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str.='</xml>'; return $str;}

将xml格式转换为数组:

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封装的提现类。

<?phpnamespace home\controller;u think\controller;class tixiancontroller extends controller{ //高级功能-》开发者模式-》获取 private $app_id1 = '';  //appid private $app_cret1 = ''; //creat private $apikey1 = ''; //支付秘钥 private $mchid1 = 's';  //商户号  private $app_id=null;  private $app_cret=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  $ur_id = i('ur_id');  $this->app_id=$this->app_id1;  $this->app_cret=$this->app_cret1;  $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['ur_id'] = $伤感四字网名ur_id;    $data['amount'] = $this->amount;    $res = $cash->where('ur_id="'.$ur_id.'"')->find();    if($res){     $res2 = $cash->where('ur_id="'.$ur_id.'"')->tinc('amount',$this->amount);     $res4 = d('member')->where('ur_id="'.$ur_id.'"')->tdec('ur_balance',$this->amount);    }el{     $res3 = $cash->add($data);    }   $output = array('code' => 1, 'data' => $result->result_code, 'info' => '提现成功');   exit(json_encode($output));  }el{   $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_topt ( $ch, curlopt_url, $menu_url );  curl_topt ( $ch, curlopt_customrequest, "post" );  curl_topt ( $ch, curlopt_ssl_verifypeer, fal );  curl_topt ( $ch, curlopt_ssl_verifyhost, fal );  //证书地址,微信支付下面  curl_topt($ch,curlopt_sslcerttype,'pem');  curl_topt($ch,curlopt_sslcert, 'c:\web\www\home\wx_pay\apiclient_cert.pem'); //证书这块大家把文件放到哪都行、  curl_topt($ch,curlopt_sslkeytype,'pem');  curl_topt($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_topt($ch,curlopt_sslcert,$zs1);  //curl_topt($反重力瑜伽ch,curlopt_sslkey,$zs2);  // curl_topt($ch, curlopt_uragent, 'mozilla/5.0 (compatible; msie 5.01;  // windows nt 5.0)');  //curl_topt ( $ch, curlopt_followlocation, 1 );  curl_topt ( $ch, curlopt_autoreferer, 1 );  curl_topt ( $ch, curlopt_postfields, $data );  curl_topt ( $ch, curlopt_returntransfer, true );  $info = curl_exec ( $ch );  //返回结果  if($info){   curl_clo($ch);   return $info;  } el {   $error = curl_errno($ch);   curl_clo($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_ur_name'=>'',//用户姓名,check_name为no_check时为可选项   'spbill_create_ip'=>$_rver["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_ur_name>".$dataarr['re_ur_name']."</re_ur_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, fal);  //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;chart=utf-8");  $ch = curl_init();  /* curl ttings */  curl_topt($ch, curlopt_url, $url);  curl_topt($ch, curlopt_postfields, "");  curl_topt($ch, curlopt_returntransfer, true);  curl_topt($ch, curlopt_ssl_verifypeer, fal); // https请求 不验证证书和hosts  curl_topt($ch, curlopt_ssl_verifyhost, fal);  curl_topt($ch, curlopt_timeout, 30);  switch ($method){   ca 'post':   curl_topt($ch,curlopt_post, true);   break;  }  curl_topt($ch, curlopt_httpheader,$headers);  curl_topt($ch, curlinfo_header_out, true);  $respon = curl_exec($ch);  $http_code = curl_getinfo($ch, curlinfo_http_code); //返回请求状态码  curl_clo($ch);  return array($http_code, $respon); }}

总结

以上所述是www.887551.com给大家介绍的php实现微信提现功能,希望对大家有所帮助

本文发布于:2023-04-08 05:31:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/16a3dc98565dbeb69d9b91c1241ef705.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP实现微信提现功能(微信商城).doc

本文 PDF 下载地址:PHP实现微信提现功能(微信商城).pdf

标签:商户   用户   证书   数组
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图