package com.seer.teach.common.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author: Captain
 * @Description: 微信小程序配置类
 * @Date: 2025-05-09 20:52
 */
@Component
public class WxConfig implements InitializingBean {
    @Value("${wx.appId:}")
    private String appId;

    @Value("${wx.secret:}")
    private String secret;

    @Value("${wx.getAccessTokenUrl:}")
    private String getAccessTokenUrl;

    @Value("${wx.getQrCodeUrl:}")
    private String getQrCodeUrl;

    @Value("${wx.redisAccessKey:}")
    private String redisAccessKey;

    @Value("${wx.wxLoginUrl:}")
    private String wxLoginUrl;

    /**
     *  商户号
     *  sp_mchid是服务商在微信支付侧的唯一身份标识，所有接口调用都必须包含此参数，以便微信支付确认商户的身份。
     *  当入驻审核成功后，微信支付侧会向商户提供该商户号。开发者需与负责申请商户号的同事联系获取，
     *  具体操作如下：登录服务商平台，点击【账户中心->个人设置-->个人信息】即可查看服务商商户号。
     */
    @Value("${wx.mchid:}")
    private String mchid;

    /**
     *  商户私钥文件路径
     */
    @Value("${wx.privateKeyPath:}")
    private String privateKeyPath;

    /**
     *  商户证书序列号
     */
    @Value("${wx.merchant-serial-number:}")
    private String merchantSerialNumber;

    /**
     * 商户APIV3密钥
     */
    @Value("${wx.apiV3Key:}")
    private String apiV3Key;

    /**
     * 微信支付异步通知地址
     */
    @Value("${wx.js-pay-notify-url:}")
    private String jsPayNotifyUrl;

    /**
     * 微信支付退款异步通知地址
     */
    @Value("${wx.js-refund-notify-url:}")
    private String refundNotifyUrl;

    /**
     * 微信支付公钥id
     */
    @Value("${wx.public-key-id:PUB_KEY_ID_0117240729862025081100212138000608}")
    private String publicKeyId;

    @Value("${wx.public-key-path:certs/pub_key.pem}")
    private String publicKeyPath;
    /**
     * 定义公开静态常量
     */
    public static String APP_ID;
    public static String SECRET;
    public static String GET_ACCESS_TOKEN_URL;
    public static String GET_QR_CODE_URL;
    public static String REDIS_ACCESS_KEY;
    public static String WX_LOGIN_URL;

    /**
     * 支付相关
     */
    public static String SP_MCHID;
    public static String PRIVATE_KEY_PATH;
    public static String MERCHANT_SERIAL_NUMBER;
    public static String API_V3_KEY;
    // 小程序支付回调url
    public static String PAY_NOTIFY_URL;
    // 小程序退款回调url
    public static String REFUND_NOTIFY_URL;

    public static String PUBLIC_KEY_ID;

    public static String PUBLIC_KEY_PATH;

    @Override
    public void afterPropertiesSet() throws Exception {
        APP_ID = appId;
        SECRET = secret;
        GET_ACCESS_TOKEN_URL = getAccessTokenUrl+ "?appid="+APP_ID+"&secret="+SECRET+"&grant_type=client_credential";
        GET_QR_CODE_URL = getQrCodeUrl;
        REDIS_ACCESS_KEY = redisAccessKey;
        WX_LOGIN_URL = wxLoginUrl + "?appid="+APP_ID+"&secret="+SECRET+"&grant_type=authorization_code"+"&js_code=";
        SP_MCHID = mchid;
        PRIVATE_KEY_PATH = privateKeyPath;
        MERCHANT_SERIAL_NUMBER = merchantSerialNumber;
        API_V3_KEY = apiV3Key;
        PAY_NOTIFY_URL = jsPayNotifyUrl;
        REFUND_NOTIFY_URL = refundNotifyUrl;
        PUBLIC_KEY_ID = publicKeyId;
        PUBLIC_KEY_PATH = publicKeyPath;
    }
}
