admin
2019-11-06 eed89d185cc54187353b03966296498b8495c442
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
package com.yeshi.fanli.controller;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.SignatureException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.wx.WXUtil;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.google.gson.Gson;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg;
import com.yeshi.fanli.dto.push.PushContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgDeviceReadState;
import com.yeshi.fanli.entity.config.push.PushMsgFactory;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.MsgDeviceReadStateService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.shop.BanLiShopOrderUtil;
import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
 
import net.sf.json.JSONObject;
 
/**
 * 
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("client/v1/callback")
public class CallBackController {
    @Resource
    private ConfigService configService;
 
    @Resource
    private PushService pushService;
 
    @Resource
    private MsgDeviceReadStateService msgDeviceReadStateService;
 
    @Resource(name = "producer")
    private Producer producer;
 
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    /**
     * 客服消息回调
     * 
     * @param response
     */
    @RequestMapping(value = "kefuMsg")
    public void kefuMsg(HttpServletResponse response) {
 
    }
 
    /**
     * 美洽消息回调
     * 
     * @param response
     */
 
    @RequestMapping(value = "meiQia")
    public void meiQia(HttpServletRequest request, HttpServletResponse response) {
        String auth = request.getHeader("Authorization");
        String queryString = request.getQueryString();
        LogHelper.test("美洽:queryString-" + queryString + "-auth:" + auth);
 
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        LogHelper.test("美洽:body----" + sb.toString());
 
        String sign = "";
        try {
            sign = sign(sb.toString());
        } catch (SignatureException e) {
            e.printStackTrace();
        }
 
        if (!auth.equalsIgnoreCase(sign)) {
            LogHelper.test("美洽回调签名错误");
            return;
        }
        JSONObject json = JSONObject.fromObject(sb.toString());
        String msg = "";
        if (json != null) {
            String deviceOS = json.optString("deviceOS");
            String contentType = json.optString("contentType");
            if (contentType.equalsIgnoreCase("text"))
                msg = json.optString("content");
            else if (contentType.equalsIgnoreCase("photo"))
                msg = "[图片]";
            else if (contentType.equalsIgnoreCase("audio"))
                msg = "[语音]";
            String customizedId = json.optJSONObject("customizedData").optString("设备标识");
            String uid = json.optJSONObject("customizedData").optString("用户ID");
            msgDeviceReadStateService.addUnreadDeviceMsg(MsgDeviceReadState.TYPE_KEFU, customizedId,
                    "android".equalsIgnoreCase(deviceOS) ? 1 : 2, 1, msg, new Date());
            if (!StringUtil.isNullOrEmpty(uid))// 推送客服消息
            {
                PushContentDTO dto = PushMsgFactory.createMsgKefu();
                try {
                    pushService.pushZNX(Long.parseLong(uid), dto.getTitle(), dto.getContent(), null, null);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (PushException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
    public String sign(String raw_body) throws java.security.SignatureException {
        String key = "$2a$12$uC3EG/zSaSI37KKOgt1IgetDRHJY6Q2zEVDBr0DeWcwQbGNU7pewy";
        String result = "";
        try {
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(raw_body.getBytes("UTF-8"));
            byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(rawHmac);
            result = org.apache.commons.codec.binary.Base64.encodeBase64String(hexBytes).trim();
        } catch (Exception e) {
            throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
        }
        return "meiqia_sign:" + result;
    }
 
    @RequestMapping(value = "test")
    public void test(PrintWriter out) {
        configService.getConfig(1L);
        out.print("success");
    }
 
    @RequestMapping(value = "vip")
    public void VIP(PrintWriter out) {
        out.print("success");
    }
 
    @RequestMapping(value = "banlishop/pay")
    public void banLiShopPay(HttpServletRequest request, PrintWriter out) {
 
        Map<String, String> map = WXUtil.parseXML(request);
        JSONObject json = JSONObject.fromObject(map);
        LogHelper.test("商城微信支付回调:" + json.toString());
        String sign = map.get("sign");
        map.remove("sign");
        boolean isSuccess = BanLiShopWXPayUtil.signIsRight(map, sign);
        if (isSuccess)
            if (map.get("return_code").equalsIgnoreCase("SUCCESS")
                    && map.get("result_code").equalsIgnoreCase("SUCCESS")) {
                // 商户订单号
                String wxPayOrderNo = map.get("out_trade_no");
                // 订单号
                String orderNo = BanLiShopOrderUtil.getOrderNoFromWXPayOrderNo(wxPayOrderNo);
                if (!StringUtil.isNullOrEmpty(orderNo)) {
                    BanLiShopOrder order = banLiShopOrderService.selectByOrderNo(orderNo);
                    if (order != null) {// 支付成功消息推送
                        BanLiShopOrderMQMsg msg = new BanLiShopOrderMQMsg(order.getId(), order.getUid());
                        Message message = new Message(MQTopicName.TOPIC_ORDER.name(),
                                OrderTopicTagEnum.banLiShopOrderPaid.name(), new Gson().toJson(msg).getBytes());
                        producer.send(message);
                        Map<String, String> returnMap = new HashMap<>();
                        returnMap.put("return_code", "SUCCESS");
                        returnMap.put("return_msg", "OK");
                        out.print(WXUtil.loadWXMessage(returnMap));
                        out.close();
                    }
                }
            }
    }
 
}