ab
2024-11-04 09d882262f530ded672f1c01fb65a1fefa00d52d
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
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&param=%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;
        }
 
    }
}