step:1 写一个红包工具类; 目录为__ROOT__\ThinkPHP\Library\Vendor\phpPay\WxPayPubHelper\WxPayPubHelper.php
/*
* 红包工具类
* */
class bonus_pub extends Wxpay_client_pub{
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!"."
");
}elseif($this->parameters["send_name"] == null){
throw new SDKRuntimeException("缺少红包接口必填参数send_name!"."
");
}elseif ($this->parameters["act_name"] == null ) {
throw new SDKRuntimeException("缺少红包接口必填参数act_name!"."
");
}elseif ($this->parameters["total_num"] == null) {
throw new SDKRuntimeException("缺少红包接口必填参数total_num!"."
");
}elseif ($this->parameters["total_amount"] == null) {
throw new SDKRuntimeException("缺少红包接口必填参数total_amount!"."
");
}elseif ( $this->parameters["re_openid"] == NULL){
throw new SDKRuntimeException("缺少红包接口必填参数 openid!"."
");
}elseif($this->parameters["remark"] == NULL){
throw new SDKRuntimeException("缺少红包接口必填参数 remark!"."
");
}elseif($this->parameters["wishing"] == NULL){
throw new SDKRuntimeException("缺少红包接口必填参数 wishing!"."
");
}
$this->parameters["appid"] = 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["sign"] = $this->getSign($this->parameters);//签名
return $this->arrayToXml($this->parameters);
}catch (SDKRuntimeException $e)
{
die($e->errorMessage());
}
}
/**
* 发红包 并且获取返回值
*/
function send_bouns()
{
$this->postXmlSSL();
$this->result = $this->xmlToArray($this->response);
return $this->result;
}
}
php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 15-9-8
* Time: 下午6:07
*/
header("Content-type:text/html;charset=utf-8");
class bonus extends \Think\Controller{
public function bonus(){
//构造方法
vendor ( ‘phpPay.WxPayPubHelper.WxPayPubHelper‘ );
}
/*openid 和红包金额*/
public function sendBonus($openid,$fee){
$fee = $fee*100;
$bouns = new \bonus_pub();
$mch_billno = WxPayConf_pub::MCHID.time().rand(0,100);
$bouns->setParameter("wxappid",WxPayConf_pub::APPID);//总金额
$bouns->setParameter("mch_billno","$mch_billno");//商户订单号
$bouns->setParameter("send_name","Gosstech");//红包发送者名称
$bouns->setParameter("re_openid","$openid");//用户openid
$bouns->setParameter("total_amount","$fee");//总金额
$bouns->setParameter("total_num","1");//红包发放总人数
$bouns->setParameter("wishing","好人一生平安,gosstech公益基金会");//附加数据 订单ID
$bouns->setParameter("act_name","公益红包");//活动名称
$bouns->setParameter("remark","一起做公益吧");//备注
$res = $bouns->send_bouns();
return $res;
}
}
php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 15-9-9
* Time: 下午4:03
*/
namespace Pay\Controller;
use Think\Controller;
class BonusController extends Controller {
public function sendBouns(){
vendor ( ‘phpPay.demo.bonus‘ );
$bonus = new \bonus();
$bonus->sendBonus(‘oc8rujitiX4ghHtvP57WvQQW4-UA‘,1);
}
}