Pu Zhibing
1 天以前 878ac7cc8e1951bbc7b27619c4e7ece1e3d331ff
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
package com.ruoyi.order.util.kuaishou;
 
import com.kuaishou.locallife.open.api.KsLocalLifeApiException;
import com.kuaishou.locallife.open.api.client.KsLocalLifeAccessTokenClient;
import com.kuaishou.locallife.open.api.common.utils.GsonUtils;
import com.kuaishou.locallife.open.api.domain.locallife_trade.VerifyPrepareDataNew;
import com.kuaishou.locallife.open.api.domain.locallife_trade.VerifyResult;
import com.kuaishou.locallife.open.api.request.locallife_trade.GoodlifeV1FulfilmentCertificatePrepareRequest;
import com.kuaishou.locallife.open.api.request.locallife_trade.GoodlifeV1FulfilmentCertificateVerifyRequest;
import com.kuaishou.locallife.open.api.response.locallife_trade.GoodlifeV1FulfilmentCertificatePrepareResponse;
import com.kuaishou.locallife.open.api.response.locallife_trade.GoodlifeV1FulfilmentCertificateVerifyResponse;
import com.ruoyi.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
 
/**
 * @author zhibing.pu
 * @Date 2025/6/16 10:28
 */
@Slf4j
public class VerifyUtil {
    
    
    /**
     * 验券准备
     * @param code  短链地址
     * @return
     */
    public static VerifyPrepareDataNew certificatePrepare(RedisService redisService, String code){
        //判断token是否过期
        Object ks_access_token = redisService.getCacheObject("ks_access_token");
        if(null == ks_access_token){
            //刷新token
            ClientTokenUtil.refreshToken(redisService);
            ks_access_token = redisService.getCacheObject("ks_access_token");
        }
        String access_token = ks_access_token.toString();
        KsLocalLifeAccessTokenClient client = KsLocalLifeAccessTokenClient.Builder.newBuilder()
                .setAccessToken(access_token)
                .build();
        //截取链接中的检验码
        code = code.substring(code.lastIndexOf("/") + 1);
        GoodlifeV1FulfilmentCertificatePrepareRequest request = new GoodlifeV1FulfilmentCertificatePrepareRequest();
        request.setEncrypted_short_code(code);
        
        try {
            GoodlifeV1FulfilmentCertificatePrepareResponse response = client.execute(request);
            log.info("【快手】验券准备返回结果:{}", GsonUtils.toJSON(response));
            VerifyPrepareDataNew data = response.getData();
            if(0 != data.getError_code()){
                log.error("【快手】验券准备失败");
                return null;
            }
            return data;
        } catch (KsLocalLifeApiException e) {
            log.error("【快手】验券准备失败");
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 验券
     * @param poiId 快手门店id
     * @param encryptedCodes    加密券码
     * @param encryptedCodes    验券准备接口返回的加密快手券码
     * @param order_id          快手订单号
     * @return
     */
    public static List<VerifyResult> certificateVerify(RedisService redisService, String verify_token, String poiId, List<String> encryptedCodes, String order_id){
        //判断token是否过期
        Object ks_access_token = redisService.getCacheObject("ks_access_token");
        if(null == ks_access_token){
            //刷新token
            ClientTokenUtil.refreshToken(redisService);
            ks_access_token = redisService.getCacheObject("ks_access_token");
        }
        String access_token = ks_access_token.toString();
        KsLocalLifeAccessTokenClient client = KsLocalLifeAccessTokenClient.Builder.newBuilder()
                .setAccessToken(access_token)
                .build();
        
        GoodlifeV1FulfilmentCertificateVerifyRequest request = new GoodlifeV1FulfilmentCertificateVerifyRequest();
        
        request.setVerify_token(verify_token);
        request.setPoi_id(poiId);
        request.setEncrypted_codes(encryptedCodes);
        request.setOrder_id(order_id);
        try {
            GoodlifeV1FulfilmentCertificateVerifyResponse response = client.execute(request);
            log.info("【快手】验券返回结果:{}", GsonUtils.toJSON(response));
            if(0 != response.getData().getError_code()){
                log.error("【快手】验券失败");
                return null;
            }
            /**
             * result  验券结果码,0表示成功,非0表示失败
             * msg  验券结果说明
             * code 代表验券传入的code或encrypted_code
             * verify_id 代表券码一次核销的标识(撤销时需要)
             * certificate_id 代表一张券码的标识(撤销时需要)
             * origin_code 原始券码
             * account_id 代表商家总店id(查询验券历史时需要)
             * is_abnormal 验券结果通知 true 异常 false 非异常
             */
            return response.getData().getVerify_results();
        } catch (KsLocalLifeApiException e) {
            log.error("【快手】验券失败");
            e.printStackTrace();
        }
        return null;
    }
}