package com.cpop.pay.framewok.config.wxPay;

import com.cpop.common.utils.StringUtils;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author DB
 * @Description:
 * @create 2023-10-26 22:38
 */
@Getter
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {

    private WxPayProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        //服务商模式微信支付
        payConfig.setApiV3Key(this.properties.getApiV3Key());
        payConfig.setCertSerialNo(this.properties.getCertSerialNo());
        payConfig.setKeyPath(this.properties.getKeyPath());
        payConfig.setPrivateKeyPath(this.properties.getPrivateKeyPath());
        payConfig.setPrivateCertPath(this.properties.getPrivateCertPath());
        //通知地址
        payConfig.setNotifyUrl(this.properties.getNotifyUrl());
        payConfig.setServiceId(this.properties.getServiceId());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
