温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现微信提现功能

发布时间:2021-05-27 16:42:06 来源:亿速云 阅读:267 作者:Leah 栏目:开发技术

这篇文章给大家介绍使用PHP怎么实现微信提现功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

用户提现代码:

//用户微信提现
 private function withdrawals_weixin($id){
    $falg = M('withdrawals')->where(['id'=>$id])->find();
    $openid = M('users')->where('user_id', $falg['user_id'])->value('openid');
    $data['openid'] = $openid;
    $data['pay_code'] = $falg['id'].$falg['user_id'];
    $data['desc'] = '提现ID'.$falg['id'];
    if($falg['taxfee'] >= $falg['money']){
      return array('status'=>1, 'msg'=>"提现额度必须大于手续费!" );
    }else{
      $data['money'] = bcsub($falg['money'], $falg['taxfee'], 2);
    }
    include_once PLUGIN_PATH . "payment/weixin/weixin.class.php";
    $weixin_obj = new \weixin();
    $result = $weixin_obj->transfer($data);
   
    return $result;
 }

其中pay_code在商户号的提现功能是唯一的,所以为了防重放攻击,这个值千万不能用随机数,最好用ID,具有提现记录唯一。

提现逻辑代码:

// 微信提现转账
  function transfer($data){
    
    header("Content-type: text/html; charset=utf-8");
    //CA证书及支付信息
   $wxchat['appid'] = WxPayConfig::$appid;
   $wxchat['mchid'] = WxPayConfig::$mchid;
 
   $wxchat['api_cert'] = PLUGIN_PATH.'/payment/weixin/cert/apiclient_cert.pem';
    $wxchat['api_key'] = PLUGIN_PATH.'/payment/weixin/cert/apiclient_key.pem';
    
    // $wxchat['api_ca'] = '/plugins/payment/weixin/cert/rootca.pem';
   $webdata = array(
    'mch_appid' => $wxchat['appid'],
    'mchid'   => $wxchat['mchid'],
    'nonce_str' => md5(time()),
    //'device_info' => '1000',
    'partner_trade_no'=> $data['pay_code'], //商户订单号,需要唯一
    'openid' => $data['openid'],//转账用户的openid
    'check_name'=> 'NO_CHECK', //OPTION_CHECK不强制校验真实姓名, FORCE_CHECK:强制 NO_CHECK:
    //'re_user_name' => 'jorsh', //收款人用户姓名
    'amount' => $data['money'] * 100, //付款金额单位为分
    'desc'  => $data['desc'],
    'spbill_create_ip' => request()->ip(),
    );
  
   foreach ($webdata as $k => $v) {
   $tarr[] =$k.'='.$v;
    }
 
   sort($tarr);
   $sign = implode($tarr, '&');
   $sign .= '&key='.WxPayConfig::$key;
    $webdata['sign']=strtoupper(md5($sign));
    
    $wget = $this->array2xml($webdata);
    
    $pay_url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
 
    $res = $this->http_post($pay_url, $wget, $wxchat);
 
   if(!$res){
   return array('status'=>1, 'msg'=>"Can't connect the server" );
   }
    $content = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
    
   if(strval($content->return_code) == 'FAIL'){
   return array('status'=>1, 'msg'=>strval($content->return_msg));
   }
   if(strval($content->result_code) == 'FAIL'){
   return array('status'=>1, 'msg'=>strval($content->err_code),':'.strval($content->err_code_des));
    }
 
   $rdata = array(
    'mch_appid'    => strval($content->mch_appid),
    'mchid'      => strval($content->mchid),
    'device_info'   => strval($content->device_info),
    'nonce_str'    => strval($content->nonce_str),
    'result_code'   => strval($content->result_code),
    'partner_trade_no' => strval($content->partner_trade_no),
    'payment_no'    => strval($content->payment_no),
    'payment_time'   => strval($content->payment_time),
   );
   return $rdata;
 
  }

其中 PLUGIN_PATH 是一个常量

define('PLUGIN_PATH', __DIR__ . '/plugins/');

定义插件目录

/**
   * 将一个数组转换为 XML 结构的字符串
   * @param array $arr 要转换的数组
   * @param int $level 节点层级, 1 为 Root.
   * @return string XML 结构的字符串
   */
  function array2xml($arr, $level = 1) {
   $s = $level == 1 ? "<xml>" : '';
   foreach($arr as $tagname => $value) {
   if (is_numeric($tagname)) {
    $tagname = $value['TagName'];
    unset($value['TagName']);
   }
   if(!is_array($value)) {
    $s .= "<{$tagname}>".(!is_numeric($value) ? '<![CDATA[' : '').$value.(!is_numeric($value) ? ']]>' : '')."</{$tagname}>";
   } else {
    $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>";
   }
   }
   $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
   return $level == 1 ? $s."</xml>" : $s;
  }
  
  function http_post($url, $param, $wxchat) {
   $oCurl = curl_init();
   if (stripos($url, "https://") !== FALSE) {
   curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
   }
   if (is_string($param)) {
   $strPOST = $param;
   } else {
   $aPOST = array();
   foreach ($param as $key => $val) {
    $aPOST[] = $key . "=" . urlencode($val);
   }
   $strPOST = join("&", $aPOST);
   }
   curl_setopt($oCurl, CURLOPT_URL, $url);
   curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($oCurl, CURLOPT_POST, true);
   curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
   if($wxchat){
   curl_setopt($oCurl,CURLOPT_SSLCERT,$wxchat['api_cert']);
   curl_setopt($oCurl,CURLOPT_SSLKEY,$wxchat['api_key']);
   curl_setopt($oCurl,CURLOPT_CAINFO,$wxchat['api_ca']);
   }
   $sContent = curl_exec($oCurl);
   $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    
   if (intval($aStatus["http_code"]) == 200) {
   return $sContent;
   } else {
   return false;
   }
 }

关于使用PHP怎么实现微信提现功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI