yeg
2026-09-16 87eca64eeeec679933b7686111396a8f35951b41
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
package com.talentcard.server.business.payment.huifu;
 
import com.alibaba.fastjson.JSON;
import com.huifu.bspay.sdk.opps.client.BasePayClient;
import com.huifu.bspay.sdk.opps.core.BasePay;
import com.huifu.bspay.sdk.opps.core.config.MerConfig;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Service;
 
/** External boundary for manual recovery of an existing Huifu merchant. */
@Service
public class HuifuOnboardingProgressGateway {
    private final HuifuPaymentProperties properties;
    public HuifuOnboardingProgressGateway(HuifuPaymentProperties properties) { this.properties = properties; }
 
    public Map<String,Object> queryApplication(String huifuId, String applyNo) {
        Map<String,Object> request = queryRequest(huifuId);
        request.put("apply_no", applyNo);
        return send("v2.merchant.basicdata.status.query", request);
    }
 
    public Map<String,Object> queryCapabilities(String huifuId) {
        return send("v2.merchant.basicdata.query", queryRequest(huifuId));
    }
 
    public Map<String,Object> queryWechatConfig(String huifuId) {
        return send("v2.merchant.busi.config.query", queryRequest(huifuId));
    }
 
    public Map<String,Object> queryRealname(String huifuId) {
        Map<String,Object> request = queryRequest(huifuId);
        request.put("pay_scene", "01");
        return send("v2.merchant.busi.realname.query", request);
    }
 
    public Map<String,Object> configureDelay(String huifuId, String requestNo, String requestDate) {
        Map<String,Object> request = new LinkedHashMap<>(Map.of("huifu_id",huifuId,"req_seq_id",requestNo,"req_date",requestDate,
                "delay_flag","Y","forced_delay_flag","N"));
        if (!properties.getMerchantBusinessOpenNotifyUrl().isBlank()) request.put("async_return_url",properties.getMerchantBusinessOpenNotifyUrl());
        return send("v2.merchant.busi.modify", request);
    }
 
    protected Map<String,Object> send(String function, Map<String,Object> request) {
        if (!properties.isEnabled()) throw new BusinessException(ErrorCode.CONFLICT, "Huifu payment is disabled");
        try {
            synchronized (BasePay.class) {
                BasePay.debug = false;
                BasePay.prodMode = "prod".equalsIgnoreCase(properties.getEnvironment()) ? BasePay.MODE_PROD : BasePay.MODE_TEST;
                MerConfig config = new MerConfig();
                config.setSysId(properties.getSystemId());
                config.setProductId(properties.getProductId());
                config.setRsaPrivateKey(properties.getPrivateKey());
                config.setRsaPublicKey(properties.getPlatformPublicKey());
                BasePay.initWithMerConfig(config);
                return data(BasePayClient.request(request, function, false));
            }
        } catch (BusinessException exception) { throw exception; }
        catch (Exception exception) { throw new HuifuPaymentGatewayException("Huifu request outcome is unknown", true, exception); }
    }
 
    static Map<String,Object> queryRequest(String huifuId) {
        Map<String,Object> request = new LinkedHashMap<>();
        request.put("req_seq_id", UUID.randomUUID().toString().replace("-", ""));
        request.put("req_date", LocalDate.now(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.BASIC_ISO_DATE));
        request.put("huifu_id", huifuId);
        return request;
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String,Object> data(Map<String,Object> response) {
        if (response == null) throw new IllegalArgumentException("Empty Huifu response");
        Object nested = response.get("data");
        if (nested instanceof Map<?,?> value) return (Map<String,Object>) value;
        if (nested instanceof String value && !value.isBlank()) return JSON.parseObject(value, Map.class);
        return response;
    }
}