欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信红包发送问题

程序员文章站 2022-05-28 15:33:04
...

         微信红包发送准备条件

  • 公众号支持发送红包功能
  • 每次发送红包不能低于1元
  • 公众号支付证书下载
  • 获取到要发送红包的微信的openID/金额
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $body = [
            'nonce_str' => sprintf('%10d%10d', rand(0, 9999999999), rand(0, 9999999999)),
            'mch_billno' => sprintf('%s%s2%09d', $mchid, date('Ymd'), $hongBaoLog->getId()),
            'mch_id' => $mchid,
            'wxappid' => $appid,
            'nick_name' => $data['nickname'],
            'send_name' => $data['send_name'],
            're_openid' => $data['openid'],
            'total_amount' => $data['amount'],
            'min_value' => $data['amount'],
            'max_value' => $data['amount'],
            'total_num' => 1,
            'wishing' => $data['wishing'],
            'client_ip' => '115.155.155.155',
            'act_name' => $data['act_name'],
            'remark' => $data['remark'],
        ];
        ksort($body);
        $query = urldecode(http_build_query($body));
        $stringSign = sprintf('%s&key=%s', $query, $key);
        $sign = md5($stringSign);
        $sign = strtoupper($sign);
        $body['sign'] = $sign;

        $xml = new \SimpleXMLElement('<xml></xml>');
        foreach ($body as $k => $v) {
            $xml->addChild($k, $v);
        }

        $certPath = sprintf('%s/misc/all.pem', $this->container->getParameter('kernel.root_dir'));

            $xml = $guzzle->post($url, [
                'body' => $xml->asXml(),
                'cert' => $certPath,
            ])->xml();

 

           请求返回处理

  • 返回是XML文件,需要解析xml文件拿到返回的数据
  • 涉及金额问题,根据返回值,如果有异常发送,必定要发送报错原因告诉给维护人员,并且要注意写日志!

 

            if (!isset($xml->result_code)) {
                return new JsonResponse(Commons::getResult('wechat api response result_code is null','500'));

            }

            if ('SUCCESS' != $xml->result_code) {
                $message = \Swift_Message::newInstance()
                    ->setSubject('红包接口通知!')
                    ->setFrom($this->getParameter('mailer_user'))
                    ->setTo($this->getParameter('app_error_receipants'))
                    ->setBody("$xml->return_msg"."红包发送失败,请知悉!");

                $this->get('mailer')->send($message);
                //将报错信息发送出去
                return new JsonResponse(Commons::getResult("$xml->return_msg",'500'));
            }
            $hongBaoLog->send();

 

 微信红包发送问题
            
    
    博客分类: PHP微信开发 红包API微信开发 求赞,留言