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

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

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

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


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


step3:之前的微信支付的demo微信官方已经帮我们写好了WxPayHelper.php这个类库,我们可以很方便的调用就够了,而微信红包目前还没有官方demo,所以这里我们得自己在WxPayHelper.php文件下写自己的红包支付方法:
  1. /**
  2.  * 现金红包接口
  3.  * @author gaoyl101
  4.  */
  5. class Redpack_pub extends Wxpay_client_pub
  6. {
  7.     var $code;//code码,用以获取openid
  8.     var $openid;//用户的openid
  9.     
  10.     function __construct()
  11.     {
  12.         //设置接口链接
  13.         $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
  14.         //设置curl超时时间
  15.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  16.     }
  17.     /**
  18.      * 生成接口参数xml
  19.      */
  20.     function createXml()
  21.     {
  22.         try
  23.         {
  24.             //检测必填参数
  25.             if($this->parameters["mch_billno"] == null)
  26.             {
  27.                 throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."<br>");
  28.             }elseif($this->parameters["nick_name"] == null){
  29.                 throw new SDKRuntimeException("缺少发红包接口必填参数nick_name!"."<br>");
  30.             }elseif ($this->parameters["send_name"] == null ) {
  31.                 throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."<br>");
  32.             }elseif ($this->parameters["total_amount"] == null) {
  33.                 throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."<br>");
  34.             }elseif($this->parameters["min_value"] == null){
  35.                 throw new SDKRuntimeException("缺少发红包接口必填参数min_value!"."<br>");
  36.             }elseif ($this->parameters["max_value"] == null ) {
  37.                 throw new SDKRuntimeException("缺少发红包接口必填参数max_value!"."<br>");
  38.             }elseif ($this->parameters["total_num"] == null) {
  39.                 throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."<br>");
  40.             }elseif ($this->parameters["wishing"] == null) {
  41.                 throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."<br>");
  42.             }elseif ($this->parameters["act_name"] == null) {
  43.                 throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."<br>");
  44.             }elseif ($this->parameters["remark"] == null) {
  45.                 throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."<br>");
  46.             }
  47.             $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
  48.             $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  49.             $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
  50.             $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  51.             $this->parameters["re_openid"] = $this->openid;//用户openid
  52.             $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  53.             return  $this->arrayToXml($this->parameters);
  54.         }catch (SDKRuntimeException $e)
  55.         {
  56.             die($e->errorMessage());
  57.         }
  58.     }
  59.     
  60.     
  61.     function sendRedpack()
  62.     {
  63.         $this->postXmlSSL();
  64.         $this->result = $this->xmlToArray($this->response);
  65.         return $this->result;
  66.     }
  67.     
  68.     
  69.     
  70.     /**
  71.      *     作用:生成可以获得code的url
  72.      */
  73.     function createOauthUrlForCode($redirectUrl)
  74.     {
  75.         $urlObj["appid"] = WxPayConf_pub::APPID;
  76.         $urlObj["redirect_uri"] = "$redirectUrl";
  77.         $urlObj["response_type"] = "code";
  78.         $urlObj["scope"] = "snsapi_base";
  79.         $urlObj["state"] = "STATE"."#wechat_redirect";
  80.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  81.         return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  82.     }
  83.     
  84.     
  85.     
  86.     /**
  87.      *     作用:生成可以获得openid的url
  88.      */
  89.     function createOauthUrlForOpenid()
  90.     {
  91.         $urlObj["appid"] = WxPayConf_pub::APPID;
  92.         $urlObj["secret"] = WxPayConf_pub::APPSECRET;
  93.         $urlObj["code"] = $this->code;
  94.         $urlObj["grant_type"] = "authorization_code";
  95.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  96.         return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  97.     }
  98.     
  99.     /**
  100.      *     作用:通过curl向微信提交code,以获取openid
  101.      */
  102.     function getOpenid()
  103.     {
  104.         $url = $this->createOauthUrlForOpenid();
  105.         //初始化curl
  106.            $ch = curl_init();
  107.         //设置超时
  108.         curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
  109.         curl_setopt($ch, CURLOPT_URL, $url);
  110.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  111.         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  112.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  113.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  114.         //运行curl,结果以jason形式返回
  115.         $res = curl_exec($ch);
  116.         curl_close($ch);
  117.         //取出openid
  118.         $data = json_decode($res,true);
  119.         $this->openid = $data['openid'];
  120.         return $this->openid;
  121.     }
  122.     
  123.     /**
  124.      *     作用:设置code
  125.      */
  126.     function setCode($code_)
  127.     {
  128.         $this->code = $code_;
  129.     }
  130. }
复制代码
其实这里的代码我做的并不是很好,我并没有封装,因为做裂变红包也会用到相似的代码,这里做demo我就先不改了,有兴趣的朋友可以在此基础上继续晚上!上面的代码就是我们要用到的工具类,把他放在WxPayHelper.php最下面就可以了!

step4:创建控制器WxCashRedPackController


控制器中的代码:
1.引入WxPayHelper.php类库
  1. /**
  2.      * 初始化
  3.      */
  4.     public function _initialize()
  5.     {
  6.         //引入WxPayPubHelper
  7.         vendor('WxPayPubHelper.WxPayPubHelper');
  8.     }
复制代码
2.创建发送红包方法:sendRedpack,这个方法就是发送红包的具体功能代码!
  1. /**
  2.      * 发送红包
  3.      */
  4.     public function sendRedpack()
  5.     {
  6.         //调用请求接口基类
  7.         $Redpack = new \Redpack_pub();
  8.         
  9.         //=========步骤1:网页授权获取用户openid============
  10.         //通过code获得openid
  11.         if (!isset($_GET['code']))
  12.         {
  13.             //触发微信返回code码
  14.             $reduct_uri = WEB_HOST."/index.php/Home/WxCashRedPack/sendRedpack";
  15.             $url = $Redpack->createOauthUrlForCode($reduct_uri);
  16.             Header("Location: $url");
  17.         }else
  18.         {
  19.             //获取code码,以获取openid
  20.             $code = $_GET['code'];
  21.             $Redpack->setCode($code);
  22.             $openid = $Redpack->getOpenId();
  23.         }
  24.          
  25.         
  26.         
  27.         //商户订单号
  28.         $Redpack->setParameter('mch_billno', C('WxPayConf_pub.APPID')."static");
  29.         //提供方名称
  30.         $Redpack->setParameter('nick_name', "gaoyl101");
  31.         //商户名称
  32.         $Redpack->setParameter('send_name', "gaoyl101");
  33.         //用户openid
  34. //         $Redpack->setParameter('re_openid', $parameterValue);
  35.         //付款金额
  36.         $Redpack->setParameter('total_amount', 100);
  37.         //最小红包金额
  38.         $Redpack->setParameter('min_value', 100);
  39.         //最大红包金额
  40.         $Redpack->setParameter('max_value', 100);
  41.         //红包发放总人数
  42.         $Redpack->setParameter('total_num', 1);
  43.         //红包祝福语
  44.         $Redpack->setParameter('wishing', "现金红包教程祝大家写代码快乐");
  45.         //活动名称
  46.         $Redpack->setParameter('act_name', "现金红包教程");
  47.         //备注
  48.         $Redpack->setParameter('remark', "现金红包教程祝大家写代码快乐");
  49.         //以下是非必填项目
  50.         //子商户号  
  51. //         $Redpack->setParameter('sub_mch_id', $parameterValue);
  52. //        //商户logo的url
  53. //         $Redpack->setParameter('logo_imgurl', $parameterValue);
  54. //         //分享文案
  55. //         $Redpack->setParameter('share_content', $parameterValue);
  56. //         //分享链接
  57. //         $Redpack->setParameter('share_url', $parameterValue);
  58. //         //分享的图片
  59. //         $Redpack->setParameter('share_imgurl', $parameterValue);
  60.         
  61.         
  62.         
  63.         $result = $Redpack->sendRedpack();
  64.         
  65.         dump($result);
  66.     }
复制代码
访问这个方法,微信就会发红包啦
在这里我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/1330.html

猜你喜欢

转载自blog.csdn.net/lj81020302/article/details/51787696
今日推荐