微信现金红包接口实现红包发放:

一:流程:【

流程:微信用户访问红包活动页面--》后端判断是否是微信访问的

【否:提示用微信打开连接,是:提示用户是否授权允许,获取其的用户信息【openID等信息】】--》

进入红包活动页面---》用户点击领取红包【判断是否领取过红包】是:【提示已领取过红包】否

--》后端程序调用接口发放红包

--》微信用户在微信中收到红包

--》打开红包

--》红包金额会添加到钱包零钱里

--》完成红包发放。

二:要实现微信现金红包接口实现红包发放,首先要符合以下条件:

1.用于发放红包的微信公众号要是服务类型

2.登录微信公众平台官网后,在公众平台后台管理页面 - 开发者中心页,

点击“修改配置”按钮,填写服务器地址(URL)、Token和EncodingAESKey,

其中URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,

用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)。

EncodingAESKey由开发者手动填写或随机生成,将用作消息体加解密密钥。

3.获取access_token:公众号可以使用AppID和AppSecret调用本接口来获取access_token。

【AppID和AppSecret可在微信公众平台官网-开发者中心页中获得(需要已经成为开发者,且帐号没有异常状态)。】

4.微信公众号要开通 “网页授权接口” 用户获取用户基本信息【特别是openID ,发红包时用到】

5.微信公众号的 “微信支付“  中的  ”商户号” 要开通微信支付【发红包的金额是该支付账户扣款】,开通“现金红包”接口【调用该接口发放红包】。

6. 登陆 ”商户号”【微信公众号分配的商户号。第5 中有说明】 在 “API安全” 中 下载PHP版的 证书 【.pem格式】

以上如描述不清楚,请查看 微信开发者文档 里面有详细的秒杀。

部分代码如下【其余的请查看附件】:

<?php
namespace RedClient\Controller;
use Think\Controller;
use RedClient\Redpack\WeiXinInfo;
use RedClient\Redpack\Oauth;
use RedClient\Redpack\SendRedPack;
use RedClient\Redpack\CreateRedPack;
class IndexController extends Controller {public function index(){$this->isWeixin();//是否是微信打开if($this->access){$this->display();}else{$class=new Oauth();$class->index('http://www.myweb.com/index.php/Index/oauth');}}//获取用户信息 openIDpublic function oauth(){$code=isset($_GET['code'])?strip_tags(trim($_GET['code'])):'';$state=isset($_GET['state'])?strip_tags(trim($_GET['state'])):'';$class=new Oauth();$userInfo=$class->getCode($code,$state);//获取用户信息if(!empty($userInfo['data'])){//$model=M('wxuser');//$result=$model->where('openid = "'.$userInfo['data']->openid.'"')->field('openid')->select();if(empty($result)){$userInfo['data']=$this->object2array($userInfo['data']);$model->data($userInfo['data'])->add();}}$userInfo['data']=$this->object2array($userInfo['data']);//创建红包$class=new CreateRedPack();$red=$class->redval();//发红包$class=new SendRedPack();$configs=array('send_name'=>'红包发送者名称',//红包发送者名称're_openid'=>$userInfo['data']['openid'],//接受红包的用户,用户在wxappid下的openid'total_amount'=>$red,//付款金额,单位分'total_num'=>'1',//红包发放总人数'wishing'=>'红包祝福语',//红包祝福语'client_ip'=>$_SERVER['SERVER_ADDR'],//调用接口的机器Ip地址'act_name'=>'活动名称',//活动名称'remark'=>'备注信息',// 备注信息);$class->setFields($configs);$result=$class->requestXml();//微信返回信息处理if(strtoupper($result['return_code'])=='SUCCESS'){if(strtoupper($result['result_code'])=='SUCCESS'){//红包发送成功!$datas['flag']=1;echo $result['err_code_des'];}else{//红包发送失败$datas['flag']=0;$datas['msg']=$result['err_code_des'];echo $result['err_code_des'];}}else{//红包发送失败$datas['flag']=0;$datas['msg']=$result['err_code_des'];echo $result['err_code_des'];}}public function isWeixin(){if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { $this->access=true; } return false; }//类转换成数组public function object2array($object) {if (is_object($object)) {foreach ($object as $key => $value) {$array[$key] = $value;}}else {$array = $object;}return $array;}}?>
<?php
namespace RedClient\Redpack;
/**简单红包算法类
**/
class CreateRedPack{private $rid=0;//当前红包随机数private $rand_arr=array();//35%是1.0到1.1 红包随机数private $rand_arr1=array();//35%是1.1到1.2 红包随机数private $rand_arr2=array();//30%是1.2到1.95 红包随机数private $red=0;//红包金额private $simplered=0;private $red_config=array(array('min'=>1.0,'max'=>1.1),array('min'=>1.1,'max'=>1.2),array('min'=>1.2,'max'=>1.95));public function __construct(){$this->rid=mt_rand(1,10000);//当前红包随机数$this->rand_arr=range(1,3500);//35%是1.0到1.1$this->rand_arr1=range(3501,7000);//35%是1.1到1.2$this->rand_arr2=range(7001,10000);//30%是1.2到1.95$this->simplered=666;}public function redval(){$maxrp=$this->maxred();//随机最大红包金额if($maxrp!=$this->simplered){if(in_array($this->rid,$this->rand_arr)){$red_val=$this->red_config[0];$min=$red_val['min']*100;$max=$red_val['max']*100;$this->red=mt_rand($min,$max);$this->red=$this->red/100;}elseif(in_array($this->rid,$this->rand_arr1)){$red_val=$this->red_config[1];$min=$red_val['min']*100;$max=$red_val['max']*100;$red=mt_rand($min,$max);$this->red=$this->red/100;}elseif(in_array($this->rid,$this->rand_arr2)){$red_val=$this->red_config[2];$min=$red_val['min']*100;$max=$red_val['max']*100;$this->red=mt_rand($min,$max);$this->red=$this->red/100;}}else{$this->red=$maxrp;}return $this->red?$this->red:1;}protected function maxred(){$rid=mt_rand(1,100000);$rid1=mt_rand(1,100000);$red=0;$dff=$rid-$rid1;if($dff > 0){if(($rid1%$rid1)==$this->simplered){$red=$this->simplered;}}else{if(($rid1%$rid)==$this->simplered){$red=$this->simplered;}}return $red;}}?>
<?php
namespace RedClient\Redpack;
/*********先在公共平台配置授权的域名;然后才能通过,网页授权方式获取微信用户基本信息网页授权流程分为四步: 1.引导用户进入授权页面同意授权,获取code 2.通过code换取网页授权access_token(与基础支持中的access_token不同) 3.如果需要,开发者可以刷新网页授权access_token,避免过期 4.通过网页授权access_token和openid获取用户基本信息 **********/
class Oauth{protected $appid='';protected $redirect_uri='';protected $state='';//mt_rand(100,999);protected $appsecret='';protected $data=array('flag'=>0,'msg'=>'','data'=>'');public function __construct(){$this->appid='appid';//$this->appsecret='secret';//;$this->state=mt_rand(100,999);}//引导用户访问链接处理函数  public function index($redirect_uri){$this->redirect_uri=urlencode($redirect_uri);//微信自动跳转到$redirect_uriheader('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$this->redirect_uri.'/oauth.php&response_type=code&scope=snsapi_userinfo&state='.$this->state.'&connect_redirect=1#wechat_redirect');}public function getCode($code='',$state='',$token='',$webToken=''){$data=array('errorno'=>'','errormsg'=>'','data'=>'');if(empty($code)){$this->data['flag']=0;$this->data['msg']='授权失败!';}else{  /* 1获取webtoken2获取reflash_token3获取用户信息 */$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code';$token = json_decode(file_get_contents($token_url));if (isset($token->errcode)) {$data['errorno']=$token->errcode;$data['errormsg']=$token->errmsg;}else{$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;//转成对象$accessToken = json_decode(file_get_contents($access_token_url));if (isset($accessToken->errcode)) {$data['errorno']=$token->errcode;$data['errormsg']=$token->errmsg;}else{$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$accessToken->access_token.'&openid='.$accessToken->openid.'&lang=zh_CN';//转成对象$userInfo = json_decode(file_get_contents($user_info_url));if (isset($userInfo->errcode)) {$data['errorno']=$token->errcode;$data['errormsg']=$token->errmsg;}else{$data['data']=$userInfo;}}}}return $data;}}?>
<?php
/******用于企业向微信用户个人发现金红包目前支持向指定微信用户的openid发放指定金额红包。(获取openid参见微信公众平台开发者文档:网页授权获取用户基本信息)****/
namespace RedClient\Redpack;class SendRedPack{private $config=array('nonce_str'=>'',//随机字符串,不长于32位'sign'=>'',//签名'mch_billno'=>'',//商户订单号'mch_id'=>'1111sdfsafsaddf',//微信支付分配的商户号'wxappid'=>'sddafdsadfdsafdsdd',//微信分配的公众账号ID'send_name'=>'',//红包发送者名称're_openid'=>'',//接受红包的用户,用户在wxappid下的openid'total_amount'=>'',//付款金额,单位分'total_num'=>'',//红包发放总人数'wishing'=>'',//红包祝福语'client_ip'=>'',//调用接口的机器Ip地址'act_name'=>'',//活动名称'remark'=>'',//  备注信息);protected $key='';protected $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';protected $requestXml='';//设置必要参数public function setFields($conf=array()){foreach($conf as $k=>$v){if(isset($this->config[$k]) && !empty($v)){$this->config[$k]=$v;}}$this->config['mch_billno']=$this->billno();$this->config['nonce_str']=$this->createNonceStr();$this->createSign($conf);$this->createXml();//echo $this->requestXml;}protected function billno(){return $this->config['mch_id'].time().md5(mt_rand(1000,9999));}/* //检查必要参数是否为空!public function checkConfig(){$flag=true;foreach($this->config as $k=>$v){if(empty($v)){$flag=false;break;}}} *///随机字符串,不长于32位public function createNonceStr( $len=32 ){$strs = "abcdefghijklmnopqrstuvwxyz0123456789";$str ="";$len=$len<=32?$len:32;for ( $i = 0; $i < $len; $i++ ){$str.= substr($strs, mt_rand(0, strlen($strs)-1), 1);}return $str;}//格式化参数public function formatParam($config=array(),$flag=1){$format='';if(!empty($config)){ksort($config);foreach($config as $k=>$v){if($flag){$v=urlencode($v);}if($flag==0 && strtolower($k)=='sign'){continue;}$format.=$k.'='.$v.'&';}$format=trim($format,'&');}return $format;}//创建SIGNATURE   protected function createSign($config){$format=$this->formatParam($config,0);$format.='&key='.$this->key;$signature=strtoupper(md5($format));$this->config['sign']=$signature;return true;//$signature;}//创建xml格式数据protected function createXml(){$xml='<xml>';foreach($this->config as $k=>$v){if(!empty($v)){$xml.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';}}$xml.='</xml>';$this->requestXml=$xml;}//XML格式数据转换为数组public function createArray($xml=''){$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);return $arr;}//发送红包public function requestXml($timeout=30,$header=array()){$ch = curl_init();//超时时间curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch,CURLOPT_URL,$this->url);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//默认格式为PEM,可以注释curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).'/apiclient_cert.pem');//pem证书地址//默认格式为PEM,可以注释curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).'/apiclient_key.pem');//key证书地址curl_setopt($ch,CURLOPT_CAINFO,'PEM');curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'/rootca.pem');//CA证书地址//两个文件合成一个.pem文件//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');if( count($header) >= 1 ){curl_setopt($ch, CURLOPT_HTTPHEADER, $header);}curl_setopt($ch,CURLOPT_POST, 1);curl_setopt($ch,CURLOPT_POSTFIELDS,$this->requestXml);$data = curl_exec($ch);var_dump($data);if($data){curl_close($ch);var_dump($data);return $data;}else {$error = curl_errno($ch);echo 'ERROR:'.$error;curl_close($ch);return false;}}//返回非空值,非NULLpublic function notEmpty($val=''){$return='';if(null !=$val && strlen($val) != 0){$return=$val;}return $return?$return:false;}}?>
<?php
namespace RedClient\Redpack;
class WeiXinInfo{private $AppID='appid';private $AppSecret='secret';private $grant_type='client_credential';private $url='https://api.weixin.qq.com/cgi-bin/token';public function __construct(){//$arr=array('access_token'=>'1','expires'=>'7200');}//设置获取ACCESSTOKEN 的URLpublic function setUrl(){return $this->url=$this->url.'?grant_type='.$this->grant_type.'&appid='.$this->AppID.'&secret='.$this->AppSecret;}//获取APPID,SECRETpublic function getAppid(){$sql='select appid,secret from ly_appid where flag=1';$model=M();$result=$model->query($sql);if(!empty($result)){$this->AppID=$result[0]['appid'];$this->AppSecret=$result[0]['secret'];return $result[0];}}public function object2array($object) {if (is_object($object)) {foreach ($object as $key => $value) {$array[$key] = $value;}}else {$array = $object;}return $array;}//检验URL有效public function checkUrl(){$signature=isset($_GET['signature'])?strip_tags(trim($_GET['signature'])):'';$timestamp=isset($_GET['timestamp'])?strip_tags(trim($_GET['timestamp'])):'';$nonce=isset($_GET['nonce'])?strip_tags(trim($_GET['nonce'])):'';$echostr=isset($_GET['echostr'])?$_GET['echostr']:'';if(!empty($signature) && !empty($timestamp) && !empty($nonce)){if($this->checkSign($signature,$timestamp,$nonce)){echo $echostr;return true;}else{return false;}}}    //验证SIGNATURE是否有效private function checkSign($sign,$timestamp,$nonce){$token=$this->getAccessToken();//$token=$this->object2array($token);$sign_arr=array($token,$timstamp,$nonce);sort($sign_arr,SORT_STRING);$signStr=implode($sign_arr);$signStr=sha1($signStr);if(strtoupper($signStr)==strtoupper($sign)){return true;}return false;}//通过微信接口获取微信Access_Token  public function getAccessToken(){$token='';$this->setUrl();$check=$this->checkToken();if(session('?'.md5('getaccesstoken')) && $check){$tokens=session(md5('getaccesstoken'));$token=$tokens->access_token;}else{$result=file_get_contents($this->url);$result=json_decode($result);$token=$result->access_token;$result->expires_in=$result->expires_in+time();session(md5('getaccesstoken'),$result);}return $token;}//检查微信ACCESS_TOKEN是否有效public function checkToken(){if(session('?'.md5('getaccesstoken')) && session(md5('getaccesstoken'))){$time=time();$token=session(md5('getaccesstoken'));if($token->expires_in-$time > 30){return true;}else{session(md5('getaccesstoken'),null);}}return false;}//保存微信ACCESSTOKEN到数据库   public function saveAccessToken(){$token=$this->getAccessToken();$sql='select `id`,`rate`,token,ex_time,createtime from ly_token where token="'.$token.'" where appid="'.$this->AppID.'" AND secret="'.$this->AppSecret.'"';$model=M();$result=$model->query($sql);if(!empty($result)){$time=time();$expires=$time-$result[0]['createtime'];if($result[0]['ex_time']-$expires > 0){return $result[0]['token'];                      }else{$token=$this->getAccessToken();if(!empty($token)){$token=json_decode($token);if(!isset($token['errcode']) or !$token['errcode']){if(isset($token['access_token']) && $token['access_token']){$data['access_token']=$token['access_token'];$data['createtime']=$time;$data['ex_time']=$token['expires_in'];$data['rate']=$result[0]['rate']+1;//$sql='update ly_token set token="'.$token['access_token'].'" ,createtime='.$time.',ex_time='.$token['expires_in'].' where `id`='.$result[0]['id'];$model=M('token');$update=$model->where('`id`='.$result[0]['id'])->save($data);if($update){return $token['access_token'];}}}else{//微信返回的错误信息$data['errcode']=$token['errcode'];$data['errmsg']=$token['errmsg'];$data['appid']=$this->AppID;$data['secret']=$this->AppSecret;$data['createtime']=time();$data['rate']=$result[0]['rate']+1;$ein=$model->where('`id`='.$result[0]['id'])->save($data);if($ein){return false;}}}}}else{//新插入数据库保存$token=$this->getAccessToken();$model=M('token');if(!empty($token)){$data=array();$token=json_decode($token);if(!isset($token['errcode']) or !$token['errcode']){if(isset($token['access_token']) && $token['access_token']){$data['access_token']=$token['access_token'];$data['createtime']=$time;$data['ex_time']=$token['expires_in'];//$sql='insert into ly_token() vlaues()';$data['rate']=1;$in=$model->data($data)->add();if($ein){$token['access_token'];}}}else{//微信返回的错误信息$data['errcode']=$token['errcode'];$data['errmsg']=$token['errmsg'];$data['appid']=$this->AppID;$data['secret']=$this->AppSecret;$data['createtime']=time();$data['rate']=1;$ein=$model->data($data)->add();if($ein){return false;}}}}return false;}  }?>

转载于:https://blog.51cto.com/yipianlvye/1915337

微信现金红包接口实现红包发放相关推荐

  1. java 红包接口开发_java调用微信现金红包接口的心得与体会总结

    这几天看了下之前写的有关微信支付的博客,看的人还是挺多的,看了下留言不知道是因为博客写的不够细还是什么情况,大多都找我要源码,我觉得吧程序员还是需要有这么一个思考的过程,因此没直接给源码,俗话说&qu ...

  2. java调用微信红包接口_java调用微信现金红包接口的心得与体会

    这几天看了下以前写的有关微信支付的博客,看的人仍是挺多的,看了下留言不知道是由于博客写的不够细仍是什么状况,大多都找我要源码,我以为吧程序员仍是须要有这么一个思考的过程,所以没直接给源码,俗话说&qu ...

  3. 微信红包接口 java_java调用微信现金红包接口的心得与体会总结

    这几天看了下之前写的有关微信支付的博客,看的人还是挺多的,看了下留言不知道是因为博客写的不够细还是什么情况,大多都找我要源码,我觉得吧程序员还是需要有这么一个思考的过程,因此没直接给源码,俗话说&qu ...

  4. php微信商务平台 红包调用,微信平台红包接口怎么调用?微信支付商户平台红包发放接口调用图文教程[多图]...

    微信平台红包接口怎么调用?估计很多商家都还不太会操作吧?别着急,下面是友情小编搜集相关资料整理出来的微信支付商户平台红包发放接口调用图文教程,希望可以帮到大家,现在就跟随小编一起看看吧!!! 首先,商 ...

  5. python 微信支付 小程序红包 发放红包接口

    python 微信支付 小程序红包 发放红包接口 文章目录 python 微信支付 小程序红包 发放红包接口 前言 一.官方文档 二.使用步骤 1.引入,直接复制粘贴以下代码,新建wx_pay.py ...

  6. 微信红包发放接口服务器签名失败,微信现金红包接口开发注意的事项

    微信支付现金红包接口正式开放,只需开通微信支付,即可接入现金红包.通过现金红包接口,公众号开发者可以策划相关运营活动,向用户发放微信支付现金红包,更好的达到品牌推广及回馈用户的效果.具体能力如下: 商 ...

  7. 微信支付现金红包接口应用实例代码说明和DEMO详解,适合用来做微信红包营销活动、吸粉利器...

    本文详细介绍微信红包开发的接口,商户调用接口时,通过指定发送对象以及发送金额的方式发放红包,领取到红包后,用户的资金直接进入微信零钱.后面带有具体调用php实例 总结一下:需要注意的是PEM秘钥文件要 ...

  8. 微信公众号可通过现金红包接口发放微信支付现金红包(附开发教程)

    标签: 农历新年将至,支付宝红包打了一仗,微信在朋友圈屏蔽了它的分享,但单防守还不行,进攻才是最好的防守.昨日,微信支付现金红包接口正式开放,只需开通微信支付,即可接入现金红包.微信公众号也可以发放现 ...

  9. H5活动抽现金红包,微信第三方APi接口代发红包

    最近需要做一个能抽奖发红包的H5活动,用在微信上,本身微信支付自带了发红包功能,但是奈何开通此功能需要微信 支付入驻满90天并且有1个月以上的交易流水,所以就放弃了,然后采用第三方接口实现红包,第三方 ...

最新文章

  1. python3.0内建函数大全_python3内置函数大全
  2. 《JavaScript高级程序设计》阅读笔记(三):ECMAScript中的引用类型
  3. 监听js变量的变化_Node.js从零开始——事件、系统和流
  4. 用户(三次)登录--作业小编完成
  5. 基于51的串行通讯原理及协议详解(uart)
  6. 栈应用:实现二进制转八进制、十进制、十六进制
  7. 刚买的iPad可获1100元退款
  8. 断网问题解决【值得一记】
  9. 如何在FL Studio中对整首歌曲音量进行调整
  10. Geoserver+postSQL+openlayer实现路径规划
  11. SLAM方向国内有哪些优秀的公司?
  12. 关于表格冻结行和列的方法
  13. [日推荐]『小恩故事』育儿助手!
  14. .NET 开发从入门到精通
  15. ext4文件系统布局
  16. python的分隔符_python分隔符
  17. VMware虚拟机下载安装教程
  18. 湖南省第六届大学生计算机程序设计竞赛---弟弟的作业
  19. 敏捷其实很简单(9)Scrum Master的七种武器之离别钩霸王枪箱子
  20. matlab nctool使用,感知器和BP网络设计及应用技术总结.doc

热门文章

  1. vue如何获取年月日_vue 学习笔记第二弹
  2. win7怎么查看电脑配置_电脑死机是什么原因?出现问题你会怎么办?
  3. 创建python2与python3虚拟环境失败的原因
  4. php fopen 错误,php fopen函数失败怎么办
  5. Codeforces 797C Minimal string【贪心】
  6. 统计“3_人民日报语料”文本中的字符数和词数,把文件分别保存为 ansi, UTF8,UTF16,unicode 格式
  7. RAID原理分析总结
  8. PhpStorm代码换行设置
  9. Oracle11g服务详细介绍及哪些服务是必须开启的?
  10. Java中Socket通信-客户端向服务端发送照片