2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-25 dda268997ca8f8a364f7c19b45d7a43a50a98efe
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
package com.sandu.ximon.admin.pay;
 
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.sandu.common.security.annotation.AnonymousAccess;
import com.sandu.ximon.admin.pay.alipay.UsrAlipayConfigService;
import com.sandu.ximon.admin.service.C3mOrderService;
import com.sandu.ximon.dao.domain.AliConfigEntity;
import com.sandu.ximon.dao.domain.C3mOrder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
@RestController
@AllArgsConstructor
@RequestMapping("/callback/pay")
@Slf4j
public class PayCallBackController {//http://112.74.63.130:20017/callback/pay/wechatCallback
 
 
    private final C3mOrderService userOrderService;
    private final UsrAlipayConfigService alipayConfigService;
 
    /**
     * 微信支付回调
     * @param request
     * @param httpResponse
     * @return
     */
    @AnonymousAccess
    @RequestMapping("wechatCallback")
    public Object wechatNotify(HttpServletRequest request, HttpServletResponse httpResponse) {
        return userOrderService.payOrderInstoreNotify(request,httpResponse);
    }
 
    /**
     * 支付宝支付回调
     *
     * @return
     */
    @AnonymousAccess
    @RequestMapping("alipayCallback")
    public Object alipayCallback(HttpServletRequest request) {
        log.info("这里是支付宝回调");
        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决,这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        log.info(params.toString());
        String orderSn = params.get("out_trade_no");
        C3mOrder userOrder = userOrderService.getByOrderSn(orderSn);
//        Long poleId = userOrder.getPoleId();
        AliConfigEntity aliConfig = alipayConfigService.getConfigByPoleId(userOrder.getPoleId());
        //切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        boolean flag = false;
        try {
            flag = AlipaySignature.rsaCheckV1(params, aliConfig.getPublicKey(), "UTF-8", "RSA2");
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (flag) {
            if (!"TRADE_SUCCESS".equals(params.get("trade_status"))) {
                return "success";
            }
 
            if (userOrder == null) {
                return "订单不存在";
            }
            // 检查这个订单是否已经处理过
            if (!OrderStatusEnums.UNPAID.getCode().equals(userOrder.getOrderStatus())) {
                return WxPayNotifyResponse.success("订单已经处理成功!");
            }
            String transactionId = params.get("out_trade_no");
            C3mOrder update = new C3mOrder();
            update.setOrderId(userOrder.getOrderId());
            update.setOutTradeNo(transactionId);
            update.setPayTimestamp(new Date().getTime());
            update.setOrderStatus(OrderStatusEnums.PAID.getCode());
 
            if (!userOrderService.updateById(update)) {
                return WxPayNotifyResponse.fail("更新数据已失效");
            }
 
//            UserPayRecord userPayRecord = new UserPayRecord();
//            userPayRecord.setOrderId(userOrder.getId());
//            userPayRecord.setPayAmount(userOrder.getTotalPrice());
//            userPayRecord.setPayDate(userOrder.getPayTime());
//            userPayRecord.setPayType(PayTypeEnums.ALIPAY.getCode());
//            userPayRecord.setUserId(userOrder.getUserId());
//            userPayRecordService.save(userPayRecord);
            return "success";
        }
        return null;
    }
}