PHP微信发红包帮助类WeChatReder.class

Class,代码是根据git一个代码修改得

config = array_merge($this->config, $config);
    }

    public function setParameter($k, $v)
    {
        $this->parameters[$k] = $v;
    }

    public function getParameter($k)
    {
        return $this->parameters[$k];
    }

    public function create_noncestr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            //$str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $str;
    }

    protected final function check_packet()
    {
        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;

    }

    /**
     * 例如:
     * appid:    wxd930ea5d5a258f4f
     * mch_id:    10000100
     * device_info:  1000
     * Body:    test
     * nonce_str:  ibuaiVcKdpRxkhJA
     * 第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:
     * stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
     * d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
     * 第二步:拼接支付密钥:
     * stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
     * sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
     * 9CF3B7"
     */
    protected function get_sign()
    {
        if (!isset($this->config['partnerkey']) || $this->check_packet() === false) {
            return false;
        }
        ksort($this->parameters);
        $unSignParaString = $this->formatQueryParaMap($this->parameters, false);
        return $this->sign($unSignParaString, $this->config['partnerkey']);
    }

    private function sign($content, $key)
    {
        $signStr = $content . "&key=" . $key;
        return strtoupper(md5($signStr));
    }

    private function verifySignature($content, $sign, $md5Key)
    {
        $signStr = $content . "&key=" . $md5Key;
        $calculateSign = strtolower(md5($signStr));
        $tenpaySign = strtolower($sign);
        return $calculateSign == $tenpaySign;
    }

    //生成红包接口XML信息
    /*
    
        ![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]
        ![CDATA[0010010404201411170000046545]]
        ![CDATA[888]]
        ![CDATA[wxcbda96de0b165486]]
        ![CDATA[nick_name]]
        ![CDATA[send_name]]
        ![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]
        ![CDATA[200]]
        ![CDATA[200]]
        ![CDATA[200]]
        ![CDATA[1]]
        ![CDATA[恭喜发财]]
        ![CDATA[127.0.0.1]]
        ![CDATA[新年红包]]
        ![CDATA[act_id]]
        ![CDATA[新年红包]]
        ![CDATA[https://xx/img/wxpaylogo.png]]
        ![CDATA[share_content]]
        ![CDATA[https://xx/img/wxpaylogo.png]]
        ![CDATA[https:/xx/img/wxpaylogo.png]]
        ![CDATA[50780e0cca98c8c8e814883e5caa672e]]
    
    */
    public function create_hongbao_xml($retcode = 0, $reterrmsg = "ok")
    {
        //var_dump($this->parameters);
        //生成签名
        $this->setParameter('sign', $this->get_sign());
        dump($this->parameters);
        return $this->arrayToXml($this->parameters);
    }


    public function sendRedPacket($vars, $ssl = [], $second = 30, $aHeader = [])
    {
        $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, $this->red_packet_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //以下两种方式需选择一种
        //第一种方法,cert 与 key 分别属于两个.pem文件
        if (array_key_exists("cert", $ssl) && array_key_exists('key', $ssl) && array_key_exists('rootca', $ssl)) {
            curl_setopt($ch, CURLOPT_SSLCERT, $ssl['cert']);
            curl_setopt($ch, CURLOPT_SSLKEY, $ssl['key']);
            curl_setopt($ch, CURLOPT_CAINFO, $ssl['rootca']);
        }
        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);
            $responseObj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
            return $responseObj;
        } else {
            $error = curl_errno($ch);
            //echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }

    private function genAllUrl($toURL, $paras)
    {
        $allUrl = null;
        if (null == $toURL) {
            die("toURL is null");
        }
        if (strripos($toURL, "?") == "") {
            $allUrl = $toURL . "?" . $paras;
        } else {
            $allUrl = $toURL . "&" . $paras;
        }

        return $allUrl;
    }

    private function splitParaStr($src, $token)
    {
        $resMap = array();
        $items = explode($token, $src);
        foreach ($items as $item) {
            $paraAndValue = explode("=", $item);
            if ($paraAndValue != "") {
                $resMap[$paraAndValue[0]] = $parameterValue[1];
            }
        }
        return $resMap;
    }

    private 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 . "&";
            }
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }

    private function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            //  if (null != $v && "null" != $v && "sign" != $k) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= strtolower($k) . "=" . $v . "&";
            //}
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }

    private function arrayToXml($arr)
    {
        $xml = "";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "";

            } else
                $xml .= "<" . $key . ">";
        }
        $xml .= "";
        return $xml;
    }

}

调用方法 require or load,由于我的是多微信端管理,所以定自行修改匹配代码


        $MCHID = "";
        $money = 1 * 100;
        $openid = "";

        $config = ['partnerkey' => '', 'appid' => ''];
        $redHelper = new WeChatReder($config);

        $redHelper->setParameter("nonce_str", $redHelper->create_noncestr());//随机字符串,丌长于 32 位
        $redHelper->setParameter("mch_billno", OrderHelper::createOrderNo());//订单号
        $redHelper->setParameter("mch_id", $MCHID);//商户号
        $redHelper->setParameter("wxappid", $config['appid']);

        $redHelper->setParameter("nick_name", '腾讯');//提供方名称
        $redHelper->setParameter("send_name", '腾讯');//红包发送者名称
        $redHelper->setParameter("re_openid", $openid);//相对于医脉互通的openid
        $redHelper->setParameter("total_amount", $money);//付款金额,单位分
        $redHelper->setParameter("min_value", $money);//最小红包金额,单位分
        $redHelper->setParameter("max_value", $money);//最大红包金额,单位分
        $redHelper->setParameter("total_num", 1);//红包収放总人数
        $redHelper->setParameter("wishing", '感谢您参加抢红包,祝您新年快乐!');//红包祝福诧
        $redHelper->setParameter("client_ip", '127.0.0.1');//调用接口的机器 Ip 地址
        $redHelper->setParameter("act_name", "test");//活劢名称
        $redHelper->setParameter("remark", '告诉你的朋友一起来抢红包吧');//备注信息
//      $redHelper->setParameter("logo_imgurl", RES_DOMAIN.'assets/images/getheadimg.jpg');//商户logo的url
//      $redHelper->setParameter("share_content", '一起来抢[腾讯]红包吧');//分享文案
//      $redHelper->setParameter("share_url", RES_DOMAIN);//分享链接
//      $redHelper->setParameter("share_imgurl", RES_DOMAIN.'assets/images/getheadimg.jpg');//分享的图片url


        $postXml = $redHelper->create_hongbao_xml();
        $weid = 1;
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $ssl['cert'] = SSL_PATH . "{$weid}/apiclient_cert.pem";
        $ssl['key'] = SSL_PATH . "{$weid}/apiclient_key.pem";
        $ssl['rootca'] = SSL_PATH . "{$weid}/rootca.pem";

        if (!file_exists($ssl['key'])) {
            exit("证书不存在");
        }
        $rest = $redHelper->sendRedPacket($postXml, $ssl);
        var_dump($rest);

你可能感兴趣的:(PHP微信发红包帮助类WeChatReder.class)