package com.ichangzuo.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConstants;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.ichangzuo.config.property.AliPayProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@EnableConfigurationProperties(AliPayProperties.class)
public class AlipayConfig {
	private static AliPayProperties aliPayProperties;

	public AlipayConfig(AliPayProperties properties) {
		aliPayProperties = properties;
	}

    public static final String APP_ID_APP = "2016041901311447";
	public static final String APP_ID_WEB = "2021005104641778";
    public static final String APP_ID_SANDBOX = "9021000138603610";

	public static final int WEB = 0;
	public static final int APP = 1;
//	public static final int WAP = 2;
    //支付宝私钥
    private static final String APP_PRIVATE_KEY = """
					MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDKIIMWunigVf65\
					5us9PWlMKebBlDwU8mkJHLWqSj35HWugWEq4lQkkCQOegUIHCvVGaQIyha9WpWdi\
					wxGQZTfry+VxGtA/TiFKD1K5hGQw7Wnf5yMqM/iFZGyORdWdtE1ajESrxHYqp05h\
					qqfk922qRgKtlFdHxU/52MCXhauhmvARrHHrFpe3QIXpFh5U05WyWqs2VhTGNihM\
					83sOvu6D8uEru412cMnbe0iu5Rc+nc0Uzy+YaJEVKtDBueu8IMZmTsJenK8eizrU\
					6gRZrgAB+bCJc4l1ZtA00/ygI6IawJf3S67fwb4+hL4fDyuhyCWU4lRGW8+mM8DP\
					tFaI9ciPAgMBAAECggEBAJ134MinM2yuMknARgoqusr0ZervwjMLe5r2u+zT9B4M\
					tplvz56ntTxWrXQh/T+hYN6e1nBnx+b362h80kUtJfjUm4aXPh/jjXm2IFuZcbjQ\
					bUhbOQEbBgVd7FiWvUokepMNbj5nSywFtcHPAwgVX2FlM5bpV2n5pUnffINPRxwZ\
					RverEmpcqZf1L5yPG8+axAh3RJdsM5PYp4D8erkD9bp0vU1Tq7hQ+MU9HHM/i9D8\
					L3kGSz/xah3l68QvI91MiyF3Qg1EMHyz4tqxq0Db63Err6s8v0Y0n8F9SCrzP2d5\
					d3Xh8MMuVNPtQnIEAG084IpK1HLy2pL9CDb47cRsyWkCgYEA7/E0ga9CFzMt6kAc\
					2qolXbX1jopwjVFpmfdWjyrrrDgke+eC19bFP/BR7MpLEKanTY8azKKU3VInIHFy\
					uddp/ZJqrMNHV0BtaiRWqJMw5VnuQTgTo1BNJ128bfLDep6F4I103csQncu9I6/S\
					Am2GSKPRvKu1CrzOJpige0jQx9UCgYEA16dwtnrw4vKeFvsTC2LOkiQ9YC/EFKkP\
					EKPhtHuTXeaxBQ1scZgBVTr65pfz3cl3VKbIfxFM5TflSl5K72KtxGPn/2wUh0fV\
					ud+E5LM76mGSe5pBjoPyFY2i/Qyic+AA8L8vfHQwKqFo1OqfDBEJkmrsXgjQBtyX\
					1Oi3zj9axNMCgYEA4J7FsMII9P8MdMcgO/QcluXIw3AGfcVBPsm1VsGvbsIAJZ5N\
					dxGwBnNLvoiCTUw2Qv088WUiRy6pQk3yQNfQeXmgM6t8FcpSo5LxLU7d71eJG7UL\
					bU+3aqrtw2AIb7oHSngid5+qJo6cudPWnj85/radmiqEiVDHDIrFcaRxDyECgYBW\
					TUDTFiIegH95rOKzNMh8PZp+Sr9KkVlhDGR/6NBRzMdcwUF7uBwYcrED5R2HzV8+\
					9jvYdiDyvkq5V0DfyfrGVED8u9D/TmUerG+vYncA1ilb46CGmxEfRP5MDGlau/NE\
					ZQ5o3MqF1PBx/K7Hkm3lNXsAKsCtbkwovTUJidsWVwKBgQC9BED3+SJIKmGnP107\
					piz0hCvT/5HkfOXYgP8K0IAATW26h81k5QFEGfWHGSKqam9oB+Jz2YToh7gwuS6E\
					Zo6zc0xzYPR5QJvbZC/YYOMmfjz0F9Z7uFC2Tq+143bCkg9XlDwY8KKTxAfb80t7\
					m9zGkwDF59l1GEV9QNmh1q7jKA==""";
    //支付宝公钥
    public static final String ALIPAY_PUBLIC_KEY = """
                    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi2w/yQ9QCkAeKD1mQDkN\
                    tZ5GY83FQ2x+mEVmVMF1c2a4eb8btYd62WOzaCFb0Is8BP2up1B8GElTmoDEO44V\
                    tuC+2elHYezSRXhiBcrnnRjZebFh+k5SG4pgIBXVsSR/2fL94p8reYadTmV18wLd\
                    W7Mqi54S16WNlw+PXkg8o4bnYxvJj6K8/lhgZg5As5cfsWK4WMQ1osnlPkSUb4uj\
                    ye4KB2RWkVla+cZmswu8gWA1M9ygtcjCpvTyT2oVIoFQkWIU3IO9CxVPWhF7l8nP\
                    OJW2fj9qIWWISPNA4PJCeTvGwN7lwE4oASGnag0CSJxpgz8SmKKAFtOm4rB08aCG\
                    4QIDAQAB""";

    public static final String ALIPAY_PUBLIC_KEY_SANDBOX =
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvMfJ4G5X9JtJ5JwIiYpZ8WHKnbBaAHNjdS/Pvy7O51KBuBC/3fiQRKMy8P9HhfikAuoDrAJo3LKc3vQ2oSLcg5P+6/phL/y1fLudu3rX45hfSuB2pm+eImgViylK2oXZxMu4KJCuK+iwt0NDABz5tGKc1veuoLJQKOLPgV0NXOP/QMKa598T98NqlM5qGBvZEykKI6f6E8GOytzY+deTtUPhoNAVmTWz4KgLWN1f9pGRT9qDKPai+TmVgqXPwpsBYeXoC9usCxpc20x+HF+CmL0nG1p7Wb43LDL17pbnL/ZFgfWWoa/mvbLOFP1zmeMsYNU3Rs5V8YjgWf0u5y+rhQIDAQAB";
    private static final String APP_PRIVATE_KEY_SANDBOX =
			"MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDhVzjbtde5cTroYrZ2aqVZ85Lwx6M7iVehoOHgW6Hq3mYspA7m4Axs/uCjaeORNsMoG6P0t5XDBkDzQ9XZyzUGdcsXKviIKPMwdzqA3lOGp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/j21TiBa+ZEXAoVb2Tu8qw6OrxuBNi1OlgGbzYdlzvU7tS0bj53ZDMC5cdECgYBlo59piSpEqZGPYbK5pquF/4lpWhGl7cqcLnb9ZNT3xxrH3KlTQ4BlYPvWS0rnerpm//nROTAIw8Kag7pDyNlh9Jzor6hzs2F4ptrMKz83gLivoZ5ZxtEzGSC1+AiAd7jBp66ILGXGRBLVaRJPp0eXvZ129LZycU+5iM5VNGLJMwKBgQCZig65wd33dZdtt/oYdfZqFOZHdVk7QaQygqxWNh8N2TraWW571kfGFqlUJ8+IIuSx8+CAMNRSCB7MruuSVLvkouywcVt3lSsQvhPRriY7UUXI4mYDCSihk7aP7e11OfVH3ex3/mELHPEG0UgfaIa/Co9SlOYDnMyKZz3aL5xV4w==";

    public static final String SIGNTYPE = "RSA2";
    //支付宝回调的接口
    public static final String SERVERURL = "https://openapi.alipay.com/gateway.do";
    public static final String SERVERURL_SANDBOX = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static AlipayClient alipayAppClient = null;
	private static AlipayClient alipayWebClient = null;

    //因为支付宝alipayClient本身是线程安全的，因此只用创建一个，创建成单例的模式
//    public static AlipayClient getAlipayAppClient() {
//        synchronized (AlipayConfig.class) {
//            if (null == alipayAppClient) {
//                alipayAppClient = new DefaultAlipayClient(SERVERURL, APP_ID_APP, APP_PRIVATE_KEY,
//                                        AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
//                                        ALIPAY_PUBLIC_KEY, SIGNTYPE);
//            }
//        }
//        return alipayAppClient;
//    }
//
//	public static AlipayClient getAlipayWebClient() {
//		synchronized (AlipayConfig.class) {
//			if (null == alipayWebClient) {
//				alipayWebClient = new DefaultAlipayClient(SERVERURL, APP_ID_WEB, APP_PRIVATE_KEY,
//										AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
//										ALIPAY_PUBLIC_KEY, SIGNTYPE);
//			}
//		}
//		return alipayWebClient;
//	}
//
//    public static AlipayClient getAlipaySandboxClient() {
//        synchronized (AlipayConfig.class) {
//            if (null == alipayWebClient) {
//                alipayWebClient = new DefaultAlipayClient(SERVERURL_SANDBOX, APP_ID_SANDBOX, APP_PRIVATE_KEY_SANDBOX,
//                        AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
//                        ALIPAY_PUBLIC_KEY_SANDBOX, SIGNTYPE);
//            }
//        }
//        return alipayWebClient;
//    }

	public static String notifyUrl(){
		return aliPayProperties.getNotifyUrl();
	}

	public static String returnUrl(){
		return aliPayProperties.getReturnUrl();
	}

	//因为支付宝alipayClient本身是线程安全的，因此只用创建一个，创建成单例的模式
	public static AlipayClient getClient(int type){
		synchronized (AlipayConfig.class) {
			AlipayClient client = type==WEB ? alipayWebClient : alipayAppClient;
			if(null == client) {
				if (aliPayProperties.isSandBox()) {
					client = new DefaultAlipayClient(SERVERURL_SANDBOX, APP_ID_SANDBOX, APP_PRIVATE_KEY_SANDBOX,
							AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
							ALIPAY_PUBLIC_KEY_SANDBOX, SIGNTYPE);
				} else if (type == WEB) {
					client = new DefaultAlipayClient(SERVERURL, APP_ID_WEB, APP_PRIVATE_KEY,
							AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
							ALIPAY_PUBLIC_KEY, SIGNTYPE);
				} else if (type == APP) {
					client = new DefaultAlipayClient(SERVERURL, APP_ID_APP, APP_PRIVATE_KEY,
							AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8,
							ALIPAY_PUBLIC_KEY, SIGNTYPE);
				}

				if(type==WEB)
					alipayWebClient = client;
				else
					alipayAppClient = client;
			}
		}

		return type==WEB ? alipayWebClient : alipayAppClient;
	}

	public static Boolean checkSignature(Map<String, String> params) throws AlipayApiException {
		if(aliPayProperties.isSandBox()){
			return AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY_SANDBOX,
					AlipayConstants.CHARSET_UTF8,AlipayConfig.SIGNTYPE) ;
		}
		else{
			return AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY,
					AlipayConstants.CHARSET_UTF8,AlipayConfig.SIGNTYPE) ;
		}
	}
}
