/**
* 现金红包接口
* @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!"."<br>");
}elseif($this->parameters["nick_name"] == null){
throw new SDKRuntimeException("缺少发红包接口必填参数nick_name!"."<br>");
}elseif ($this->parameters["send_name"] == null ) {
throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."<br>");
}elseif ($this->parameters["total_amount"] == null) {
throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."<br>");
}elseif($this->parameters["min_value"] == null){
throw new SDKRuntimeException("缺少发红包接口必填参数min_value!"."<br>");
}elseif ($this->parameters["max_value"] == null ) {
throw new SDKRuntimeException("缺少发红包接口必填参数max_value!"."<br>");
}elseif ($this->parameters["total_num"] == null) {
throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."<br>");
}elseif ($this->parameters["wishing"] == null) {
throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."<br>");
}elseif ($this->parameters["act_name"] == null) {
throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."<br>");
}elseif ($this->parameters["remark"] == null) {
throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."<br>");
}
$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最下面就可以了!/**
* 初始化
*/
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);
}访问这个方法,微信就会发红包啦ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。