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

import com.cpop.core.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;

    /**
     * 获取普通微信支付
     * @author DB
     * @since 2024/3/13
     * @return WxPayService
     */
    @Bean("normalWxPayService")
    public WxPayService normalWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getBackstageAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getBackstageMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getBackstageMchKey()));
        payConfig.setKeyPath(properties.getBackstageKeyPath());
        payConfig.setNotifyUrl(properties.getBackstagePayNotifyUrl());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    /**
     * 获取普通微信支付
     * @author DB
     * @since 2024/3/13
     * @return WxPayService
     */
    @Bean("serviceWxPayService")
    public WxPayService serviceWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getServiceAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getServiceMchId()));
        payConfig.setPrivateKeyPath(properties.getServicePrivateKeyPath());
        payConfig.setPrivateCertPath(properties.getServicePrivateCertPath());
        payConfig.setApiV3Key(StringUtils.trimToNull(properties.getServiceApiV3Key()));
        payConfig.setServiceId(properties.getServiceId());
        payConfig.setNotifyUrl(properties.getServicePayNotifyUrl());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
