概述
微信文档:
https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2
PHP调用接口需要证书(curl用到):
微信商户平台(pay.weixin.qq.com)-->账户中心-->账户设置-->API安全-->下载证书
需要支付密钥:
key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
设置允许服务IP白名单:
微信商户平台(pay.weixin.qq.com)-->产品中心-->现金红包-->产品设置-->设置服务器IP白名单
封装类:
/*
* 微信支付:小程序发放红包接口
*/
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);
//证书的位置
curl_setopt($ch, CURLOPT_SSLCERT, __DIR__ . '/cert/apiclient_cert.pem');
//证书key的位置
curl_setopt($ch, CURLOPT_SSLKEY, __DIR__ . '/cert/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;
}
}
调用示范:
$mch_billno = '1000000000201907031234567890';//商户订单号
$mch_id = '1000000000';//商户号
$wxappid = 'wx**************ff';//小程序账号appid
$send_name = '某某企业';//商户名称
$re_openid = 'oq*************************Is';//用户openid
$total_amount = 100;//付款金额,单位分
$total_num = 1;//红包发放总人数
$wishing = '恭喜';//红包祝福语
$client_ip = '120.0.1.120';//调用接口的服务器Ip地址
$act_name = '红包活动';//活动名称
$remark = '快来抢';//备注
$notify_way = '';//通知用户形式JSAPI
$scene_id = '';//发放红包使用场景,红包金额大于200时必传
$key = 'aany**************************nt';//商户号支付钥匙
$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();
最后
以上就是聪明黄豆为你收集整理的PHP小程序发放红包接口的全部内容,希望文章能够帮你解决PHP小程序发放红包接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复