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

浏览:13670 发布日期:2015/07/09 分类:用法示例 关键字: ThinkPHP整合微信红包 微信红包 微信红包现金红包 ThinkPHP

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

现在微信商户可以向指定的openid发送红包,目前红包分两种:现金红包和裂变红包。本教程是关于现金红包的。

注意点

在贴代码之前,先讲几个注意点:

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

step1:配置文件WxPayConf_pub.php

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

step2:下载证书

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

step3:编写红包类库Redpack_pub

之前的微信支付的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

控制器中的代码:

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了微信发送红包之后返回的结果,下面的业务逻辑就可以根据自己的需求接下去写了,返回值的说明可以看微信红包的接口说明,在微信支付平台上有。

到这里微信红包现金红包代码已经全部结束,功能经过测试已经完成!

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部