Heim  >  Artikel  >  php教程  >  ThinkPHP整合微信支付之发裂变红包

ThinkPHP整合微信支付之发裂变红包

WBOY
WBOYOriginal
2016-06-07 11:38:001094Durchsuche

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

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

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

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


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

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

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn