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;
|
}
|
}
|