# 微信红包
[TOC]
[官方文档](https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5)
### 环境配置
发送红包需要证书,登录 [微信支付->API安全](https://pay.weixin.qq.com/index.php/core/cert/api_cert) 下载证书。
### 操作代码
```
$data = [
//商户名称
'send_name' => '后盾网HDCMS',
//付款金额,单位分,必须大于1元小于200元
'total_amount' => 100,
//红包发放总人数
'total_num' => '1',
//红包祝福语
'wishing' => '恭喜发财',
//活动名称
'act_name' => '开学红包',
//红包祝福语
'wishing' => '祝同学们学业有成,心想事成',
//备注
'remark' => '新班开课红包鼓励',
];
//用户openid
$user = WeChat::instance('oauth')->snsapiBase();
$data['re_openid'] = $user['openid'];
$res = WeChat::instance('cash')->sendRedPack($data);
if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS'
) {
echo '红包发送成功';
}
//$res为红包支付后微信返回的消息,内容如下
[
'return_code' => 'SUCCESS',
'return_msg' => '发放成功',
'result_code' => 'SUCCESS',
'err_code' => 'SUCCESS',
'err_code_des' => '发放成功',
'mch_billno' => '1494924430',
'mch_id' => '1283388801',
'wxappid' => 'wxc47243ed572e273d',
're_openid' => 'oGiQGuNCkJCTsc_t61XTrqbyY3rM',
'total_amount' => '100',
'send_listid' => '1000041701201705163000101612277',
]
```
> 必须为开通微信支付的服务号