package com.miaxis.common.config;

import com.miaxis.common.utils.uuid.CommonUtils;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import org.apache.http.client.HttpClient;
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 BeanConfig {


    @Autowired
    private WxpayConfig wxpayConfig;
    @Autowired
    private AutoUpdateCertificatesVerifier verifier;

    /**
     * 腾讯云 COS
     * @return
     */




    /**
     * 微信支付 httpclient 对象
     * @return
     * @throws Exception
     */
    @Bean
    public HttpClient wxpayConfigBean() throws Exception{
        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
        String realPath =file.getAbsolutePath();
        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
        // 初始化httpClient
        HttpClient httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(wxpayConfig.getMerchantId(), wxpayConfig.getSerialNumber(), privateKey)
                .withValidator(new WechatPay2Validator(verifier)).build();
        return httpClient;

    }




}
