php + uniapp发送微信小程序红包

php 后台:

1:想要领取红包,先要发红包,生成红包参数

public function send(){
        //随机字符串
        $nonce_str = $this->createNoncestr();
        //商户订单号,需要每次随机,这里做测试,所以先写死了
        $mch_billno = "10000098201411111234567890";
        //商户号
        $mch_id = "1624732058";
        //小程序账号appid
        $wxappid = "wxfXXXXXXXXXX";
        //商户名称
        $send_name = "XXXX";
        //用户小程序openid
        $re_openid = "oxxxxxxxxxxxx";
        //付款金额,单位分
        $total_amount = 100;
        //红包发放总人数
        $total_num = 1;
        //红包祝福语
        $wishing = "获得分成啦,好开心";
        //活动名称
        $act_name = "商品分成";
        //备注
        $remark = "推荐越多得越多,快来!";
        //通知用户形式
        $notify_way = "MINI_PROGRAM_JSAPI";

        $parameters = array(
            'nonce_str' => $nonce_str,
            'mch_billno' => $mch_billno,
            'mch_id' => $mch_id,
            'wxappid' => $wxappid,
            'send_name' => $send_name,
            're_openid' => $re_openid,
            'total_amount' => $total_amount,
            'total_num' => $total_num,
            'wishing' => $wishing,
            'act_name' => $act_name,
            'remark' => $remark,
            'notify_way' => $notify_way
        );
        //生成签名,所有参数+key然后MD5
        $parameters['sign'] = $this->getSign($parameters);

        $xmlData = $this->arrayToXml($parameters);
//        var_dump($xmlData);
        $curlres = $this->postXmlCurl($xmlData, "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb");
//        var_dump($curlres);
        $res = $this->xmlToArray($curlres);

        var_dump($res);exit;
        return $res;
    }


    private function postXmlCurl($xml, $url)
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //证书的位置
//        var_dump(curl_setopt($ch, CURLOPT_SSLCERT, __DIR__ . '/cert/apiclient_cert.pem'));die;
        curl_setopt($ch, CURLOPT_SSLCERT,  '/data/xx/ht/extend/pay-php-sdk/cert/apiclient_cert.pem');
        //证书key的位置
        curl_setopt($ch, CURLOPT_SSLKEY,'/data/xx/ht/extend/pay-php-sdk/cert/apiclient_key.pem');

        //运行curl
        $data = curl_exec($ch);
        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
            throw new Exception("curl出错,错误码:$error");
        }
    }

    //数组转换成xml
    private function arrayToXml($arr) {
        $xml = "";
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . $this->arrayToXml($val) . "";
            } else {
                $xml .= "<" . $key . ">" . $val . "";
            }
        }
        $xml .= "";
        return $xml;
    }

    //xml转换成数组
    private function xmlToArray($xml) {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring), true);
        return $val;
    }

    //作用:产生随机字符串,不长于32位
    private function createNoncestr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //作用:生成签名
    private function getSign($Obj) {
        $key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  //商户支付key
        foreach ($Obj as $k => $v) {
            //不为空的参数才参与签名
            if(!empty($v)){
                $Parameters[$k] = $v;
            }
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //签名步骤二:在string后加入KEY
        $String = $String . "&key=" . $key;
        //签名步骤三:MD5加密
        $String = md5($String);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($String);
        return $result;
    }

    //作用:格式化参数,签名过程需要使用
    private function formatBizQueryParaMap($paraMap, $urlencode) {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }

        2. 返回结果中有我们需要的package

     3.package进行urlencode,在php中代码就是

$package = urlencode("sendid=ef374b7xxxxxxx&xxxxx");

     4. 支付签名

 $parameters = [
            "appId"=>"wxxxxxxxxxxx",    //小程序appid
            "timeStamp"=>"1686807639",    //时间戳
            "nonceStr"=>$this->createNoncestr(),    //随机32位字符串
            "package"=>"sendid%3Def3xxxxxxxxx"      //刚才生成的urlencode后的package
        ];

   $sign = $this->getSign($parameters);

uniapp端:

        注:小程序红包只有在扫码进入的时候,红包才会生效

			wx. sendBizRedPacket ({
			    timeStamp: '1686807639', // 支付签名时间戳,
			    nonceStr: 'BXb1WNvpH1K5EaTt2Ye56bvZoDsrCBYk', // 支付签名随机串,不长于 32 位
			    package: 'sendid%3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', //扩展字段,由商户传入
			    signType: 'MD5', // 签名方式,
			    paySign: '015D684D67EC62863A54FCD91CA73A8C', // 支付签名
			    success:function(res){
					console.log(res);
				},
			    fail:function(res){
					console.log(res);
				},
			    complete:function(res){
					console.log(res);
				}
			})

你可能感兴趣的:(uni-app,微信小程序,数学建模)