package com.leromro.framework.config;

import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.leromro.framework.config.properties.WxPayProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
public class WxPayConfig {


  @Bean(name = "wxMiniPayService")
  public WxPayService getWxMiniPayService(WxPayProperties properties) {
    com.github.binarywang.wxpay.config.WxPayConfig payConfig = new com.github.binarywang.wxpay.config.WxPayConfig();
    payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
    payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
    payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
    payConfig.setNotifyUrl(StringUtils.trimToNull(properties.getNotifyUrl()));
    payConfig.setTradeType(StringUtils.trimToNull(properties.getTradeType()));
    payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));

    // 可以指定是否使用沙箱环境
    payConfig.setUseSandboxEnv(false);

    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
  }

  @Bean(name = "wxMiniPayRefundService")
  public WxPayService getWxMiniPayRefundService(WxPayProperties properties) {
    com.github.binarywang.wxpay.config.WxPayConfig payConfig = new com.github.binarywang.wxpay.config.WxPayConfig();
    payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
    payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
    payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
    payConfig.setNotifyUrl(StringUtils.trimToNull(properties.getRefundNotifyUrl()));
    payConfig.setTradeType(StringUtils.trimToNull(properties.getTradeType()));
    payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));

    // 可以指定是否使用沙箱环境
    payConfig.setUseSandboxEnv(false);

    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
  }

}