微信公众平台给用户发红包+php
直接上代码:
代码
<?php
CLASS WXHongBao {
private $mch_id = "********";
private $wxappid = "********";
private $client_ip = "";
private $apikey = "";
private $total_num = 1;
private $nick_name = "微信公众号红包";
private $send_name = "微信公众号红包";
private $wishing = "欢迎再次参与";
private $act_name = "";
private $remark = "";
private $nonce_str = "";
private $mch_billno = "";
private $re_openid = "";
private $total_amount = 1 ;
private $min_value = 1;
private $max_value = 1;
private $sign = "";
private $amt_type;
private $apiclient_cert;
private $apiclient_key;
private $apiclient_ca;
private $isShare = false;
private $share_content = "";
private $share_url ="";
private $share_imgurl = "";
private $wxhb_inited;
private $api_hb_group = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
private $api_hb_single = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
private $error = "ok";
function __construct(){
$this->wxhb_inited = false;
$this->apiclient_cert = getcwd() . "/zzz1/apiclient_cert.pem";
$this->apiclient_key = getcwd() . "/zzz1/apiclient_key.pem";
$this->apiclient_ca = getcwd() . "/zzz1/rootca.pem";
}
public function err(){
return $this->error;
}
public function error(){
return $this->err();
}
public function newhb($toOpenId,$amount){
if(!is_numeric($amount)){
$this->error = "金额参数错误";
return;
}elseif($amount<100){
$this->error = "金额太小";
return;
}elseif($amount>20000){
$this->error = "金额太大";
return;
}
$this->gen_nonce_str();
$this->gen_mch_billno();
$this->setOpenId($toOpenId);
$this->setAmount($amount);
$this->wxhb_inited = true;
$this->share_content= "";
$this->share_imgurl = "";
$this->share_url = "";
}
public function sendGroup($num=3){
$this->amt_type = "ALL_RAND";
return $this->send($this->api_hb_group,$num);
}
public function getApiSingle(){
return $this->api_hb_single;
}
public function getApiGroup(){
return $this->api_hb_group;
}
public function setNickName($nick){
$this->nick_name = $nick;
}
public function setSendName($name){
$this->send_name = $name;
}
public function setWishing($wishing){
$this->wishing = $wishing;
}
public function setActName($act){
$this->act_name = $act;
}
public function setRemark($remark){
$this->remark = $remark;
}
public function setOpenId($openid){
$this->re_openid = $openid;
}
public function setAmount($price){
$this->total_amount = (int)$price;
$this->min_value = (int)$price;
$this->max_value = (int)$price;
}
public function setHBminmax($min,$max){
$this->min_value = $min;
$this->max_value = $max;
}
public function setShare($img="",$url="",$content=""){
$this->share_content = $content;
$this->share_imgurl = $img;
$this->share_url = $url;
}
public function send(){
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
$total_num = 1;
if(!$this->wxhb_inited) {
$this->error .= "(红包未准备好)";
return false;
}
$this->total_num = $total_num;
$this->gen_Sign();
$xml = $this->genXMLParam();
file_put_contents("hbxml.debug",$xml);
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,10);
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);
curl_setopt($ch,CURLOPT_SSLCERT,$this->apiclient_cert);
curl_setopt($ch,CURLOPT_SSLKEY,$this->apiclient_key);
curl_setopt($ch,CURLOPT_CAINFO,$this->appclient_ca);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
$rsxml = simplexml_load_string($data);
if($rsxml->return_code == 'SUCCESS' ){
return true;
}else{
$this->error = json_encode($rsxml->return_msg);
return false;
}
}else{
$this->error = curl_errno($ch);
curl_close($ch);
return false;
}
}
private function gen_nonce_str(){
$this->nonce_str = strtoupper(md5(mt_rand().time()));
}
private function gen_Sign(){
unset($param);
$param["act_name"]=$this->act_name;
if($this->total_num==1){
$param["client_ip"]=$this->client_ip;
$param["max_value"]=$this->max_value;
$param["min_value"]=$this->min_value;
$param["nick_name"]=$this->nick_name;
}
$param["mch_billno"] = $this->mch_billno;
$param["mch_id"]=$this->mch_id;
$param["nonce_str"]=$this->nonce_str;
$param["re_openid"]=$this->re_openid;
$param["remark"]=$this->remark;
$param["send_name"]=$this->send_name;
$param["total_amount"]=$this->total_amount;
$param["total_num"]=$this->total_num;
$param["wishing"]=$this->wishing;
$param["wxappid"]=$this->wxappid;
if($this->share_content) $param["share_content"] = $this->share_content;
if($this->share_imgurl) $param["share_imgurl"] = $this->share_imgurl;
if($this->share_url) $param["share_url"] = $this->share_url;
if($this->amt_type) $param["amt_type"] = $this->amt_type;
ksort($param);
$sign_raw = "";
foreach($param as $k => $v){
$sign_raw .= $k."=".$v."&";
}
$sign_raw .= "key=".$this->apikey;
$this->sign = strtoupper(md5($sign_raw));
}
public function genXMLParam(){
$xml = "<xml>
<act_name><![CDATA[".$this->act_name."]]></act_name>
<client_ip><![CDATA[".$this->client_ip."]]></client_ip>
<max_value>".$this->max_value."</max_value>
<mch_billno>".$this->mch_billno."</mch_billno>
<mch_id>".$this->mch_id."</mch_id>
<min_value>".$this->min_value."</min_value>
<nick_name><![CDATA[".$this->nick_name."]]></nick_name>
<nonce_str>".$this->nonce_str."</nonce_str>
<re_openid>".$this->re_openid."</re_openid>
<remark><![CDATA[".$this->remark."]]></remark>
<send_name><![CDATA[".$this->send_name."]]></send_name>
<total_amount>".$this->total_amount."</total_amount>
<total_num>".$this->total_num."</total_num>
<wishing><![CDATA[".$this->wishing."]]></wishing>
<wxappid>".$this->wxappid."</wxappid>
<sign>".$this->sign."</sign>
</xml>
";
return $xml;
}
private function gen_mch_billno(){
$rnd_num = array('0','1','2','3','4','5','6','7','8','9');
$rndstr = "";
while(strlen($rndstr)<10){
$rndstr .= $rnd_num[array_rand($rnd_num)];
}
$this->mch_billno = $this->mch_id.date("Ymd").$rndstr;
}
}
?>
|