微信公众号发送红包功能实现源码
最近项目要求推出活动,
//A推荐了B,1.如果B成功支付,2.如果B来源为雅思哥给A发66.66,如果不是雅思哥发200
//在活动期间,当A推荐了B时,A不会有任何的增加积分的操作,只可以领取红包(积分加0),活动结束之后领取红包功能下线后,再有推荐即可增加积分
//B: $_GET['userid']
//A: $_GET['tui_userid']
//开启红包
function pay($url,$obj){
$obj['nonce_str'] = $this->create_noncestr();
$stringA = $this->create_qianming($obj,false);
$stringSignTemp = $stringA."&key=XADMH3SJYHHAJKA24RRU6LFSCXGADMIN";
$sign = strtoupper(md5($stringSignTemp));
$obj['sign'] = $sign;
$postXml = $this->arrayToXml($obj);
$responseXml = $this->curl_post_ssl($url,$postXml);
return $responseXml;
}
function get_mode(){
//A推荐了B,1.如果B成功支付,2.如果B来源为雅思哥给A发66.66,如果不是雅思哥发200
//在活动期间,当A推荐了B时,A不会有任何的增加积分的操作,只可以领取红包(积分加0),活动结束之后,领取红包功能下线后,再有推荐即可增加积分
//B: $_GET['userid']
//A: $_GET['tui_userid']
//判断被推荐人是否成功支付
$succNums = $this->pay_db->select(array('userid'=>$_GET['userid'],'status'=>'succ'));
if($succNums > 0){
$source=$this->leads_db->get_one("userid={$_GET['userid']} AND type='main' AND add_time>0 AND userid>0",'userid,MIN(add_time) AS add_time,data_source','','userid','');
//如果成功支付判断是否雅思哥来源
if($source['data_source']==3618){
//如果是雅思哥来源 话术改变,金额改变6666
$money = 100; //最低1元,单位分
}else{
//如果不是雅思哥来源 话术改变,金额改变20000
$money = 200; //最低1元,单位分
}
$sender = "活动";
$obj2 = array();
$obj2['wxappid'] = "公众号的APPID";
$obj2['mch_id'] = "商户ID";
$obj2['mch_billno'] = "1451750402".date('YmdHis').rand(1000,9999);
$obj2['client_ip'] = $_SERVER['REMOTE_ADDR'];
$obj2['re_openid'] ="obOd2wUwTTj2T_oewrBn3lWO-jw4";
$obj2['total_amount'] = $money;
$obj2['min_value'] = $money;
$obj2['max_value'] = $money;
$obj2['total_num'] = 1;
$obj2['nick_name'] = $sender;
$obj2['send_name'] = $sender;
$obj2['wishing'] = "恭喜";
$obj2['act_name'] = "新年红包活动";
$obj2['remark'] = "新年红包活动";
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
$res = $this->pay($url,$obj2);
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($postObj),true);
if($val['result_code'] == 'SUCCESS'){
$qian = sprintf("%.2f",$money/100);
if($_GET['point_id']){
$this->coupons_db->insert(array('point_id'=>$_GET['point_id'],'userid'=>$_GET['tui_userid'],'money'=>$qian,'type'=>2,'status'=>2,'c_time'=>SYS_TIME,'mch_billno'=>$obj2['mch_billno'],'openid'=>$_GET['openid']));
$this->point_db->update(['note'=>"猪年红包奖励"],["id"=>$_GET['point_id']]);
}
}else{
file_put_contents(CACHE_PATH.'sendPacketErr.txt',var_export($val,true),FILE_APPEND);
}
}else{
showmessage('被推荐人未支付', '?m=referral&c=referral&a=referral_list',5000);
}
}
//生成签名,参数:生成签名的参数和是否编码
function create_qianming($arr,$urlencode){
$buff = "";
ksort($arr); //对传进来的数组参数里面的内容按照字母顺序排序,a在前面,z在最后(字典序)
foreach ($arr as $k=>$v) {
if(null!=$v && "null" != $v && "sign" != $k){
//签名不要转码
if ($urlencode){
$v = urlencode($v);
}
$buff.=$k."=".$v."&";
}
}
if (strlen($buff)>0) {
$reqPar = substr($buff,0,strlen($buff)-1); //去掉末尾符号“&”
}
return $reqPar;
}
//生成随机字符串,默认32位
function create_noncestr($length=32){
//创建随机字符
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for($i=0;$i<$length;$i++){
$str.=substr($chars, mt_rand(0,strlen($chars)-1),1);
}
return $str;
}
//数组转xml
function arrayToXml($arr){
$xml="" ;
foreach($arr as $k=>$v){
$xml.="<".$k.">".$v."".$k.">";
}
$xml.="";
return $xml;
}
//post请求网站,需要证书
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_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//cert 与 key 分别属于两个.pem文件
//请确保您的libcurl版本是否支持双向认证,版本高于7.20.1
curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.
'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.
'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.
'zhengshu'.DIRECTORY_SEPARATOR.'rootca.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);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}