package org.jeecg.modules.pay.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@RequiredArgsConstructor
@ConditionalOnClass(WxPayService.class)
public class WxPayConfiguration {

    @Resource
    private WxPay wxPay;

    @Bean
    public WxPayService wxMpPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxPay.appId);
        payConfig.setMchId(wxPay.mchId);
        payConfig.setMchKey(wxPay.mchKey);
        payConfig.setKeyPath(wxPay.keyPath);
        payConfig.setSignType(WxPayConstants.SignType.MD5);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

//    @Bean("saboxWxPayService")
//    public WxPayService saboxWxPayService(){
//        WxPayConfig payConfig = new WxPayConfig();
//        WxPayService wxPayService = new WxPayServiceImpl();
//        String sandboxSignKey = null;
//        try {
//            wxPayService.setConfig(payConfig);
//            sandboxSignKey = wxPayService.getSandboxSignKey();
//        } catch (WxPayException e) {
//            e.printStackTrace();
//        }
//        payConfig.setUseSandboxEnv(true);
//        payConfig.setMchKey(sandboxSignKey);
//        wxPayService.setConfig(payConfig);
//        return wxPayService;
//    }

}