无关风月
4 天以前 7fd053651ac11db87fe4f6c57e65eed3b9a59452
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
package com.ruoyi.other.controller;
 
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.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
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, String r2_OrderNo) {
//        System.err.println("积分充值回调");
//        PayResult payResult= null;
//        try {
//            payResult = wechatPayService.processNotify(request);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
        System.err.println("======积分充值回调");
        System.err.println("======积分充值回调单号" + r2_OrderNo);
        System.err.println("请求" + request.getParameterMap());
        Map<String, String[]> parameterMap = request.getParameterMap();
        String r6Status = request.getParameter("r6_Status");
        if (org.springframework.util.StringUtils.hasLength(r6Status)) {
            if (r6Status.equals("101")) {
                return R.fail("支付失败");
            }
        }
        // 循环打印
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String key = entry.getKey();
            String[] values = entry.getValue();
            for (String value : values) {
                System.err.println("======回调开始" + key + ":" + value);
            }
        }
        IntegralPay integralPay = integralPayService.lambdaQuery().eq(IntegralPay::getCode, r2_OrderNo).one();
        if (integralPay != null && integralPay.getPayStatus() == 1) {
            SysUser data = sysUserClient.getSysUser(integralPay.getUserId()).getData();
            integralPay.setPayStatus(2);
            integralPay.setPayTime(LocalDateTime.now());
//            integralPay.setOrderNumber(payResult.getTransactionId());
            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());
            integralRecordService.save(integralRecord);
            data.setIntegral(data.getIntegral() + integralPay.getIntegralCount());
            sysUserClient.updateSysUser(data);
            return R.ok(null, "success");
        }
        return R.ok(null, "error");
 
    }
 
    @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();
    }
 
 
}