发放红包文档: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
领取记录查询:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5
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 = "<xml>";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->arrayToXml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</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;
}
}