无关风月
2025-09-01 324ed9fac852bc4117ee0da65a586adef258bd52
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.ruoyi.other.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.IntegralPay;
import com.ruoyi.other.api.domain.IntegralRecord;
import com.ruoyi.other.api.domain.ServicePay;
import com.ruoyi.other.query.ServiceListQuery;
import com.ruoyi.other.service.TIntegralPayService;
import com.ruoyi.other.service.TIntegralRecordService;
import com.ruoyi.other.service.TIntegralRuleService;
import com.ruoyi.other.service.TServicePayService;
import com.ruoyi.other.util.UUIDUtil;
import com.ruoyi.other.util.payment.wx.PayResult;
import com.ruoyi.other.util.payment.wx.WechatPayService;
import com.ruoyi.other.vo.ServiceListVO;
import com.ruoyi.other.vo.ServiceVO;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * <p>
 * 微信回调
 * </p>
 *
 * @author 无关风月
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/wx")
public class WXCallBackController {
 
    @Autowired
    private TIntegralRuleService integralRuleService;
    @Autowired
    private TIntegralRecordService integralRecordService;
    @Autowired
    private TIntegralPayService integralPayService;
 
    @Autowired
    private TokenService tokenService;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private WechatPayService wechatPayService;
    @Resource
    private TServicePayService servicePayService;
 
    @ResponseBody
    @PostMapping("/integralCallback")
    public R integralCallback(HttpServletRequest request) throws IOException {
        System.err.println("积分充值回调");
        Map<String, String> params = new HashMap<String, String>();
        System.err.println("请求"+request);
        BufferedReader reader = request.getReader();
        String string1 = reader.toString();
        System.err.println("请求reader"+string1);
        StringBuilder requestBody = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            requestBody.append(line);
        }
        System.err.println("全部请求体"+requestBody);
        // URL解码
        String decodedBody = URLDecoder.decode(requestBody.toString(), "UTF-8");
        System.err.println("解码后请求体: " + decodedBody);
        String[] paramsArray = decodedBody.split("&");
        String respDataJson = "";
        for (String param : paramsArray) {
            if (param.startsWith("resp_data=")) {
                respDataJson = param.substring(10); // 去掉"resp_data="前缀
                break;
            }
        }
        System.err.println("处理后的"+respDataJson);
        // 转换为JSONObject
        JSONObject resp_data = JSONObject.parseObject(respDataJson);
        System.err.println(resp_data);
        String string = resp_data.getString("trans_stat");
        if (!string.equals("S")){
            System.err.println("陈工");
        }
        String req_seq_id = resp_data.getString("req_seq_id");
        String hf_seq_id = resp_data.getString("hf_seq_id");
        IntegralPay integralPay = integralPayService.lambdaQuery().eq(IntegralPay::getCode, req_seq_id).one();
        if (integralPay != null && integralPay.getPayStatus() == 1) {
 
            SysUser data = sysUserClient.getSysUser(integralPay.getUserId()).getData();
            integralPay.setPayStatus(2);
            integralPay.setPayTime(LocalDateTime.now());
            integralPay.setOrderNumber(hf_seq_id);
            integralPayService.updateById(integralPay);
            IntegralRecord integralRecord = new IntegralRecord();
            integralRecord.setPayId(integralPay.getId());
            integralRecord.setSiteId(data.getSiteId());
            integralRecord.setAppUserId(integralPay.getUserId());
            integralRecord.setIntegralType(1);
            integralRecord.setIntegralCount(integralPay.getIntegralCount());
            integralRecord.setDelFlag(0);
            integralRecord.setCreateTime(LocalDateTime.now());
            if (integralPay.getIntegralCount()>0){
                integralRecordService.save(integralRecord);
            }
            data.setIntegral(data.getIntegral()!=null?data.getIntegral() + integralPay.getIntegralCount():integralPay.getIntegralCount());
            sysUserClient.updateSysUser(data);
            return R.ok(null, "success");
        }
        return R.ok(null, "error");
    }
 
    public static void main(String[] args) {
        String s = "resp_desc=交易成功[000]&resp_code=00000000&sign=PymKvUt5iBSn1EmBHu41ESxygYl80rHVRZVFlmipqg5zYSF/Yeq30ZJm0l5ztvIaGvUQChYQ5DAVkN/N+TYg1WU+FhMi/RTP15OpBAj8gGtYPyk7Z4lF9FLwiSGmX0RvCYWI5+LVsk10i7+j9332kAuB5GpKm0tOaDGULHZYlfrt5D5BgJ8RKdjYJ4OOV1Ex5J5PNLPYE5uc8rdzTQ9HHTu4N+sBOBV+tXJFYtSOoAaiDsRmv5Er8X8ogSlNODmaj7zBbZQHrPTTs8rtBl+YbQo28cmgykwdl9pPTMtAjkL9dX6/JVApquKgm9M+v8+CDT0MKjn/nyVE4J2EWc/7LQ==&" +
                "resp_data={\"acct_date\":\"20250816\",\"acct_id\":\"F10004674\",\"acct_split_bunch\":{\"acct_infos\":[{\"acct_date\":\"20250816\",\"acct_id\":\"F10004674\",\"div_amt\":\"0.01\",\"huifu_id\":\"6666000174575407\"}],\"fee_acct_date\":\"20250816\",\"fee_acct_id\":\"F10004674\",\"fee_amt\":\"0.00\",\"fee_huifu_id\":\"6666000174575407\"},\"acct_stat\":\"S\",\"atu_sub_mer_id\":\"797542530\",\"avoid_sms_flag\":\"\",\"bagent_id\":\"6666000167477678\",\"bank_desc\":\"支付成功\",\"bank_message\":\"支付成功\",\"bank_order_no\":\"4200002762202508169050647775\"" +
                ",\"bank_seq_id\":\"273346\",\"bank_type\":\"OTHERS\",\"base_acct_id\":\"F10004674\"," +
                "\"batch_id\":\"250816\",\"channel_type\":\"N\",\"charge_flags\":\"758_0\",\"debit_type\"" +
                ":\"0\",\"delay_acct_flag\":\"N\",\"div_flag\":\"0\",\"end_time\":\"20250816153144\"," +
                "\"fee_amount\":\"0.00\",\"fee_amt\":\"0.00\",\"fee_flag\":2,\"fee_formula_infos\"" +
                ":[{\"fee_formula\":\"MAX(0.00,AMT*0.005)\",\"fee_type\":\"TRANS_FEE\"}],\"fee_rec_type" +
                "\":\"1\",\"fee_type\":\"INNER\",\"gate_id\":\"IB\",\"hf_seq_id\":\"002900TOP1B250816153133P665ac139cd300000\"" +
                ",\"huifu_id\":\"6666000174575407\",\"is_delay_acct\":\"0\",\"is_div\":\"0\",\"maze_resp_code\":\"\",\"mer_nam" +
                "e\":\"贵州中嘉瑞祺新能源科技有限公司\",\"mer_ord_id\":\"2025081615313312273346\",\"mypaytsf_discount\":\"0.00\",\"" +
                "need_big_object\":false,\"notify_type\":2,\"org_auth_no\":\"\",\"org_huifu_seq_id\":\"\",\"org_t" +
                "rans_date\":\"\",\"out_ord_id\":\"4200002762202508169050647775\",\"out_trans_id\":\"42" +
                "00002762202508169050647775\",\"party_order_id\":\"032425081655893847103301\",\"pay" +
                "_amt\":\"0.01\",\"pay_scene\":\"02\",\"posp_seq_id\":\"032425081655893847103301\"" +
                ",\"product_id\":\"KAZX\",\"ref_no\":\"153133273346\",\"req_date\":\"202508" +
                "16\",\"req_seq_id\":\"2025081615313312273346\",\"resp_code\":\"00000000" +
                "\",\"resp_desc\":\"交易成功\",\"risk_check_data\":{},\"risk_check_info\":{" +
                "},\"settlement_amt\":\"0.01\",\"sub_resp_code\":\"00000000\",\"sub_resp_desc\"" +
                ":\"交易成功\",\"subsidy_stat\":\"I\",\"sys_id\":\"6666000174575407\",\"" +
                "trade_type\":\"T_MINIAPP\",\"trans_amt\":\"0.01\",\"trans_date\":\"2025081" +
                "6\",\"trans_fee_allowance_info\":{\"actual_fee_amt\":\"0.00\",\"allowance_fee_" +
                "amt\":\"0.00\",\"allowance_type\":\"0\",\"receivable_fee_amt\":\"0.00\"},\"trans_s" +
                "tat\":\"S\",\"trans_time\":\"153133\",\"trans_type\":\"T_MINIAPP\",\"ts_encash_detail\":[" +
                "],\"wx_response\":{\"bank_type\":\"OTHERS\",\"coupon_fee\":\"0.00\",\"openid\":\"o8jhot3gj5UeqqT" +
                "-od35bM6rJOYk\",\"sub_appid\":\"wx049faf9c5234f31c\",\"sub_openid\":\"oJiKr7Rm5v3qGzOSy5WQp8_qrvic\"}}";
 
        // 解析参数
        String[] paramsArray = s.split("&");
        String respDataJson = "";
        for (String param : paramsArray) {
            if (param.startsWith("resp_data=")) {
                respDataJson = param.substring(10); // 去掉"resp_data="前缀
                break;
            }
        }
 
        // 转换为JSONObject
        JSONObject resp_data = JSONObject.parseObject(respDataJson);
        System.err.println(resp_data);
        String string = resp_data.getString("trans_stat");
        if (!string.equals("S")){
            System.err.println("陈工");
        }
        String req_seq_id = resp_data.getString("req_seq_id");
        String hf_seq_id = resp_data.getString("hf_seq_id");
        System.err.println(req_seq_id);
        System.err.println(hf_seq_id);
    }
    @ResponseBody
    @PostMapping("/serviceCallback")
    public void serviceCallback(HttpServletRequest request, HttpServletResponse response) {
        System.err.println("服务费缴纳回调");
        PayResult payResult = null;
        try {
            payResult = wechatPayService.processNotify(request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ServicePay servicePay = servicePayService.lambdaQuery().eq(ServicePay::getCode, payResult.getOrderNumber()).one();
        if (servicePay != null && servicePay.getPayStatus() == 1) {
            servicePay.setPayStatus(2);
            ServicePay servicePayBefore = servicePayService.lambdaQuery()
                    .eq(ServicePay::getUserId, servicePay.getUserId())
                    .eq(ServicePay::getPayStatus, 2)
                    .orderByDesc(ServicePay::getCreateTime)
                    .last("limit 1")
                    .one();
            if (servicePayBefore != null) {
                servicePay.setEndTime(servicePayBefore.getEndTime().plusDays(365));
            } else {
                servicePay.setEndTime(LocalDateTime.now().plusDays(365));
 
            }
 
            servicePay.setPayType(1);
            servicePay.setPayTime(LocalDateTime.now());
            servicePay.setOrderNumber(payResult.getTransactionId());
            servicePayService.updateById(servicePay);
        }
        response.setStatus(200);
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        out.println("success");
        out.flush();
        out.close();
    }
 
 
}