微信公众号 调用红包接口

微信的红包接口已经提供了详细的说明。在使用之前需要先开通 然后记得充钱。微信发红包金额1-200,单位是分。

 
define('APPID','adfadf98735123');
//商户ID
define('MCHID','1111111');
//key
define('KEY','keykeykeykey');
define('SENDER','为右城');

class WxPay{

    private $arrayMap=array();

    function __construct($openid,$money){
        //随机字符串 $obj['nonce_str'];
        //签名 $obj['sign'];
        $this->arrayMap['mch_billno']=MCHID.date('Ymd').time();//商户订单号
        $this->arrayMap['mch_id']=MCHID;//商户号
        $this->arrayMap['wxappid']=APPID;//公众账号appid
        $this->arrayMap['send_name']=SENDER;//商户名称
        $this->arrayMap['re_openid']=$openid;//用户openid    ouNtss-9LeY49B9aQekGX7y43okE
        $this->arrayMap['total_amount']=$money;//付款金额
        $this->arrayMap['total_num']=1;//红包发放总人数
        $this->arrayMap['wishing']='恭喜发财';//红包祝福语
        $this->arrayMap['client_ip']=$_SERVER['REMOTE_ADDR'];//获取当天Ip地址
        $this->arrayMap['act_name']=SENDER.'红包';//活动名称
        $this->arrayMap['remark']=SENDER.'红包';//备注
        //场景id $obj['scene_id'];
        //活动信息 $obj['risk_info'];
        //资金授权商户号 $obj['consume_mch_id'];

        //设置随机字符串
        $this->arrayMap['nonce_str']=$this->get_nonce_str(32);

        //设置证书签名
        $this->sign($this->arrayMap);
    }

    function pay(){
        //将请求值转给xml格式
        $xmldata=$this->Array2XML();
        return $this->curl_post_ssl($xmldata);
    }

    //生成签名
    function sign($parmMap){
        ksort($parmMap);//键名升序排序
        $keys=array_keys($parmMap);
        $stringA='';
        foreach($keys as $k){
            $stringA.=$k.'='.$parmMap[$k].'&';
        }
        $stringSignTemp=$stringA.'key='.KEY;
        $this->arrayMap['sign']=strtoupper(MD5($stringSignTemp));
    }

    //创建32位长度的随机字符串
    function get_nonce_str($length){
        $charts='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max=strlen($charts)-1;
        $str='';
        for($i=0;$i<$length;$i++){
            $str.=$charts[mt_rand(0,$max)];
        }
        return $str;
    }

    function Array2XML(){
        $xml='';
            $keys=array_keys($this->arrayMap);
            foreach($keys as $k){
                $xml.='<'.$k.'>'.$this->arrayMap[$k].'.$k.'>';
            }
        $xml.='';
        return $xml;
    }

    function curl_post_ssl($vars, $second=30,$aHeader=array())
    {
        $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

        //以下两种方式需选择一种

        //第一种方法,cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');

        //第二种方式,两个文件合成一个.pem文件
        //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/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 { 
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n"; 
            curl_close($ch);
            return false;
        }
    }
}
?> 

你可能感兴趣的:(微信公众号 调用红包接口)