zoukankan      html  css  js  c++  java
  • ThinkPHP整合微信支付之发裂变红包

    1.去商户平台里,给你的商户充钱,没钱是发不了红包哒! 2.微信红包需要证书支持,所以请大家到商户平台下去下载好证书后放到安全文件夹下,并且需要在配置文件中指定好证书路径!
    好,接下来带来裂变红包具体功能实现代码:
    step1:重复一下配置文件WxPayConf_pub.php,看过之前微信支付教程的同学应该很清楚这一块了,这里我将代码截图出来,配置好后进行下一步!


    step2:下载你的证书,放到一个目录下,对应配置文件中,记得这里是绝对路径!


    step3:与微信现金红包一样,我们得自己在WxPayHelper.php文件下写自己的红包支付方法,这里的主要区别就是createxm

    /**
     * 红包支付接口
     * @author gaoyl101
     */
    class Groupredpack_pub extends Wxpay_client_pub
    {
        var $code;//code码,用以获取openid
        var $openid;//用户的openid
        function __construct()
        {
            //设置接口链接
            $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
            //设置curl超时时间
            $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
        }
        /**
         * 生成接口参数xml
         */
        function createXml()
        {
            try
            {
                //检测必填参数
                if($this->parameters["mch_billno"] == null)
                {
                    throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."<br>");
                }elseif ($this->parameters["send_name"] == null ) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."<br>");
                }elseif ($this->parameters["total_amount"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."<br>");
                }elseif ($this->parameters["total_num"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."<br>");
                }elseif ($this->parameters["amt_type"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数amt_type!"."<br>");
                }elseif ($this->parameters["wishing"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."<br>");
                }elseif ($this->parameters["act_name"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."<br>");
                }elseif ($this->parameters["remark"] == null) {
                    throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."<br>");
                }
                $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
                $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
                $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
                $this->parameters["re_openid"] = $this->openid;//用户openid
                $this->parameters["sign"] = $this->getSign($this->parameters);//签名
                return  $this->arrayToXml($this->parameters);
            }catch (SDKRuntimeException $e)
            {
                die($e->errorMessage());
            }
        }
        function sendRedpack()
        {
            $this->postXmlSSL();
            $this->result = $this->xmlToArray($this->response);
            return $this->result;
        }
        /**
         *     作用:生成可以获得code的url
         */
        function createOauthUrlForCode($redirectUrl)
        {
            $urlObj["appid"] = WxPayConf_pub::APPID;
            $urlObj["redirect_uri"] = "$redirectUrl";
            $urlObj["response_type"] = "code";
            $urlObj["scope"] = "snsapi_base";
            $urlObj["state"] = "STATE"."#wechat_redirect";
            $bizString = $this->formatBizQueryParaMap($urlObj, false);
            return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
        }
        /**
         *     作用:生成可以获得openid的url
         */
        function createOauthUrlForOpenid()
        {
            $urlObj["appid"] = WxPayConf_pub::APPID;
            $urlObj["secret"] = WxPayConf_pub::APPSECRET;
            $urlObj["code"] = $this->code;
            $urlObj["grant_type"] = "authorization_code";
            $bizString = $this->formatBizQueryParaMap($urlObj, false);
            return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
        }
        /**
         *     作用:通过curl向微信提交code,以获取openid
         */
        function getOpenid()
        {
            $url = $this->createOauthUrlForOpenid();
            //初始化curl
            $ch = curl_init();
            //设置超时
            curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            //运行curl,结果以jason形式返回
            $res = curl_exec($ch);
            curl_close($ch);
            //取出openid
            $data = json_decode($res,true);
            $this->openid = $data['openid'];
            return $this->openid;
        }
        /**
         *     作用:设置code
         */
        function setCode($code_)
        {
            $this->code = $code_;
        }
    }
    

     step4:创建控制器WxGroupRedPackController


    控制器中的代码:
    1.引入WxPayHelper.php类库

    /**
         * 初始化
         */
        public function _initialize()
        {
            //引入WxPayPubHelper
            vendor('WxPayPubHelper.WxPayPubHelper');
        }
    

     2.创建发送红包方法:sendRedpack,这个方法就是发送红包的具体功能代码!

    /**
         * 发送红包
         */
        public function sendRedpack()
        {
            //调用请求接口基类
            $Redpack = new Groupredpack_pub();
            
            //=========步骤1:网页授权获取用户openid============
            //通过code获得openid
            if (!isset($_GET['code']))
            {
                //触发微信返回code码
                $reduct_uri = WEB_HOST."/index.php/Home/WxGroupRedPack/sendRedpack";
                $url = $Redpack->createOauthUrlForCode($reduct_uri);
                Header("Location: $url");
            }else
            {
                //获取code码,以获取openid
                $code = $_GET['code'];
                $Redpack->setCode($code);
                $openid = $Redpack->getOpenId();
            }
             
            
            
            //商户订单号
            $Redpack->setParameter('mch_billno', C('WxPayConf_pub.APPID')."static");
            //商户名称
            $Redpack->setParameter('send_name', "gaoyl101");
            //用户openid
    //         $Redpack->setParameter('re_openid', $parameterValue);
            //付款金额
            $Redpack->setParameter('total_amount', 100);
            //红包发放总人数
            $Redpack->setParameter('total_num', 3);
            $Redpack->setParameter('amt_type','ALL_RAND');
            //红包祝福语
            $Redpack->setParameter('wishing', "现金红包教程祝大家写代码快乐");
            //活动名称
            $Redpack->setParameter('act_name', "现金红包教程");
            //备注
            $Redpack->setParameter('remark', "现金红包教程祝大家写代码快乐");
            //以下是非必填项目
            //子商户号  
    //         $Redpack->setParameter('sub_mch_id', $parameterValue);
    //        //商户logo的url
    //         $Redpack->setParameter('amt_list', '200|100|100');
            
            
            $result = $Redpack->sendRedpack();
            
            dump($result);
        }
    

     访问这个方法,微信就会发裂变红包啦
    在这里我dump了微信发送红包之后返回的结果,下面的业务逻辑就可以根据自己的需求接下去写了,返回值的说明可以看微信红包的接口说明,在微信支付平台上有。
    到这里微信红包现金红包代码已经全部结束,功能经过测试已经完成!

    原文转载地址 : http://www.thinkphp.cn/code/1330.html

    微信支付之jsapi:
    http://www.thinkphp.cn/code/1321.html
    微信支付教程扫码模式一:
    http://www.thinkphp.cn/code/1322.html
    微信支付教程扫码模式二:
    http://www.thinkphp.cn/code/1323.html
    微信支付教程刷卡支付:
    http://www.thinkphp.cn/code/1324.html
    微信现金红包教程:
    http://www.thinkphp.cn/code/1328.html

  • 相关阅读:
    从头到尾测地理解KMP算法【转】
    【Android】使用BaseAdapter实现复杂的ListView【转】
    Git命令速查表【转】
    图解Git命令【转】
    Git-入门教程
    自定义Git【转】
    linux命令大全
    ppt转pdf网址
    【转】设置电脑眼睛保护色(背景色)
    【转】putty基本操作--不错
  • 原文地址:https://www.cnblogs.com/gyrgyr/p/6039225.html
Copyright © 2011-2022 走看看