php微信企业付款到个人微信零钱的类

<?php
#================================================
# Author: yujiang
# Date: 2020-07-28
# Filename: GetWechatController.class.php
# Description:企业付款到雇个人零钱
#================================================

namespace App\Controller;

use Think\Controller;

class GetwechatController extends Controller
{   
    private $state = 'state';
    private $app_id = 'wx88b47***9bc6d0';
    private $app_secret = '34d0d********d0c58f';
    private $APPID="wx8**********";//打款商户appid
    private $MCHID="1589873221";//打款商户号
    private $SECRECT_KEY="8416*******c4";//支付密钥签名 商户平台设置的api秘钥
    private $ip="127.0.0.1";//ip
    /**
     * 获取用户openid
     */
    public function getopenid(){
        header("Access-Control-Allow-Origin:*");
        $Withdrawalopenid=M("user")->where('id='.I("get.userid"))->find();
        if(!empty($Withdrawalopenid['Withdrawalopenid'])){
           $this->sendMoney(1,$Withdrawalopenid['Withdrawalopenid'],$desc='测试',$check_name='于江');
        }
        if (!isset($_GET['code'])) {
            $userid = I("get.userid");
            //触发微信返回code码
            $url = $this->get_authorize_url($userid);

            Header("Location: $url");
            exit();
        } else {
            //获取code码,以获取openid
			$userid = I("get.userid");
            $code = $_GET['code'];
            $access_info = $this->get_access_token($userid,$code);
			$result=M("user")->where('id='.$userid)->setField('Withdrawalopenid',$access_info['openid']);
			if($result){
				
			}
        }
    }
      /**
     * 获取授权token网页授权
     *
     * @param string $code 通过get_authorize_url获取到的code
     */
    private function get_access_token($userid,$code = '')
    {	
	
        $appid = $this->app_id;
        $appsecret = $this->app_secret;
        $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";
		//file_put_contents("./tokentest",$token_url);
        $token_data = $this->httpget($token_url);
        $res = json_decode($token_data, true);
		 return $res;
    }
    //获取用户基本信息
	private function gethead($access_token,$openid){
		$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$res=$this->httpget($url);
		$data=json_decode($res, true);
		return $data;
	}
    //获取微信授权链接
    private function get_authorize_url($userid)
    {
        $redirect_uri = urlencode(BASEURL . "/index.php/App/Getwechat/getopenid?userid=".$userid);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$this->state}#wechat_redirect";
        return $url;
    }
    protected function httpget($url, $params, $method = 'GET', $header = array(), $multi = false) {
        $opts = array(CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header);
 
        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)) {
            case 'GET' :
                $opts[CURLOPT_URL] = $url . '&' . http_build_query($params);
                //dump($opts[CURLOPT_URL]);
                break;
            case 'POST' :
                //判断是否传输文件
                $params = $multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                
               // dump($opts[CURLOPT_URL]);
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            default :
                throw new Exception('不支持的请求方式!');
        }
 
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error)
            throw new Exception('请求发生错误:' . $error);
        return $data;
    }
            /*
        $amount 发送的金额(分)目前发送金额不能少于1元
        $re_openid, 发送人的 openid
        $desc  //  企业付款描述信息 (必填)
        $check_name    收款用户姓名 (选填)
        */
     public   function sendMoney($amount,$re_openid,$desc='测试',$check_name=''){
            $total_amount = (100) * $amount;
            $data=array(
                'mch_appid'=>$this->APPID,//商户账号appid
                'mchid'=>$this-> MCHID,//商户号
                'nonce_str'=>$this->createNoncestr(),//随机字符串
                'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
                'openid'=> $re_openid,//用户openid
                'check_name'=>'NO_CHECK',//校验用户姓名选项,
                're_user_name'=> $check_name,//收款用户姓名
                'amount'=>$total_amount,//金额
                'desc'=> $desc,//企业付款描述信息
                'spbill_create_ip'=>$this->ip,//Ip地址
            );
            $secrect_key=$this->SECRECT_KEY;///这个就是个API密码。MD5 32位。
            $data=array_filter($data);
            ksort($data);
            $str='';
            foreach($data as $k=>$v) {
            $str.=$k.'='.$v.'&';
            }
            $str.='key='.$secrect_key;
            $data['sign']=md5($str);
            $xml=$this->arraytoxml($data);

            $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
            $res=$this->curl_post_ssl($url,$xml);
            $return=$this->xmltoarray($res);
           print_r($return);die;
            //返回来的结果
            // [return_code] => SUCCESS [return_msg] => Array ( ) [mch_appid] => wxd44b890e61f72c63 [mchid] => 1493475512 [nonce_str] => 616615516 [result_code] => SUCCESS [partner_trade_no] => 20186505080216815 
            // [payment_no] => 1000018361251805057502564679 [payment_time] => 2018-05-15 15:29:50
			//Array ( [return_code] => SUCCESS [return_msg] => 支付失败 [mch_appid] => wx88b471db3c9bc6d0 [mchid] => 1589873221 [result_code] => FAIL [err_code] => NOTENOUGH [err_code_des] => 余额不足 )

            $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
            echo $res= $responseObj->return_code;  //SUCCESS  如果返回来SUCCESS,则发生成功,处理自己的逻辑

            return $res;
        }
          /**
   * [createNoncestr 生成随机字符串]
   * @param  integer $length [长度]
   * @return [type]          [字母大小写加数字]
   */
     private     function createNoncestr($length =32) 
        {
            $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";  
            $str ="";
            for ( $i = 0; $i < $length; $i++ )  {  
                $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
            }  
            return $str;
        }

    private    function unicode() {
            $str = uniqid(mt_rand(),1);
            $str=sha1($str);
            return md5($str);
        }
          /**
   * [arraytoxml 将数组转换成xml格式(简单方法):]
   * @param  [type] $data [数组]
   * @return [type]       [array 转 xml]
   */
    private   function arraytoxml($data){
            $str='<xml>';
            foreach($data as $k=>$v) {
                $str.='<'.$k.'>'.$v.'</'.$k.'>';
            }
            $str.='</xml>';
            return $str;
        }
    
  /**
   * [xmltoarray xml格式转换为数组]
   * @param  [type] $xml [xml]
   * @return [type]      [xml 转化为array]
   */
    private    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;
        } 

/**
   * [curl_post_ssl 发送curl_post数据]
   * @param  [type]  $url     [发送地址]
   * @param  [type]  $xmldata [发送文件格式]
   * @param  [type]  $second [设置执行最长秒数]
   * @param  [type]  $aHeader [设置头部]
   * @return [type]           [description]
   */
    private   function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
		
        $isdir =$_SERVER['DOCUMENT_ROOT']."/1589873221cert/";//证书位置;绝对路径
  
        $ch = curl_init();//初始化curl
  
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        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中规定的私钥的加密类型
        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, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送
  
        $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;
        }
    }
}

如果报call faild, errorCode:58错误,证书的绝对路径不对

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值