微信付款

代码示例:(thinkphp)

 

<?php
namespace Admin\Controller;
/******************************************
 * 微信付款功能
 * @Author   wzb [email protected]
 * @time     2015-11-6
 ****************************************/

class WxPaymentController extends AdminController{

    // 初始化函数(检测是否已经扩展该控制器)
    protected function _initialize($type=1){
        $config =   api('Config/lists');
        C($config); //添加配置

        $res = D('Admin/Addons')->where("name='WxPayment'")->find();
        if(!is_array($res)){
            if($type == 1){
                $this->error("您没有权限使用该插件,请联系开发人员!",U('Getmoney/userIndex'));exit;
            }
            return ("您没有权限使用该插件,请联系开发人员!");
        };
    }


    // 打款操作
    function wx_payment($pay_info,$type=1){
//封装成数据
        $dataArr['desc']         = $desc    = "您的此次提现已到账,感谢您使用".C('WEB_NAME');
        $dataArr['mchid']        = $mchid   = "1272****01";
        $dataArr['openid']       = $openid;
        $dataArr['amount']       = $amount  = $pay_info['price']*100;
        $dataArr['mch_appid']    = $mch_appid = C('WX_APPID'); // 公众号appid
        $dataArr['nonce_str']    = $nonce_str = 'WXGY'.rand(10000,99999);  //随机数
        $dataArr['check_name']   = $check_name = "NO_CHECK";
        $dataArr['re_user_name'] = $re_user_name = $pay_info['uid'].str_replace("&", '', $user_info['nick']); //用户姓名
        $dataArr['partner_trade_no'] = $partner_trade_no = 'WXGY'.time().rand(10000,99999);
        $dataArr['spbill_create_ip'] = $spbill_create_ip = $_SERVER["REMOTE_ADDR"]; // 请求ip

        $sign = $this->getSign($dataArr); // 生成签名

        $data = "<xml>
                <mch_appid>".$mch_appid."</mch_appid>
                <mchid>".$mchid."</mchid>
                <nonce_str>".$nonce_str."</nonce_str>
                <partner_trade_no>".$partner_trade_no."</partner_trade_no>
                <openid>".$openid."</openid>
                <check_name>".$check_name."</check_name>
                <re_user_name>".$re_user_name."</re_user_name>
                <amount>".$amount."</amount>
                <desc>".$desc."</desc>
                <spbill_create_ip>".$spbill_create_ip."</spbill_create_ip>
                <sign>".$sign."</sign>
                </xml>";

        // 执行微信公众号付款
        $cer_path   ="./certificate/".C('HOOTS_URL_H')."/apiclient_cert.pem";
        $cer_path_2 ="./certificate/".C('HOOTS_URL_H')."/apiclient_key.pem";
        if(!is_file($cer_path) || !is_file($cer_path_2)){
            if($type == 1){
                $this->error('无法获取微信证书文件,无法提现!');exit;
            }
            return('无法获取微信证书文件,无法提现!');
        }

        $wx_pay = $this->curl_post_ssl($data,$cer_path,$cer_path_2);
        $wx_pay = json_encode(simplexml_load_string($wx_pay, null, LIBXML_NOCDATA));
        $wx_pay = json_decode($wx_pay,TRUE);

        if($wx_pay['return_code'] != 'SUCCESS'){
            // 付款失败
            return("微信付款失败!".$wx_pay['return_msg']);
        }else{
            // 付款成功 
     return("付款成功!");
        } 
    }


    /*****************************
     *  作用:生成签名
     *****************************/
    private function getSign($Obj){
        foreach ($Obj as $k => $v)    {
            $Parameters[$k] = $v;
        }
        ksort($Parameters);     //签名步骤一:按字典序排序参数
        $String = $this->formatBizQueryParaMap($Parameters, false);
        $String = $String."&key=".C('WX_AUTO_PAY_KEY');//签名步骤二:在string后加入KEY
        $String = md5($String);             //签名步骤三:MD5加密
        $result_ = strtoupper($String);     //签名步骤四:所有字符转为大写
        return $result_;
    }

    /**********************************************
     *  作用:格式化参数,签名过程需要使用
     *********************************************/
    private function formatBizQueryParaMap($paraMap, $urlencode){
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)        {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0){
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    /****************************
     * 微信付款请求函数
     ***************************/
    function curl_post_ssl($vars,$cer_path,$cer_path_2, $second=30){
        $url="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);//超时时间
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_SSLCERT,realpath($cer_path));
        curl_setopt($ch,CURLOPT_SSLKEY, realpath($cer_path_2));

        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            curl_close($ch);
            return $data;
        }
    }


}        

 

你可能感兴趣的:(微信付款)