namespace appapicontroller;
/*
* 微信支付:小程序红包接口
*/
use thinkController;
class Red extends Controller
{
private $sendurl = ‘https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb’;//发放红包接口
private $searchurl = ‘https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo’;//红包查询接口
private $mch_id = ‘你的商户号’;//商户号
private $wxappid = ‘你的公众账号appid’;//公众账号appid
private $send_name = ‘随便写’;//商户名称
private $total_num = 1;//红包发放总人数,默认都是1个
private $notify_way = ‘MINI_PROGRAM_JSAPI’;//通知用户形式,这里不用改
private $scene_id = ”;//发放红包使用场景,红包金额大于200时必传
private $key=’你的秘钥’;//商户号支付钥匙
/**
* 发放红包
* @param $mch_billno
* @param $re_openid
* @param $total_amount
* @param $wishing
* @param $act_name
* @param $remark
* @return mixed
* @throws Exception
*/
public function sendhb($mch_billno,$re_openid,$total_amount,$wishing,$act_name,$remark)
{
//随机字符串
$nonce_str = $this->createNoncestr();
$parameters = array(
‘nonce_str’ => $nonce_str,
‘mch_billno’ => $mch_billno,
‘mch_id’ => $this->mch_id,
‘wxappid’ => $this->wxappid,
‘send_name’ => $this->send_name,
‘re_openid’ => $re_openid,
‘total_amount’ => $total_amount,
‘total_num’ => $this->total_num,
‘wishing’ => $wishing,
‘act_name’ => $act_name,
‘remark’ => $remark,
‘notify_way’ => $this->notify_way,
‘scene_id’ => $this->scene_id
);
//生成签名,所有参数+key然后MD5
$parameters[‘sign’] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
$curlres = $this->postXmlCurl($xmlData, $this->sendurl);
$res = $this->xmlToArray($curlres);
return $res;
}
/**
* 根据订单号查询红包状态
* @param $mch_billno
* @param $mch_id
* @param $appid
* @return mixed
* @throws Exception
*/
public function searchhb($mch_billno,$mch_id,$appid)
{
//随机字符串
$nonce_str = $this->createNoncestr();
$parameters = array(
‘nonce_str’ => $nonce_str,
‘mch_billno’ => $mch_billno,
‘mch_id’ => $mch_id,
‘appid’ => $appid,
‘bill_type’ => ‘MCHT’
);
//生成签名,所有参数+key然后MD5
$parameters[‘sign’] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
$curlres = $this->postXmlCurl($xmlData, $this->searchurl);
$res = $this->xmlToArray($curlres);
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, APP_PATH . ‘/certRed/apiclient_cert.pem’); //你的证书所在路径
//证书key的位置
curl_setopt($ch, CURLOPT_SSLKEY, APP_PATH . ‘/certRed/apiclient_key.pem’); //你的证书key所在路径
//运行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位
public function createNoncestr($length = 32)
{
$chars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;
$str = “”;
for ($i = 0; $i
$str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
}
return $str;
}
//作用:生成签名
public 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;
}
}