博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThinkPHP整合微信支付之发裂变红包
阅读量:4582 次
发布时间:2019-06-09

本文共 6363 字,大约阅读时间需要 21 分钟。

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!"."
"); }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["total_num"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
"); }elseif ($this->parameters["amt_type"] == null) { throw new SDKRuntimeException("缺少发红包接口必填参数amt_type!"."
"); }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["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了微信发送红包之后返回的结果,下面的业务逻辑就可以根据自己的需求接下去写了,返回值的说明可以看微信红包的接口说明,在微信支付平台上有。
到这里微信红包现金红包代码已经全部结束,功能经过测试已经完成!

 

原文转载地址 :

微信支付之jsapi:

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

转载于:https://www.cnblogs.com/gyrgyr/p/6039225.html

你可能感兴趣的文章
SQL 查询横表变竖表
查看>>
异常处理.
查看>>
JAVA比较两张图相似度
查看>>
SQL 中怎么查询数据库中具有的表、存储过程、试图数目、总触发器数、作业数...
查看>>
分布式缓存Redis集群配置使用
查看>>
pycharm中安装扩展包
查看>>
类库 委托 var万能类型
查看>>
我的英语提升计划----第三篇
查看>>
《计算机图形学》2.2.2 光栅扫描显示处理器
查看>>
命令行方式使用abator.jar生成ibatis相关代码和sql语句xml文件
查看>>
使用Java实现单线程模式
查看>>
Web Client Software Factory中CreateNew的使用
查看>>
npm,cnpm,yarn
查看>>
在Activity之间传递参数(一)
查看>>
关于浏览器的选择 360浏览器 firefox chrome
查看>>
Fiddler工具的过滤功能介绍
查看>>
django自定义分页器
查看>>
Python基础之面向对象思维解决游戏《天龙八部》
查看>>
藏宝图题解
查看>>
HDU 3015 Disharmony Trees
查看>>