1
sunshine
2024-11-05 92a9e53e82c74d36a72eb92f93777bbb16e2db73
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
package com.nova.sankuai.domain.api.liexiong;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nova.sankuai.domain.entity.CustomerUser;
import com.nova.sankuai.domain.vo.vipcard.VipCardVo;
import com.nova.sankuai.infra.config.CommonException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
 
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author weikangdi
 * @create 2021/12/22
 */
@Component
@ApiModel(value = "烈熊对接接口工具类")
@Slf4j
public class LieXiongUtil {
 
    @Value("${jieyidai.appId}")
    @ApiModelProperty(value = "对接用ApiId")
    private String appId;
 
    @Value("${jieyidai.appKey}")
    @ApiModelProperty(value = "对接用ApiKey")
    private String appKey;
 
    @Value("${jieyidai.innerUrl}")
    @ApiModelProperty(value = "内部回调域名")
    private String innerUrl;
 
    @Value("${jieyidai.apiUrl}")
    @ApiModelProperty(value = "对接API域名")
    private String apiUrl;
 
    /**
     * 获取accessToken
     */
    public String getAccessToken() {
        String secret = Base64.getEncoder().encodeToString(DigestUtils.sha256(appId + appKey));
        String param = "appId=" + appId + "&secret=" + secret;
        String params = HttpUtil.encodeParams(param, Charset.forName("utf-8"));
        log.info("获取accessToken,传递参数" + param);
        HttpResponse response = HttpRequest.post(apiUrl + "/secret/authorize")
                .contentType("application/x-www-form-urlencoded")
                .body(params).execute();
        if (response.getStatus() != HttpStatus.HTTP_OK) {
            log.error("获取戒易贷accessToken失败,调用结果:" + response.body());
            throw new CommonException("获取戒易贷accessToken失败");
        }
        log.info("获取accessToken,返回参数" + response.body());
        JSONObject parseObj = JSONUtil.parseObj(response.body());
        return parseObj.getStr("accessToken");
    }
 
    /**
     * 获取用户Token
     */
    public String getUserToken(String phone, String userId, String accessToken) {
        String param = "phone=" + phone + "&userId=" + userId;
        String params = HttpUtil.encodeParams(param, Charset.forName("utf-8"));
        log.info("获取用户Token,传递参数" + param);
        HttpResponse response = HttpRequest.post(apiUrl + "/user/login")
                .contentType("application/x-www-form-urlencoded")
                .header("authorization", accessToken)
                .body(params).execute();
        if (response.getStatus() != HttpStatus.HTTP_OK) {
            log.error("获取戒易贷用户Token失败,调用结果:" + response.body());
            throw new CommonException("获取用户Token失败");
        }
        log.info("获取用户Token,返回参数" + response.body());
        JSONObject parseObj = JSONUtil.parseObj(response.body());
        return parseObj.getStr("token");
    }
 
    /**
     * 获取会员卡列表
     */
    public List<LieXiongCard> getCardList(String accessToken) {
        log.info("获取会员卡列表,传递参数" + accessToken);
        HttpResponse response = HttpRequest.get(apiUrl + "/card/vipCard")
                .contentType("application/x-www-form-urlencoded")
                .header("authorization", accessToken)
                .execute();
        if (response.getStatus() != HttpStatus.HTTP_OK) {
            log.error("获取戒易贷会员卡列表失败,调用结果:" + response.body());
            throw new CommonException("获取会员卡列表失败");
        }
        log.info("获取会员卡列表,返回参数" + response.body());
        JSONArray array = JSONUtil.parseArray(response.body());
        List<LieXiongCard> cardVoList = JSONUtil.toList(array, LieXiongCard.class);
        return cardVoList;
    }
 
 
    /**
     * 获取戒易贷购买会员订单链接
     */
    public LieXiongParam buyCard(VipCardVo cardVo, String accessToken, String userToken) {
        Map<String, Object> params = new HashMap<>();
        params.put("cardId", cardVo.getCardId());
        params.put("price", cardVo.getSellingPrice());
        if (StringUtils.isEmpty(cardVo.getPartnerThirdOrderId())) {
            cardVo.setPartnerThirdOrderId(IdUtil.randomUUID());
        }
        params.put("partnerThirdOrderId", cardVo.getPartnerThirdOrderId());
        params.put("notificationUrl", innerUrl + "/api/v1/vipCardOrder/notifyOrder");
        if (StringUtils.isNotEmpty(cardVo.getUrl())) {
            params.put("successUrl", cardVo.getUrl());
        }
        log.info("获取戒易贷购买会员订单链接,传递参数" + params.toString());
        HttpResponse response = HttpRequest.post(apiUrl + "/card/buyCard")
                .contentType("application/x-www-form-urlencoded")
                .header("authorization", accessToken)
                .header("token", userToken)
                .form(params)
                .execute();
        if (response.getStatus() != HttpStatus.HTTP_OK) {
            log.error("获取戒易贷购买会员订单链接失败,调用结果:" + response.body());
            throw new CommonException("获取购买会员订单链接失败");
        }
        log.info("获取戒易贷购买会员订单链接,返回参数" + response.body());
        LieXiongParam lieXiongParam = JSONUtil.toBean(response.body(), LieXiongParam.class);
        lieXiongParam.setPartnerThirdOrderId(cardVo.getPartnerThirdOrderId());
        return lieXiongParam;
    }
 
 
    /**
     * 校验支付回调参数签名是否正确
     */
    public boolean checkNotifySign(MultiValueMap<String, String> formData) {
        List<String> signParam = formData.get("sign");
        String signValue = "";
        if (signParam != null) {
            signValue = signParam.get(0);
        }
        formData.remove("sign");
        String signStr = formData.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .map(Map.Entry::getValue)
                .filter(l -> !l.isEmpty())
                .map(l -> l.get(0))
                .filter(StringUtils::isNotEmpty)
                .collect(Collectors.joining("", appKey, ""));
        String sign = Base64.getEncoder().encodeToString(DigestUtils.sha256(signStr));
        if (sign.equals(signValue)) {
            return true;
        }
        return false;
    }
 
    /**
     * 查询戒易贷购买会员订单状态
     */
    public OrderVo getOrderStatus(String partnerThirdOrderId, String accessToken) {
        log.info("查询戒易贷购买会员订单状态,传递参数" + partnerThirdOrderId);
        HttpResponse response = HttpRequest.get(apiUrl + "/card/payOrder/partnerThirdOrderId/" + partnerThirdOrderId)
                .contentType("application/x-www-form-urlencoded")
                .header("authorization", accessToken)
                .execute();
        if (response.getStatus() != HttpStatus.HTTP_OK) {
            log.error("查询戒易贷购买会员订单状态失败,调用结果:" + response.body());
            throw new CommonException("查询戒易贷购买会员订单状态失败");
        }
        log.info("查询戒易贷购买会员订单状态,返回参数" + response.body());
        OrderVo orderVo = JSONUtil.toBean(response.body(), OrderVo.class);
        return orderVo;
    }
 
    /**
     * 检测会员卡是否在有效期内
     *
     * @param customerUser
     * @return
     */
    public boolean checkVipCardStatus(CustomerUser customerUser) {
        try {
            String accessToken = getAccessToken();
            String userToken = getUserToken(customerUser.getPhone(), customerUser.getId().toString(), accessToken);
            HttpResponse response = HttpRequest.get(apiUrl + "/card/userVipCardAll")
                    .contentType("application/x-www-form-urlencoded")
                    .header("authorization", accessToken)
                    .header("token", userToken)
                    .execute();
            if (response.getStatus() == HttpStatus.HTTP_OK) {
                JSONArray array = JSONUtil.parseArray(response.body());
                if (array.size() > 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            log.error("烈熊检测会员有效状态失败" + e);
        }
        return false;
    }
}