微信企业付款到零钱

这里是一个测试代码,仅供参考,运行没问题,但是用到线上的话建议修改一下逻辑,多做些风险处理。

public function sendMoney(){
        header("Content-Type: text/html;charset=utf-8");
        $amount = $this->request->request("money");
        $re_openid = $this->openid;
        $total_amount = (100) * $amount;
        $time = date('Y-m-d H:i:s',time());
        //校验金额
        $user = Db('user')->where(array('wx_id' => $re_openid))->find();
        if(!$user){
            $this->error(__('Please login first'), null, -1);
        }
        if($user['money'] < $amount){
            $this->error(__('余额不足'));
        }
        $data=array(
            'mch_appid'=>config('site.app_id')?config('site.app_id'):'',//商户账号appid
            'mchid'=> config('site.mch_id')?config('site.mch_id'):'',//商户号
            'nonce_str'=>$this->createNoncestr(),//随机字符串
            'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商户订单号
            'openid'=> $re_openid,//用户openid  
            'check_name'=>'NO_CHECK',//校验用户姓名选项,
            'amount'=>$total_amount,//金额
            'desc'=> '测试提现',//企业付款描述信息
            'spbill_create_ip'=> '你的ip地址',//Ip地址
        );
        $secrect_key=config('site.key')?config('site.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']=strtoupper(md5($str));
        $xml=$this->arraytoxml($data);
        $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
        $res=$this->curl($xml,$url);
        $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
        if($responseObj->result_code == 'SUCCESS'){//成功处理逻辑
            
            $this->success(__('提现成功'));
        }else{//返回失败原因
            $this->error('提现失败,请稍后重试',$responseObj->err_code_des);
        }
    }
    function curl($param="",$url) {
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 增加 HTTP Header(头)里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch,CURLOPT_SSLCERT,ROOT_PATH .'addons/epay/certs/apiclient_cert.pem'); //这个是证书的位置绝对路径
        curl_setopt($ch,CURLOPT_SSLKEY,ROOT_PATH .'addons/epay/certs/apiclient_key.pem'); //这个也是证书的位置绝对路径
        $data = curl_exec($ch); //运行curl
        curl_close($ch);
        return $data;
    }
    function arraytoxml($data){
        $str='';
        foreach($data as $k=>$v) {
            $str.='<'.$k.'>'.$v.'';
        }
        $str.='';
        return $str;
    }

 

你可能感兴趣的:(PHP基础)