package com.fjs.scenic.config;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
 
@Component
public class WeChatMpConfig {
 
    @Autowired
    private WechatAccountConfig wechatAccountConfig;
 
    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
 
    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
        wxMpConfigStorage.setToken(wechatAccountConfig.getToken());
        return wxMpConfigStorage;
    }
    
    @Bean
    public WXPay wxPay(WechatAccountConfig wechatAccountConfig){
    	WXPayConfig wxPayConfig = new WXPayConfig() {
			
			@Override
			public String getMchID() {
				return wechatAccountConfig.getMchId();
			}
			
			@Override
			public String getKey() {
				return wechatAccountConfig.getMchKey();
			}
			
			@Override
			public int getHttpReadTimeoutMs() {
				return 0;
			}
			
			@Override
			public int getHttpConnectTimeoutMs() {
				return 0;
			}
			
			@Override
			public InputStream getCertStream() {
				return null;
			}
			
			@Override
			public String getAppID() {
				return wechatAccountConfig.getMpAppId();
			}
		};
    	return new WXPay(wxPayConfig);
    }
}