yanghui
2022-10-10 50687d620d71f7785f24fc0a8007c5a9f18e0e9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.panzhihua.applets.api;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.panzhihua.applets.umf.MyAESUtil;
import com.panzhihua.applets.umf.UmfPayUtil;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.HttpClientUtil;
import com.umf.api.service.UmfService;
import com.umf.api.service.UmfServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.json.XML;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
 
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 static com.umf.api.service.UmfServiceImpl.printResult;
 
/**
 * @auther lyq
 * @create 2021-04-14 15:02:49
 * @describe 微信支付回到函数
 */
@Slf4j
@RestController
@RequestMapping("/wx/")
public class WxCallbackApi {
 
    @Resource
    private CommunityService communityService;
    @Resource
    private UmfPayUtil umfPayUtil;
    @Value("${umf.file}")
    private String file;
    @Resource
    private UserService userService;
 
    @PostMapping("wxNotify")
    public void payCallback(HttpServletRequest request, HttpServletResponse response) {
        log.info("微信支付回调start");
        String inputLine = "";
        String notityXml = "";
        try {
            while ((inputLine = request.getReader().readLine()) != null) {
                notityXml += inputLine;
            }
            // 关闭流
            request.getReader().close();
            log.info("微信回调内容信息:" + notityXml);
            // 解析成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();
                    buffer.append("<xml>");
                    buffer.append("<return_code>SUCCESS</return_code>");
                    buffer.append("<return_msg>OK</return_msg>");
                    buffer.append("</xml>");
 
                    // 给微信服务器返回 成功标示 否则会一直询问 咱们服务器 是否回调成功
                    PrintWriter writer = response.getWriter();
                    // 返回
                    writer.print(buffer.toString());
                } 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();
        }
    }
    @GetMapping("wxNotifyAll")
    public void payCallbackAll(HttpServletRequest request, HttpServletResponse response) {
        //获取联动发送请求的参数
        String requestParam =  request.getQueryString();
        System.out.println("请求参数 :" + requestParam);
        //调用异步通知解析方法
        UmfService service = new UmfServiceImpl("53461",file);
        Map respMap = null;
        try {
            respMap = service.notifyDataParserMap(requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(respMap.get("error_code").equals("0000")){
            //调用uu洗车支付成功回调
            Map map=new HashMap();
            map.put("orderId",respMap.get("order_id"));
            HttpClientUtil.sendPostByJson("https://api.uucsh.cn/notify/wx/pzh-pay",JSONObject.toJSONString(map),1,"appid","10000");;
        }
        //调用SDK生成返回联动平台字符串,加到CONTENT中
        String resMetaData = service.responseUMFMap(respMap);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("<HEAD><META NAME=\"MobilePayPlatform\" CONTENT=\"" + resMetaData + "\" /></HEAD>");
        out.println("<BODY>");
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    @ApiOperation("uu洗车退款接口")
    @PostMapping("/uuRepay")
    public R uuRepay(@RequestBody String aesString){
        try {
            Map<String, String> aesMap= (Map<String, String>) JSON.parse(aesString);
            String desString=MyAESUtil.Decrypt(aesMap.get("aesString"),"Ryo7M3n8loC5Abcd");
            Map<String,String> desMap= (Map) JSON.parse(desString);
            Map map=umfPayUtil.repay(desMap.get("orderId"),desMap.get("merdate"), DateUtils.getDateFormatString(new Date(),"yyMMddHHmmss")+"0001",desMap.get("refundAmount"),desMap.get("orgAmount"));
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("加密验证失败");
        }
 
    }
    @ApiOperation("uu洗车推送")
    @PostMapping("/uuPush")
    public R uuPush(@RequestBody String aesString){
        try {
            Map<String, String> aesMap= (Map<String, String>) JSON.parse(aesString);
            String desString=MyAESUtil.Decrypt(aesMap.get("aesString"),"Ryo7M3n8loC5Abcd");
            Map<String,String> desMap= (Map) JSON.parse(desString);
            return userService.uuPush(desMap.get("washer_mobile"),desMap.get("phone"),desMap.get("washer_name"),Integer.parseInt(desMap.get("order_status")));
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("推送失败");
        }
 
    }
 
    @GetMapping("/settle")
    public R settle(String settleDate){
        return R.ok(umfPayUtil.settle(settleDate));
    }
}