发放红包文档:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2

领取红包文档:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_10&index=3

class Index

{

public function index(Request $request=null)

{

//商户订单号(每个订单号必须唯一。取值范围:0~9,a~z,A~Z)

//组成: mch_id+yyyymmdd+10位一天内不能重复的数字。

$mch_billno = ''.rand(1000000000,9999999999);

$mch_id = '';//商户号

$wxappid = '';//小程序账号appid

$send_name = '';//商户名称

$re_openid = '';//用户openid

$total_amount = 30;//付款金额,单位分

$total_num = 1;//红包发放总人数

$wishing = '望天宇八方清似玉';//红包祝福语

$client_ip = '';//调用接口的服务器Ip地址,提前在小程序后台设置好

$act_name = '红包';//活动名称

$remark = '6666';//备注

$notify_way = 'MINI_PROGRAM_JSAPI';//通知用户形式JSAPI

$scene_id = 'PRODUCT_2';//发放红包使用场景,红包金额大于200时必传

$key = '';//商户号支付钥匙

$weixinpay = new WeixinPay($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key);

$res=$weixinpay->sendhb();

$result = [

'timeStamp'=> time()."",

'nonceStr'=>$this->createNoncestr(),

'package'=>urlencode($res['package']),

'appId'=>$wxappid,

];

$result['paySign'] = $this->getSign($result);

return json($result);

}

private function createNoncestr($length = 32) {

$chars = "abcdefghijklmnopqrstuvwxyz0123456789";

$str = "";

for ($i = 0; $i < $length; $i++) {

$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

} return $str;

}

private function getSign($Obj) {

foreach ($Obj as $k => $v) {

$Parameters[$k] = $v;

}

//签名步骤一:按字典序排序参数

ksort($Parameters);

$String = $this->formatBizQueryParaMap($Parameters, false);

//签名步骤二:在 string 后加入 KEY

$String = $String . "&key=" . '商户号支付钥匙';

//签名步骤三:MD5 加密

$String = md5($String);

//签名步骤四:所有字符转为大写--根据接口需要打开限制

$result_ = $String;

// $result_ = strtoupper($String);

return $result_;

}

/**

* 作用:格式化参数,签名过程需要使用

* @param $paraMap

* @param $urlencode

* @return string

*/

private function formatBizQueryParaMap($paraMap, $urlencode) {

$buff = "";

ksort($paraMap);

foreach ($paraMap as $k => $v) {

if ($urlencode) {

$v = urlencode($v);

}

$buff .= $k . "=" . $v . "&";

}

$reqPar = "";

if (strlen($buff) > 0) {

$reqPar = substr($buff, 0, strlen($buff) - 1);

}

return $reqPar;

}

}

class WeixinPay {

private $sendurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb';//发放红包接口

private $mch_billno;//商户订单号

private $mch_id;//商户号

private $wxappid;//公众账号appid

private $send_name;//商户名称

private $re_openid;//用户openid

private $total_amount;//付款金额,单位分

private $total_num;//红包发放总人数

private $wishing;//红包祝福语

private $client_ip;//Ip地址

private $act_name;//活动名称

private $remark;//备注

private $notify_way;//通知用户形式

private $scene_id;//发放红包使用场景,红包金额大于200时必传

private $key;//商户号支付钥匙

function __construct($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key) {

$this->mch_billno = $mch_billno;

$this->mch_id = $mch_id;

$this->wxappid = $wxappid;

$this->send_name = $send_name;

$this->re_openid = $re_openid;

$this->total_amount = $total_amount;

$this->total_num = $total_num;

$this->wishing = $wishing;

$this->client_ip = $client_ip;

$this->act_name = $act_name;

$this->remark = $remark;

$this->notify_way = $notify_way;

$this->scene_id = $scene_id;

$this->key = $key;

}

public function sendhb(){

//随机字符串

$nonce_str = $this->createNoncestr();

//商户订单号

$mch_billno = $this->mch_billno;

//商户号

$mch_id = $this->mch_id;

//公众账号appid

$wxappid = $this->wxappid;

//商户名称

$send_name = $this->send_name;

//用户openid

$re_openid = $this->re_openid;

//付款金额,单位分

$total_amount = $this->total_amount;

//红包发放总人数

$total_num = $this->total_num;

//红包祝福语

$wishing = $this->wishing;

//Ip地址

$client_ip = $this->client_ip;

//活动名称

$act_name = $this->act_name;

//备注

$remark = $this->remark;

//通知用户形式

$notify_way = $this->notify_way;

//发放红包使用场景,红包金额大于200时必传

$scene_id = $this->scene_id;

$parameters = array(

'nonce_str' => $nonce_str,

'mch_billno' => $mch_billno,

'mch_id' => $mch_id,

'wxappid' => $wxappid,

'send_name' => $send_name,

're_openid' => $re_openid,

'total_amount' => $total_amount,

'total_num' => $total_num,

'wishing' => $wishing,

'client_ip' => $client_ip,

'act_name' => $act_name,

'remark' => $remark,

'notify_way' => $notify_way,

'scene_id' => $scene_id

);

//生成签名,所有参数+key然后MD5

$parameters['sign'] = $this->getSign($parameters);

$xmlData = $this->arrayToXml($parameters);

// var_dump($xmlData);

$curlres = $this->postXmlCurl($xmlData, $this->sendurl);

// var_dump($curlres);

$res = $this->xmlToArray($curlres);

// var_dump($res);

return $res;

}

private function postXmlCurl($xml, $url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

//设置header

curl_setopt($ch, CURLOPT_HEADER, FALSE);

//要求结果为字符串且输出到屏幕上

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//post提交方式

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);

curl_setopt($ch, CURLOPT_TIMEOUT, 20);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

//证书的位置

// var_dump(curl_setopt($ch, CURLOPT_SSLCERT, __DIR__ . '/cert/apiclient_cert.pem'));die;

curl_setopt($ch, CURLOPT_SSLCERT,'D:/phpstudy_pro/WWW/weixinpem/apiclient_cert.pem');

//证书key的位置

curl_setopt($ch, CURLOPT_SSLKEY,'D:/phpstudy_pro/WWW/weixinpem/apiclient_key.pem');

//运行curl

$data = curl_exec($ch);

//返回结果

if ($data) {

curl_close($ch);

return $data;

} else {

$error = curl_errno($ch);

curl_close($ch);

throw new Exception("curl出错,错误码:$error");

}

}

//数组转换成xml

private function arrayToXml($arr) {

$xml = "";

foreach ($arr as $key => $val) {

if (is_array($val)) {

$xml .= "" . $this->arrayToXml($val) . "" . $key . ">";

} else {

$xml .= "" . $val . "" . $key . ">";

}

}

$xml .= "";

return $xml;

}

//xml转换成数组

private function xmlToArray($xml) {

//禁止引用外部xml实体

libxml_disable_entity_loader(true);

$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

$val = json_decode(json_encode($xmlstring), true);

return $val;

}

//作用:产生随机字符串,不长于32位

private function createNoncestr($length = 32) {

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

$str = "";

for ($i = 0; $i < $length; $i++) {

$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

}

return $str;

}

//作用:生成签名

private function getSign($Obj) {

foreach ($Obj as $k => $v) {

//不为空的参数才参与签名

if(!empty($v)){

$Parameters[$k] = $v;

}

}

//签名步骤一:按字典序排序参数

ksort($Parameters);

$String = $this->formatBizQueryParaMap($Parameters, false);

//签名步骤二:在string后加入KEY

$String = $String . "&key=" . $this->key;

//签名步骤三:MD5加密

$String = md5($String);

//签名步骤四:所有字符转为大写

$result = strtoupper($String);

return $result;

}

//作用:格式化参数,签名过程需要使用

private function formatBizQueryParaMap($paraMap, $urlencode) {

$buff = "";

ksort($paraMap);

foreach ($paraMap as $k => $v) {

if ($urlencode) {

$v = urlencode($v);

}

$buff .= $k . "=" . $v . "&";

}

$reqPar = '';

if (strlen($buff) > 0) {

$reqPar = substr($buff, 0, strlen($buff) - 1);

}

return $reqPar;

}

}

微信小程序红包 php,微信小程序发放红包+领取红包(PHP)相关推荐

  1. PHP抽奖小程序/微信红包封面抽奖小程序/抽奖小程序开源源码

    PHP抽奖小程序源码,微信红包封面抽奖小程序,全开源.带流量主(需要自行替换代码),独立后台,亲测完美可用. 下载 PHP抽奖小程序/微信红包封面抽奖小程序/抽奖小程序开源源码 亲测截图:

  2. 最新微信红包封面+抽奖小程序/小程序开源源码

    一.后台(PHP7.0+ MySQL5.7+) 上传后台源码到网站根目录,导入数据库文件:zzmaku_com.sql 修改数据库配置:/config/database.php 修改小程序配置:/co ...

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

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

  4. 微信小程序发放红包+领取红包(PHP)

    发放红包文档:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2 领取红包 ...

  5. 我的第一个小程序(Discuz! + 微信小程序)

    关于这个小程序 Github : https://github.com/DowneyL/di...' 这个小程序是公司针对一个自家产品的需求,而这个产品就是 Discuz (我知道没啥人用了,就不要吐 ...

  6. 微信小程序闭环处理 App -- 小程序 -- 企业微信 + 公众号

    App -- 小程序之间的交互 1)App 跳小程序指定页面 微信官方文档:接入指南 // 以下为H5部分的代码,客户端也需要根据文档做相应的改动 let bridge = window.WebVie ...

  7. 商城系统开发,使用微信服务号好?还是小程序?

    微信服务号和小程序都可以使用微信授权登录(获取用户微信头像.昵称.).微信支付.带参数二维码.微信红包等功能. 很多企业在开发商城时,会纠结使用微信服务号还是小程序? 了解服务号与小程序 服务号定位是 ...

  8. 小程序并未是微信的里程碑

    前言:智能手机时代,iphone手机引爆是iphone3于2008年发布,终于有了应用商店:android手机在中国引爆是小米手机,于2011年发布.而微信,诞生于2011年,离iphone发布已经过 ...

  9. 小程序技术始于微信?来看看移动端小程序技术的前世今生!

    本文由DCloud 公司创始人王安原创发布于CSDN,原题<小程序技术演进史>,即时通讯网收录时有改动,感谢原作者. 1.引言 微信的成功,并非特定于某个具体的功能,微信的成功实际上是一大 ...

  10. 【小程序】高校微信小程序应用大赛测试题

    报名地址 https://developers.weixin.qq.com/competition/index 测试题目 答案不保证对...但是可以通过测试  1. 以下那种小程序的内容是符合微信运营 ...

最新文章

  1. hdu 4876 ZCC loves cards(暴力)
  2. 关于线程池你不得不知道的一些设置
  3. 2.2.1生成数 自数
  4. C#用DataTable实现Group by数据统计
  5. 数据结构快速掌握和温习-面试神器
  6. 有奖投票丨HC2019开发者关注的TOP10问题你最想听哪个?
  7. 11. GD32F103C8T6 入门教程-外部中断
  8. PHP读取某站点的链接的函数
  9. 涤纶针织物用分散染料染色时,为什么小样与大样不符?
  10. springboot 注解_springBoot注解大全
  11. 2017第九届中国云计算大会精彩日程曝光
  12. 耒阳计算机学校,耒阳县系统分析师_科泰计算机学校
  13. linux常见的三种shell,几种常见的Shell
  14. TensorFlow by Google一个计算机视觉示例Machine Learning Foundations: Ep #2 - First steps in computer vision
  15. 药品缺陷检测中的机器视觉技术
  16. Debug调试工具使用
  17. uni-app 连接蓝牙打印机
  18. 杰奇 v1.7去限制版橙色模板小说源码
  19. Installing Kubernetes Using ‘kubeadm’
  20. python+appium+android模拟器自动化测试 源码

热门文章

  1. 使用Flash在线制作头像
  2. c语言tap位置什么意思,CTAP是什么意思
  3. matlab中 不是3和7倍数,从1到100的所有自然数中,不是3也不是7的倍数的数有多少个?...
  4. 简单实现微信抢红包的功能(Java)
  5. 人民币对美元汇率中间价报6.7774元 下调109个基点
  6. 学习C#的一点一滴(20)
  7. qt4.8.7 2016年4月18日 error reading collection file qthelpcollection cannot load sql
  8. C++ Beep()演奏简谱的改进以及实现背景音乐
  9. 【学习笔记】标签噪声下用于鲁棒性学习的对称交叉熵损失函数
  10. linux下磁盘坏道修复,linux磁盘坏道修复记录