package com.cf.imes.framework.pay.config;

import com.cf.imes.framework.common.util.encrypt.AesUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author Gqr
 * @since 2025/4/15 16:24
 */
@ConfigurationProperties(prefix = "chenfeng.pay.alipay")
@Data
public class ChenfengAlipayConfig extends com.alipay.api.AlipayConfig {
    @Value("${chenfeng.encrypt.publicKey:}")
    private String publicKey;

    /**
     * 销售产品码
     */
    private String productCode = "FAST_INSTANT_TRADE_PAY";

    /**
     * 二维码模式：效果参见：https://help.pingxx.com/article/1137360/
     */
    private String qrPayMode = "1";

    /**
     * 给支付宝的回调地址
     */
    private String notifyUrl;

    // 加密数据

    @Override
    public void setAppId(String appId) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setAppId(AesUtils.decrypt(appId, publicKey));
        } else {
            super.setAppId(appId);
        }
    }

    @Override
    public void setPrivateKey(String privateKey) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setPrivateKey(AesUtils.decrypt(privateKey, publicKey));
        } else {
            super.setPrivateKey(privateKey);
        }
    }

    @Override
    public void setAlipayPublicKey(String alipayPublicKey) {
        if (StringUtils.isNotEmpty(publicKey)) {
            super.setAlipayPublicKey(AesUtils.decrypt(alipayPublicKey, publicKey));
        } else {
            super.setAlipayPublicKey(alipayPublicKey);
        }
    }

    public void setNotifyUrl(String notifyUrl) {
        if (StringUtils.isNotEmpty(publicKey)) {
            this.notifyUrl = AesUtils.decrypt(notifyUrl, publicKey);
        } else {
            this.notifyUrl = notifyUrl;
        }
    }
}
