package com.miaxis.common.config;

import com.miaxis.common.utils.uuid.CommonUtils;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.security.PrivateKey;


@Configuration
public class WxVerifierConfig {


    @Autowired
    private WxpayConfig wxpayConfig;

    @Bean
    public AutoUpdateCertificatesVerifier getVerifier() throws Exception{
        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
        String realPath =file.getAbsolutePath();
        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
        // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(wxpayConfig.getMerchantId(),
                        new PrivateKeySigner(wxpayConfig.getSerialNumber(), privateKey)),
                wxpayConfig.getV3key().getBytes("utf-8"));
        return verifier;

    }

}
