Home  >  Article  >  php教程  >  ThinkPHP整合微信支付之发现金红包

ThinkPHP整合微信支付之发现金红包

WBOY
WBOYOriginal
2016-06-07 11:38:011708browse

大家好,微信支付系列教程四种方式已经结束,如果你以为结束了就错了,有同学跟我提到微信还有红包功能,我开始也没注意这一块,于是看了下微信商户平台上有讲到这一块,微信支付平台上也早就有了,于是趁热打铁,研究了下,继续发出关于微信红包的教程文章。接下来请看微信支付发红包之现金红包教程!
现在微信商户可以向指定的openid发送红包,目前红包分两种:现金红包和裂变红包。本教程是关于现金红包的。

在贴代码之前,先讲几个注意点:1.去商户平台里,给你的商户充钱,没钱是发不了红包哒! 2.微信红包需要证书支持,所以请大家到商户平台下去下载好证书后放到安全文件夹下,并且需要在配置文件中指定好证书路径!

step1:老样子,还是介绍配置文件WxPayConf_pub.php,看过之前微信支付教程的同学应该很清楚这一块了,这里我将代码截图出来,配置好后进行下一步!
ThinkPHP整合微信支付之发现金红包

step2:下载你的证书,放到一个目录下,对应配置文件中,记得这里是绝对路径!
ThinkPHP整合微信支付之发现金红包

step3:之前的微信支付的demo微信官方已经帮我们写好了WxPayHelper.php这个类库,我们可以很方便的调用就够了,而微信红包目前还没有官方demo,所以这里我们得自己在WxPayHelper.php文件下写自己的红包支付方法:/**
 * 现金红包接口
 * @author gaoyl101
 */
class Redpack_pub extends Wxpay_client_pub
{
    var $code;//code码,用以获取openid
    var $openid;//用户的openid
    
    function __construct()
    {
        //设置接口链接
        $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        //设置curl超时时间
        $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    }

    /**
     * 生成接口参数xml
     */
    function createXml()
    {
        try
        {
            //检测必填参数
            if($this->parameters["mch_billno"] == null)
            {
                throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."
");
            }elseif($this->parameters["nick_name"] == null){
                throw new SDKRuntimeException("缺少发红包接口必填参数nick_name!"."
");
            }elseif ($this->parameters["send_name"] == null ) {
                throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."
");
            }elseif ($this->parameters["total_amount"] == null) {
                throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."
");
            }elseif($this->parameters["min_value"] == null){
                throw new SDKRuntimeException("缺少发红包接口必填参数min_value!"."
");
            }elseif ($this->parameters["max_value"] == null ) {
                throw new SDKRuntimeException("缺少发红包接口必填参数max_value!"."
");
            }elseif ($this->parameters["total_num"] == null) {
                throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
");
            }elseif ($this->parameters["wishing"] == null) {
                throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."
");
            }elseif ($this->parameters["act_name"] == null) {
                throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."
");
            }elseif ($this->parameters["remark"] == null) {
                throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."
");
            }
            $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
            $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
            $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
            $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_;
    }
}
其实这里的代码我做的并不是很好,我并没有封装,因为做裂变红包也会用到相似的代码,这里做demo我就先不改了,有兴趣的朋友可以在此基础上继续晚上!上面的代码就是我们要用到的工具类,把他放在WxPayHelper.php最下面就可以了!

step4:创建控制器WxCashRedPackController
ThinkPHP整合微信支付之发现金红包

控制器中的代码:
1.引入WxPayHelper.php类库/**
     * 初始化
     */
    public function _initialize()
    {
        //引入WxPayPubHelper
        vendor('WxPayPubHelper.WxPayPubHelper');
    }
2.创建发送红包方法:sendRedpack,这个方法就是发送红包的具体功能代码!/**
     * 发送红包
     */
    public function sendRedpack()
    {
        //调用请求接口基类
        $Redpack = new \Redpack_pub();
        
        //=========步骤1:网页授权获取用户openid============
        //通过code获得openid
        if (!isset($_GET['code']))
        {
            //触发微信返回code码
            $reduct_uri = WEB_HOST."/index.php/Home/WxCashRedPack/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('nick_name', "gaoyl101");
        //商户名称
        $Redpack->setParameter('send_name', "gaoyl101");
        //用户openid
//         $Redpack->setParameter('re_openid', $parameterValue);
        //付款金额
        $Redpack->setParameter('total_amount', 100);
        //最小红包金额
        $Redpack->setParameter('min_value', 100);
        //最大红包金额
        $Redpack->setParameter('max_value', 100);
        //红包发放总人数
        $Redpack->setParameter('total_num', 1);
        //红包祝福语
        $Redpack->setParameter('wishing', "现金红包教程祝大家写代码快乐");
        //活动名称
        $Redpack->setParameter('act_name', "现金红包教程");
        //备注
        $Redpack->setParameter('remark', "现金红包教程祝大家写代码快乐");
        //以下是非必填项目
        //子商户号  
//         $Redpack->setParameter('sub_mch_id', $parameterValue);
//        //商户logo的url
//         $Redpack->setParameter('logo_imgurl', $parameterValue);
//         //分享文案
//         $Redpack->setParameter('share_content', $parameterValue);
//         //分享链接
//         $Redpack->setParameter('share_url', $parameterValue);
//         //分享的图片
//         $Redpack->setParameter('share_imgurl', $parameterValue);
        
        
        
        $result = $Redpack->sendRedpack();
        
        dump($result);
    }
访问这个方法,微信就会发红包啦
在这里我dump了微信发送红包之后返回的结果,下面的业务逻辑就可以根据自己的需求接下去写了,返回值的说明可以看微信红包的接口说明,在微信支付平台上有。
到这里微信红包现金红包代码已经全部结束,功能经过测试已经完成!

下面是成功后的红包截图:
ThinkPHP整合微信支付之发现金红包

之前的几篇微信支付的教程很多同学看了之后都会遇到问题,并找到我得到了解决,个人认为我发的文章得到了它应有的价值,希望这篇文章也能帮到正在为发送红包而发愁的同学!

有问题请留言,下面还会介绍微信发红包之裂变红包!

微信支付之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/1330.html
欢迎大家吐槽,转载请说明出处,请支持原创,谢谢!
我们的微信开发群:422579975(已满) 105195188(未满),代码已放在群文件中
欢迎大家加入讨论问题

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn