package com.nova.sankuai.domain.api.weiyaquanyi;
|
|
import cn.hutool.http.HttpRequest;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.nova.sankuai.domain.entity.VipCardOrder;
|
import com.nova.sankuai.domain.vo.vipcard.VipCardVo;
|
import lombok.AllArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
@Slf4j
|
@Component
|
@AllArgsConstructor
|
public class WeiYaUtil {
|
|
private final WeiYaConfigProperties properties;
|
private static final Logger weiYaPayLogger = LoggerFactory.getLogger("weiYaPayLogger");
|
|
public String generateParamForGetChannelData() {
|
HashMap<Object, Object> map = new HashMap<>();
|
map.put("channelCode", properties.getChannelCode());
|
return JSON.toJSONString(map);
|
}
|
|
public List<VipCardVo> transferToVipCardData(WeiYaGetChannelDataResp resData) {
|
return resData.getResult().stream().map(res -> {
|
VipCardVo card = new VipCardVo();
|
card.setCardId(res.getId());
|
int price = res.getPrice().multiply(BigDecimal.valueOf(100L)).intValue();
|
card.setMarkedPrice(price);
|
card.setSellingPrice(price);
|
card.setName(res.getName());
|
// FIXME 是这样吗
|
return card;
|
}).collect(Collectors.toList());
|
}
|
|
public String memberPayUrl(String channelUserId, String phone, String returnUrl, String cardId, String orderId) {
|
HashMap<Object, Object> map = new HashMap<>();
|
map.put("channelCode", properties.getChannelCode());
|
map.put("channelUserId", channelUserId);
|
map.put("phone", phone);
|
map.put("sankuaiOrderId", orderId);
|
map.put("returnUrl", returnUrl);
|
map.put("notifyUrl", properties.getNotifyUrl());
|
String plainTextData = JSON.toJSONString(map);
|
log.info("生成纬雅支付URL中,map={}", map);
|
String encrypt = WeiYaKeyUtil.encryptAES(plainTextData, properties.getAesSecretKey());
|
return String.format("%s?id=%s¶m=%s", properties.getMemberPayUrl(), cardId, encrypt);
|
}
|
|
public WeiYaUserGetRespVO userGet(VipCardOrder order) {
|
if (order.getUserId() == null) {
|
log.error("用户ID为null!");
|
}
|
String body = HttpRequest.post(properties.getUserGetUrl())
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
.body(JSON.toJSONString(new HashMap<String, String>() {{
|
put("channelCode", "ld");
|
put("channelUserId", order.getUserId().toString());
|
}}))
|
.execute().body();
|
JSONObject respObj = JSON.parseObject(body);
|
if (Objects.equals(respObj.getInteger("code"), 0)) {
|
return respObj.getObject("result", WeiYaUserGetRespVO.class);
|
}
|
return null;
|
}
|
|
/**
|
* 渠道商用户开通会员卡回调
|
*
|
* @param userId 渠道侧会员id
|
* @param channelMemberId 渠道侧会员卡id
|
*/
|
public WeiYaRespVO channelMemberNotify(String userId, String channelMemberId) {
|
String body = HttpRequest.post(properties.getChannelMemberNotifyUrl())
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
.body(JSON.toJSONString(new HashMap<String, String>(16) {{
|
put("channelCode", properties.getChannelCode());
|
put("userId", userId);
|
put("channelMemberId", channelMemberId);
|
}}))
|
.execute().body();
|
weiYaPayLogger.info("渠道商用户开通会员卡回调 -> {}", body);
|
try {
|
return JSON.parseObject(body, WeiYaRespVO.class);
|
} catch (Exception e) {
|
weiYaPayLogger.info("渠道商用户开通会员卡回调返回值序列化失败");
|
return null;
|
}
|
|
}
|
}
|