huanghongfa
2021-04-20 ac0a5107a0cfa9e2198176d6cdf33f6516edda12
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxCallbackApi.java
@@ -1,16 +1,21 @@
package com.panzhihua.applets.api;
import com.panzhihua.common.utlis.PayUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.dtos.shop.WxPayNotifyOrderDTO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.json.XML;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
 * @auther lyq
@@ -22,10 +27,12 @@
@RequestMapping("/wx/")
public class WxCallbackApi {
    @PostMapping("notify")
    @Resource
    private CommunityService communityService;
    @PostMapping("wxNotify")
    public void payCallback(HttpServletRequest request, HttpServletResponse response) {
        log.info("微信回调接口方法 start");
        log.info("微信回调接口 操作逻辑 start");
        log.info("微信支付回调start");
        String inputLine = "";
        String notityXml = "";
        try {
@@ -35,18 +42,31 @@
            //关闭流
            request.getReader().close();
            log.info("微信回调内容信息:"+notityXml);
            //解析成Map
            Map<String,String> map = PayUtil.doXMLParse(notityXml);
            //判断 支付是否成功
            if("SUCCESS".equals(map.get("result_code"))){
                log.info("微信回调返回是否支付成功:是");
                //获得 返回的商户订单号
                String outTradeNo = map.get("out_trade_no");
                log.info("微信回调返回商户订单号:"+outTradeNo);
                //访问DB
//                WechatAppletGolfPayInfo payInfo = appletGolfPayInfoMapper.selectByPrimaryKey(outTradeNo);
                log.info("微信回调 根据订单号查询订单状态:");
                if("0".equals("0")){
            //解析成Json
            org.json.JSONObject xmlJson = XML.toJSONObject(notityXml);
            if(StringUtils.isNotEmpty(xmlJson.toString())){
                JSONObject resultData = JSON.parseObject(xmlJson.get("xml").toString());
                //成功支付订单
                if(resultData.getString("result_code").equals("SUCCESS") && resultData.getString("return_code").equals("SUCCESS")){
                    WxPayNotifyOrderDTO notifyOrderDTO = new WxPayNotifyOrderDTO();
                    //订单号
                    String orderTradeNo = resultData.getString("out_trade_no");
                    notifyOrderDTO.setOrderTradeNo(orderTradeNo);
                    //微信支付订单号
                    String wxTradeNo = resultData.getString("transaction_id");
                    notifyOrderDTO.setWxTradeNo(wxTradeNo);
                    //订单金额
                    String totalFee = resultData.getString("total_fee");
                    notifyOrderDTO.setTotalFee(totalFee);
                    //支付完成时间
                    String payTime = resultData.getString("time_end");
                    notifyOrderDTO.setPayTime(payTime);
                    //现金支付金额
                    String cashFee = resultData.getString("cash_fee");
                    notifyOrderDTO.setCashFee(cashFee);
                    //根据订单号修改订单信息
                    communityService.wxOrderPayNotify(notifyOrderDTO);
                    //封装 返回值
                    StringBuffer buffer = new StringBuffer();
@@ -59,14 +79,21 @@
                    PrintWriter writer = response.getWriter();
                    //返回
                    writer.print(buffer.toString());
                }
            }else{
                log.info("微信回调返回支付失败");
                }else{//未成功支付订单
                    //封装 返回值
                    StringBuffer buffer = new StringBuffer();
                    buffer.append("<xml>");
                    buffer.append("<return_code>FAIL</return_code>");
                    buffer.append("<return_msg>FAIL</return_msg>");
                    buffer.append("</xml>");
                    //给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
                    PrintWriter writer = response.getWriter();
                    //返回
                    writer.print(buffer.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }