Home  >  Article  >  Backend Development  >  Development of ThinkPHP5 WeChat cash red envelope

Development of ThinkPHP5 WeChat cash red envelope

不言
不言Original
2018-07-07 14:41:442492browse

This article mainly introduces the development of ThinkPHP5 WeChat cash red envelopes. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Controller:

    /**
     * 发放现金红包     */public function payLuckyMoney()
{
    $obj2 = array();    //appid
    $obj2['wxappid'] = config('wx_gzh.appId');    //商户id
    $obj2['mch_id'] = config('wx_sh.mchId');    //组合成28位,根据官方开发文档,可以自行设置
    $obj2['mch_billno'] = config('wx_sh.mchId') . date('YmdHis') . rand(1000, 9999);    // 调用接口的机器IP地址
    $obj2['client_ip'] = $_SERVER['REMOTE_ADDR'];    //接收红包openid
    $obj2['re_openid'] = session('openid');    /* 付款金额设置start,按照概率设置随机发放。
     * 1-200元之间,单位分。这里设置95%概率为1-2元,5%的概率为2-10元     */
    $n = rand(1, 100);    if ($n <= 95) {
        $obj2[&#39;total_amount&#39;] = rand(100, 200);
    } else {
        $obj2[&#39;total_amount&#39;] = rand(200, 1000);
    }    //$obj2[&#39;total_amount&#39;] = 100;
    /* 付款金额设置end */

    // 红包个数
    $obj2[&#39;total_num&#39;] = 1;    // 商户名称
    $obj2[&#39;send_name&#39;] = "小门太";    // 红包祝福语
    $obj2[&#39;wishing&#39;] = "恭喜发财,大吉大利";    // 活动名称
    $obj2[&#39;act_name&#39;] = "小门太认证领红包";    // 备注
    $obj2[&#39;remark&#39;] = "小门太红包";    /* 文档中未说明以下变量,李富林博客中有。注释起来也没问题。不需要。
    $obj2[&#39;min_value&#39;] = $money;
    $obj2[&#39;max_value&#39;] = $money;
    $obj2[&#39;nick_name&#39;] = &#39;小门太红包&#39;;    */

    $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
    $isPay = pay_lucky_money($url, $obj2);
    $res = xml_to_array($isPay);    // 发放成功,把红包数据插入数据库
    if ($res[&#39;return_msg&#39;] == &#39;发放成功&#39;) {        // 发放成功,进行逻辑处理    
    } else {        // 发放失败,返回失败原因
        return $res[&#39;return_msg&#39;];
    }
}

common.php function (you can also common a class to call):

// ---- 以下是微信现金红包的区域start ---- ///**
 * 微信发放现金红包核心函数,调用本函数就直接发放红包了。
 * @param $url 现金红包的请求地址
 * @param $obj
 * @return mixed */function pay_lucky_money($url, $obj)
{    //创建随机字符串(32位)
    $obj[&#39;nonce_str&#39;] = str_rand();    //创建签名
    $sign = get_sign($obj, false);    //halt($sign);
    $obj[&#39;sign&#39;] = $sign;    //将签名传入数组
    $postXml = array_to_xml($obj);    //将参数转为xml格式    //halt($postXml);
    $responseXml = curl_post_ssl($url, $postXml);    //提交请求    //halt($responseXml);
    return $responseXml;
}/**
 * @param $arr 生成前面的参数
 * @param $urlencode
 * @return string 返回加密后的签名 */function get_sign($arr, $urlencode)
{
    $buff = "";    //对传进来的数组参数里面的内容按照字母顺序排序,a在前面,z在最后(字典序)    ksort($arr);    foreach ($arr as $k => $v) {        if (null != $v && "null" != $v && "sign" != $k) {    //签名不要转码
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
    }    // 去掉末尾符号“&”,其实不用这个if,因为长度肯定大于0
    if (strlen($buff) > 0) {
        $stringA = substr($buff, 0, strlen($buff) - 1);
    }    //签名拼接api
    $stringSignTemp = $stringA . "&key=" . config(&#39;wx_sh.key&#39;);    //签名加密并大写
    $sign = strtoupper(md5($stringSignTemp));    return $sign;
}//post请求网站,需要证书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文件    //请确保您的libcurl版本是否支持双向认证,版本高于7.20.1,相当于发curl验证【当前文件所在目录/cert/wxpay/】下的两个pem证书文件。    curl_setopt($ch, CURLOPT_SSLCERT, dirname(__FILE__) . DIRECTORY_SEPARATOR .        &#39;cert&#39; . DIRECTORY_SEPARATOR . &#39;wxpay&#39; . DIRECTORY_SEPARATOR . &#39;apiclient_cert.pem&#39;);
    curl_setopt($ch, CURLOPT_SSLKEY, dirname(__FILE__) . DIRECTORY_SEPARATOR .        &#39;cert&#39; . DIRECTORY_SEPARATOR . &#39;wxpay&#39; . DIRECTORY_SEPARATOR . &#39;apiclient_key.pem&#39;);    //curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.    //    &#39;cert&#39;.DIRECTORY_SEPARATOR.&#39;rootca.pem&#39;);    //这个不需要,因为大部分的操作系统都已经内置了rootca.pem证书了,就是常见的CA证书。
    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;
    }
}// ---- 以下是微信现金红包的区域end ---- //

————Above, because my code is written under common.php, and common.php is under the application directory, so the certificate is placed under the application/cert/wxpay/ directory

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the use of thinkphp behavior

Use PHPstudy to deploy the PHP system under Windows server

The above is the detailed content of Development of ThinkPHP5 WeChat cash red envelope. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn