微信发红包接口

微信

15-11-5 15:46:06

案例:

微信用户访问页面直接发送红包给用户  或者通过微信用户opendi 直接发送红包  


红包分2种   正常红包和裂变红包  金额不能小于1元   裂变红包 不能小于人均1元

代金卷要先到微信支付后台建立代金卷 充值后 取得代金卷批次号才能使用

证书  要去官方下载证书才可以  注意证书位置


$type=rand(1,3);  // 1是普通红包 2是裂变红包 3代金卷

$hongbao =new hongbao(APPID,"商户号","密钥",$openid,$type);

if($type==1){
    $data['send_name']='农品购';
    $data['total_amount']=100; //付款金额单位为
    $data['total_num']=1; //发红包人数
    $data['wishing']="祝福语-的说明";  //祝福语
    $data['client_ip']=$_SERVER['REMOTE_ADDR']; //调用接口的机器Ip地址
    $data['act_name']='这是一个正常的红包'; //活动名称
    $data['remark']="--XXXxxxxx备注"; //备注
}elseif($type==2){
    $data['send_name']='农品购';
    $data['total_amount']=300; //付款金额单位为
    $data['total_num']=3; //发红包人数
    $data['wishing']="祝福语-的说明";  //祝福语
    $data['client_ip']=$_SERVER['REMOTE_ADDR']; //调用接口的机器Ip地址
    $data['act_name']='这是一个分裂的红包 '; //活动名称
    $data['remark']="---XXXxxxxx备注!"; //备注
}elseif($type==3){
    $data['coupon_stock_id']='260611';  //代金卷批次
    $data['openid_count']=1; 
}


$hongbao->setdata($data);
$re=$hongbao->post();
$data=$hongbao->get_re_xml();


if($re['result_code']=='SUCCESS'){
echo '<h1>领取成功!</h1>';
}else{
echo '<h1>领取失败.'.$re['err_code_des']."</h1>";
}

----------------------------------------------------------------------------------------------------

红包类

<?php


class hongbao 
{

//公共变量
private $appid;    //公众号id
private $sign='';    //签名
private $nonce_str;  //随机字符串
private $mch_id;     //商户号
private $mch_billno; //商户订单id
private $my;         //支付秘钥
private $wx_id;      //微信id
public $type=1;      // 1是普通红包 2是裂变红包 3代金
private $data=array();// 参数数组
private $xml;         // xml
private $redata;      //返回数据
private $curl;       //提交数据接口
private $cert_pem;    // 证书1
private $key_pem;     //证书2
function __construct($appid,$mch_id,$my,$wx_id,$type=1,$path='.') { //appid 商户号 支付密钥 微信id 证书位置
       $this->appid=$appid; 
  $this->nonce_str=md5(rand(100000,99999)); 
  $this->mch_id=$mch_id; 
  $this->my=$my; 
  $this->wx_id=$wx_id; 
  $this->mch_billno=$mch_id.date("Ymd").rand(1111111111,9999999999);  //商户订单号
  $this->type=$type;
  if($path=='.'){
  $this->cert_pem=getcwd().'/apiclient_cert.pem';
  $this->key_pem=getcwd().'/apiclient_key.pem'; 
  }
    }


public function setdata($udata,$p=0)  //设置数据
{
$this->diy($udata);
$this->sign();
$this->xml();
if($p)var_dump(htmlspecialchars($this->xml));

}

//返回的xml
public function get_re_xml()  
{
return $this->redata;

}

//发送数据
public function post() 
{
$this->set_url();  //设置url
$this->curl_post_ssl($this->curl,$this->xml);
return $this->xml2arr($this->redata);
}

//根据类型返回不同接口
private function set_url() 
{
if($this->type==1){
$this->curl= 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
}elseif($this->type==2){
   $this->curl='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
}elseif($this->type==3){
$this->curl='https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon';
}else{
die('红包类型错误');
}
}

//根据类型返回不同内容
private function xml() 
{
$this->xml='<xml>';
$this->xml.="<sign><![CDATA[".$this->sign."]]></sign>";
foreach ($this->data as $k => $v) { 
$this->xml.="<$k><![CDATA[$v]]></$k>";

$this->xml.='</xml>';
}

//拼接签名
private function sign($data)
{
ksort($this->data); //排序
$strA='';
foreach ($this->data as $k => $v) { 
$strA.= $k.'='.$v."&"; 

$stringSignTemp=$strA."key=".$this->my;
$this->sign=strtoupper(md5($stringSignTemp));
}

//提交
private function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

//以下两种方式需选择一种

//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,$this->cert_pem);
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,$this->key_pem);

//第二种方式,两个文件合成一个.pem文件
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
 
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
 
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
$this->redata=$data;
}
else { 
$error = curl_errno($ch);
echo "通信失败, errorCode:$error\n请联系管理员"; 
curl_close($ch);
$this->redata=false;
}
}
//组合字段
private function diy($udata){


if($this->type==1){

$data['mch_billno']=$this->mch_billno;
$data['mch_id']=$this->mch_id;
$data['wxappid']=$this->appid;
$data['send_name']=$udata['send_name']; //发送名称
$data['re_openid']=$this->wx_id;   //收红包的openid
$data['total_amount']=$udata['total_amount'];  //付款金额单位为
$data['total_num']=$udata['total_num'];        //发红包人数
$data['wishing']=$udata['wishing']; //祝福语
$data['client_ip']=$udata['client_ip'];//调用接口的机器Ip地址
$data['act_name']=$udata['act_name'];  //活动名称
$data['remark']=$udata['remark'];//备注
$data['nonce_str']=$this->nonce_str;

}elseif($this->type==2){
$data['mch_billno']=$this->mch_billno;
$data['mch_id']=$this->mch_id;
$data['wxappid']=$this->appid;
$data['send_name']=$udata['send_name']; //发送名称
$data['re_openid']=$this->wx_id;   //收红包的openid
$data['total_amount']=$udata['total_amount'];
$data['total_num']=$udata['total_num'];
$data['wishing']=$udata['wishing']; //祝福语
$data['act_name']=$udata['act_name'];  //活动名称
$data['remark']=$udata['remark'];//备注
$data['nonce_str']=$this->nonce_str;
$data['amt_type']="ALL_RAND";
}elseif($this->type==3){
$data['appid']=$this->appid;
$data['coupon_stock_id']=$udata['coupon_stock_id']; 
$data['mch_id']=$this->mch_id;
$data['nonce_str']=$this->nonce_str;
$data['openid']=$this->wx_id;
$data['openid_count']=$udata['openid_count'];
$data['partner_trade_no']=$this->mch_billno;
}
$this->data=$data;

}

function xml2arr($str){
$data=array();
$xml=simplexml_load_string($str);
foreach($xml->children() as $child)
{
$data[$child->getName()]= (string)$child ;
}
return $data;
}



}