<?php
class redpacketControllers
{
var $parameters; //cft 参数
function index($openid)
{
$app_mchid = 'xxxxxxxxxx'; //商户号
$app_id = 'xxxxxxxxxxxxxxxxxx'; //公众账号id
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //公众号secret
$client_ip = $_SERVER['SERVER_ADDR']; //IP
$nonce_str = $this->great_rand(); //随机数
$mch_billno = $app_mchid . date('YmdHis') . rand(1000, 9999); //订单号
$money = 100;//金额以分为单位 100==1元,最低限制为1元以上
$this->setParameter("nonce_str", $nonce_str); //随机字符串,丌长于 32 位
$this->setParameter("mch_billno", $mch_billno); //订单号
$this->setParameter("mch_id", $app_mchid); //商户号
$this->setParameter("wxappid", $app_id);
$this->setParameter("nick_name", '服务号'); //提供方名称
$this->setParameter("send_name", '服务号'); //红包发送者名称
$this->setParameter("re_openid", $openid); //相对于医脉互通的openid
$this->setParameter("total_amount", $money); //付款金额,单位分
$this->setParameter("min_value", $money); //最小红包金额,单位分
$this->setParameter("max_value", $money); //最大红包金额,单位分
$this->setParameter("total_num", 1); //红包収放总人数
$this->setParameter("wishing", '红包祝福语'); //红包祝福诧
$this->setParameter("client_ip", $client_ip); //调用接口的机器 Ip 地址
$this->setParameter("act_name", '红包活动名称'); //活劢名称
$this->setParameter("remark", '快来领备注信息'); //备注信息
$postXml = $this->create_hongbao_xml();
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$this->Log->output('req', "请求报文" . $postXml);
$responseXml = $this->curl_post_ssl($url, $postXml);//请求微信
$this->Log->output('res', "应答报文" . $responseXml);
$responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);//解析应答报文
$return_code = $responseObj->return_code;
if ($return_code == 'SUCCESS') {
return 1;
}
}
function setParameter($parameter, $parameterValue)
{
$this->parameters[$this::trimString($parameter)] = $this::trimString($parameterValue);
}
function getParameter($parameter)
{
return $this->parameters[$parameter];
}
/**
* @return string
* 获取sign拼接请求报文
*/
function create_hongbao_xml()
{
try {
$this->setParameter('sign', $this->get_sign());
return $this->arrayToXml($this->parameters);
} catch (Exception $e) {
//日志
}
}
function check_sign_parameters()
{
if ($this->parameters["nonce_str"] == null ||
$this->parameters["mch_billno"] == null ||
$this->parameters["mch_id"] == null ||
$this->parameters["wxappid"] == null ||
$this->parameters["nick_name"] == null ||
$this->parameters["send_name"] == null ||
$this->parameters["re_openid"] == null ||
$this->parameters["total_amount"] == null ||
$this->parameters["max_value"] == null ||
$this->parameters["total_num"] == null ||
$this->parameters["wishing"] == null ||
$this->parameters["client_ip"] == null ||
$this->parameters["act_name"] == null ||
$this->parameters["remark"] == null ||
$this->parameters["min_value"] == null
) {
return false;
}
return true;
}
function formatQueryParaMap($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if (null != $v && "null" != $v && "sign" != $k) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
/**
* @param $content
* @param $key
* @return string
* 拼接支付密钥
*/
function sign($content, $key)
{
try {
if (null == $key) {
//("签名key不能为空!" . "<br>");
}
if (null == $content) {
//("签名内容不能为空" . "<br>");
}
$signStr = $content . "&key=" . $key;
return strtoupper(md5($signStr));
} catch (Exception $e) {
die($e->errorMessage());
}
}
/**
* @param $value
* @return
*/
function trimString($value)
{
$ret = null;
if (null != $value) {
$ret = $value;
if (strlen($ret) == 0) {
$ret = null;
}
}
return $ret;
}
/**
* @param $arr
* @return string
* 拼接xml方法
*/
function arrayToXml($arr)
{
$xml = "<xml version='1.0' encoding='UTF-8' >";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @return string
* 获取服务器id
*/
function getip()
{
$ip = getenv("REMOTE_ADDR");
$ip1 = getenv("HTTP_X_FORWARDED_FOR");
$ip2 = getenv("HTTP_CLIENT_IP");
($ip1) ? $ip = $ip1 : null;
($ip2) ? $ip = $ip2 : null;
return $ip;
}
/**
* 生成随机数
*/
function great_rand()
{
$str = '1234567890abcdefghijklmnopqrstuvwxyz';
$t1 = '';
for ($i = 0; $i < 30; $i++) {
$j = rand(0, 35);
$t1 .= $str[$j];
}
return $t1;
}
/**
* 例如:
* appid: wxdxxxxxxxxxxxxxxxx
* mch_id: 10000100
* device_info: 1000
* Body: test
* nonce_str: ibuaiVcKdpRxkhJA
* 第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:
* stringA="appid=wxdxxxxxxxxxxx&body=test&device_info=1000&mch_i
* d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
* 第二步:拼接支付密钥:
* stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
* sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
* 9CF3B7"
*/
function get_sign()
{
define('PARTNERKEY', "xxxxxxxxxxxxxxxxxxxxxx");//微信商户上自定义的key
try {
if (null == PARTNERKEY || "" == PARTNERKEY) {
//写入错误日志
}
if ($this->check_sign_parameters() == false) { //检查生成签名参数
//写入错误日志
}
ksort($this->parameters);
$unSignParaString = $this->formatQueryParaMap($this->parameters, false);
return $this->sign($unSignParaString, $this->trimString(PARTNERKEY));
} catch (Exception $e) {
//日志
}
}
/**
* @param $url
* @param $vars
* @param int $second
* @param array $aHeader
* @return bool|mixed
* 请求微信
*/
function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
{
$ch = curl_init();
//超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//这里设置代理,如果有的话
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//cert 与 key 分别属于两个.pem文件
curl_setopt($ch, CURLOPT_SSLCERT, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'zhengshu' . DIRECTORY_SEPARATOR . 'apiclient_cert.pem');
curl_setopt($ch, CURLOPT_SSLKEY, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'zhengshu' . DIRECTORY_SEPARATOR . 'apiclient_key.pem');
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'zhengshu' . DIRECTORY_SEPARATOR . 'rootca.pem');
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
curl_close($ch);
return false;
}
}
}