package com.icloud.server.payutils;

import cn.hutool.core.util.RandomUtil;
import com.icloud.server.payutils.alipay.AlipayPayClientConfig;
import com.icloud.server.payutils.alipay.AlipayWapPayClient;
import com.icloud.server.payutils.dto.PayOrderUnifiedReqDTO;
import com.icloud.server.payutils.pojo.CommonResult;
import com.icloud.server.payutils.wx.WXPayClientConfig;
import com.icloud.server.utils.JsonUtils;
import com.icloud.server.web.err.pay.PayChannelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**支付服务
 * @Author : yang
 * @Date : 2022-07-12 19:10
 * @Version : 2.1.2
 */

@Component
public class PayService {

    @Autowired
    private  PayClientFactoryImpl payClientFactory ;

    public CommonResult getPayUrl(PayEntiy pe){
       if(pe.getPayType()==1){
           return CreatePayClient_WX_PUB_V2(pe);
       }else if(pe.getPayType()==2) {
           return CreatePayClient_ALIPAY_WAP(pe);
       }else { //3
           return CreatePayClient_WX_JS_PUB_V2(pe);

       }

    }
    public CommonResult CreatePayClient_ALIPAY_WAP(PayEntiy pe) {
        String privateKey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDbrl+dSmi0sTHKebUL0n1ySETbbQQYZJ2lgMPmGG1mnAaGBUNQpb0pcnseMyv6swnVCBfH7nFJEOeLoVpkx5j2/gYMIx5i5fFPsdu8dDyPh3FyCSjmDmkMmhiNiPk//1+P99UPnNiN1xW8noOZRhphd8Bh0XM+sIRU/O6FDr+ymKSdySHPlF7tAKXMDILahkHN5SB8jyKNCDeZj3gYNzcD0Zyl2HYYr5haDtfr0NE0XI8c8yD4czs7FuuP51tEB37GFN0TAGz4chKfz9Mc8xZ/aBUlIa6wUk0LJEbjujP3tf+I797R4yyYJ8GhwDN9YkDspPIrw3N+a2chhpT5WzitAgMBAAECggEBALggVT+uqEga5JLCo/5+tGpfLGeF3hLb8k5PuOJYwwln6QfXzW4BHKVqsMrFNS1xduI5YEVRE16cbpZX7T3fFdSfYU42+Tc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DS1JRZrxRAFkh1WlL629vsV9jkS93nkDpc1OS7Cdp8ILsDE";
        String APP_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA265fnUpotLExynm1C9J9ckhE220EGGSdpYDD5hhtZpwGhgVDUKW9KXJ7HjMr+rMJ1QgXx+5xSRDni6FaZMeY9v4GDCMeYuXxT7HbvHQ8j4dxcgko5g5pDJoYjYj5P/9fj/fVD5zYjdcVvJ6DmUYaYXfAYdFzPrCEVPzuhQ6/spiknckhz5Re7QClzAyC2oZBzeUgfI8ijQg3mY94GDc3A9Gcpdh2GK+YWg7X69DRNFyPHPMg+HM7Oxbrj+dbRAd+xhTdEwBs+HISn8/THPMWf2gVJSGusFJNCyRG47oz97X/iO/e0eMsmCfBocAzfWJA7KTyK8NzfmtnIYaU+Vs4rQIDAQAB";
        String notify_url = "http://127.0.0.1:8080";
        String alipayPublicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAghBg+LB71Ivm+3v50LWhAmU69vZqcnWq9nNvIbp/PrDMpp+bfc8vwkEWEEkstF5Jo/kIhFPYWbECAJw8UJsJ7usTjfIZvasXv2KxRUnrsCV94Azn8jV7meHgxpE4LTpBiIqrN+AVf6SLjLlBmwCiQ78g7RbF/pIvsjtKiEPVhB7NAMttNorPVib/mIPbMjzWGXJf/GR6s1oaTs9vNcFbyjMBYd8h9KelyIQFvLaDeB1ffWxWJJ1o52wmgeoGlAhlHaF1dHdf3X9MXCKNpyZtDw9akwqJPAaG+vxKdSVUS9YDVhzBU3fSI05hb5waYeMsxAi6JAPHEluvRT03HzgDnwIDAQAB";
        // 创建配置
        // 创建配置
        AlipayPayClientConfig config = new AlipayPayClientConfig();
        config.setAppId(pe.getAppid());
        config.setServerUrl(AlipayPayClientConfig.SERVER_URL_PROD);
//        config.setServerUrl(AlipayPayClientConfig.SERVER_URL_SANDBOX);
        config.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
        config.setPrivateKey(pe.getPrivateKey());
        config.setAlipayPublicKey(pe.getAlipayPublicKey());
        // 创建客户端
//        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(pe.getChannelId() ,PayChannelEnum.ALIPAY_WAP.getCode(), config);
        PayClient client = payClientFactory.getPayClient(pe.getChannelId());
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(pe);
        CommonResult<?> result = client.unifiedOrder(reqDTO);
          System.out.println(JsonUtils.toJsonString(result));
        String s = JsonUtils.toJsonString(result);
//        result.getData()


//        AlipayPayClientConfig config = new AlipayPayClientConfig();
//        config.setAppId("2021000121617636");
//        config.setServerUrl(AlipayPayClientConfig.SERVER_URL_SANDBOX);
//        config.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
//        config.setPrivateKey(privateKey);
//        config.setAlipayPublicKey(alipayPublicKey);
//        // 创建客户端
//        Long channelId = RandomUtil.randomLong();
//        payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config);
//        PayClient client = payClientFactory.getPayClient(channelId);
//        // 发起支付
//        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO();
//        CommonResult<?> result = client.unifiedOrder(reqDTO);
//        System.out.println(JsonUtils.toJsonString(result));






        return result;
    }

    public CommonResult CreatePayClient_WX_PUB_V2(PayEntiy pe) {
        // 创建配置
        WXPayClientConfig config = new WXPayClientConfig();
        config.setAppId(pe.getAppid());
        config.setMchId(pe.getMchId());
        config.setApiVersion(WXPayClientConfig.API_VERSION_V2);
        config.setMchKey(pe.getMchKey());


//        config.
        // 创建客户端
//        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(pe.getChannelId(), PayChannelEnum.WX_H5.getCode(), config);
        PayClient client = payClientFactory.getPayClient(pe.getChannelId());
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(pe);
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        System.out.println(result);
        return result;
    }
    public CommonResult CreatePayClient_WX_JS_PUB_V2(PayEntiy pe) {
        // 创建配置
        WXPayClientConfig config = new WXPayClientConfig();
        config.setAppId(pe.getAppid());
        config.setMchId(pe.getMchId());
        config.setApiVersion(WXPayClientConfig.API_VERSION_V2);
        config.setMchKey(pe.getMchKey());
//        config.
        // 创建客户端
//        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(pe.getChannelId(), PayChannelEnum.WX_PUB.getCode(), config);
        PayClient client = payClientFactory.getPayClient(pe.getChannelId());
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(pe);
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        System.out.println(result);
        return result;
    }

    public CommonResult CreatePayClient_WX_PUB_V21(PayEntiy pe) {

        // 创建配置
        WXPayClientConfig config = new WXPayClientConfig();
        config.setAppId(pe.getAppid());
        config.setMchId(pe.getMchId());
        config.setApiVersion(WXPayClientConfig.API_VERSION_V2);
        config.setMchKey(pe.getMchKey());
        // 创建客户端
        payClientFactory.createOrUpdatePayClient(pe.getChannelId(), PayChannelEnum.WX_PUB.getCode(), config);
        PayClient client = payClientFactory.getPayClient(pe.getChannelId());
//        // 发起支付
//        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO();
//        CommonResult<?> result = client.unifiedOrder(reqDTO);
//        System.out.println(result);


        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(pe);
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        System.out.println(result);
        return result;
    }







    public void testCreatePayClient_WX_PUB_V2() {
        // 创建配置
        WXPayClientConfig config = new WXPayClientConfig();
        config.setAppId("wx09dd86974d014a08");
        config.setMchId("1371137902");
        config.setApiVersion(WXPayClientConfig.API_VERSION_V2);
        config.setMchKey("a1b2c3defghijKlmnopqrstuvwx4y5z6");
        // 创建客户端
        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_H5.getCode(), config);
        PayClient client = payClientFactory.getPayClient(channelId);
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO();
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        System.out.println(result);
    }
    private static PayOrderUnifiedReqDTO buildPayOrderUnifiedReqDTO(PayEntiy pe) {
        PayOrderUnifiedReqDTO reqDTO = new PayOrderUnifiedReqDTO();
        reqDTO.setAmount(pe.getAmount());
        reqDTO.setSubject(pe.getSubject());
        reqDTO.setBody(pe.getBody());
        reqDTO.setMerchantOrderId(pe.getMerchantOrderId());
        reqDTO.setUserIp(pe.getUserIp());
        reqDTO.setNotifyUrl(pe.getNotifyUrl());

        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("attach",pe.getOutTradeNo());
        if(pe.getPayType()==1){
            hm.put("trade_type","MWEB");


        }
        if(pe.getOpenid()!=null){
            hm.put("openid",pe.getOpenid());
        }
        reqDTO.setChannelExtras(hm);
        return reqDTO;
    }
    private static PayOrderUnifiedReqDTO buildPayOrderUnifiedReqDTO() {
        PayOrderUnifiedReqDTO reqDTO = new PayOrderUnifiedReqDTO();
        reqDTO.setAmount(123L);
        reqDTO.setSubject("IPhone 13");
        reqDTO.setBody("biubiubiu");
        reqDTO.setMerchantOrderId(String.valueOf(System.currentTimeMillis()));
        reqDTO.setUserIp("127.0.0.1");
        String url="http://47.102.216.51:8180/liveService/wxpay/payover/enter/";
        reqDTO.setNotifyUrl(url);

        HashMap<String, String> hm = new HashMap<String, String>();
//        hm.put("openid","1223456");
        hm.put("trade_type","MWEB");

        reqDTO.setChannelExtras(hm);
        return reqDTO;
    }



}
