package com.jeequan.jeepay.thirdparty.channel.aliaqf;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alipay.api.internal.util.AlipaySignature;
import com.jeequan.jeepay.core.model.params.aliaqf.AliaqfIsvParams;
import com.jeequan.jeepay.core.model.params.alipay.AlipayConfig;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.util.ChannelCertConfigKitBean;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 安全发的一些通用请求，签名等
 *
 * @author deng
 * @since 2024/5/9
 */
@Slf4j
public class AliaqfKit {

    // alipay.fund.trans.uni.transfer 涉及的状态：SUCCESS、FAIL、DEALING、REFUND
    public static final String ORDER_SUCCESS = "SUCCESS";
    /** 处理中 */
    public static final String ORDER_DEALING = "DEALING";
    /** 退票 */
    public static final String ORDER_REFUND = "REFUND";
    /** 失败 */
    public static final String ORDER_FAIL = "FAIL";

    public static boolean checkSign(JSONObject reqParamJSON, AliaqfIsvParams isvParams) {
        try {
            TypeReference<Map<String, String>> mapTypeReference = new TypeReference<Map<String, String>>() {
            };
            ChannelCertConfigKitBean channelCertConfigKitBean = SpringBeansUtil.getBean(ChannelCertConfigKitBean.class);
            return AlipaySignature.rsaCertCheckV1(reqParamJSON.toJavaObject(mapTypeReference), channelCertConfigKitBean.getCertFilePath(isvParams.getAlipayPublicCert()),
                    AlipayConfig.CHARSET, "RSA2");
        } catch (Exception e) {
            log.info("验签失败", e);
        }

        return false;
    }
}
