package com.icloud.server.payutils.alipay;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.icloud.server.payutils.PayClient;
import com.icloud.server.payutils.PayClientFactoryImpl;
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.payutils.wx.WXPubPayClient;
import com.icloud.server.utils.JsonUtils;
import com.icloud.server.web.err.pay.PayChannelEnum;
import com.icloud.server.web.json.JsonResult;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.lang.*;
import java.util.UUID;

@Api( tags = {"pay_demo" }, value = "API",hidden = true )
@ApiIgnore
@RestController
@RequestMapping("/pay")
public class AliPayController {
    private WxPayService client;

    @Autowired
    private  PayClientFactoryImpl payClientFactory ;


    @RequestMapping("/ali/order")
    public JsonResult findMyPlayRecord(){
//        testCreatePayClient_WX_PUB_V2();
//        testCreatePayClient_ALIPAY_QR();
        testCreatePayClient_ALIPAY_WAP();
        Map<String ,String> recordMap = new HashMap<>();



        return new JsonResult(recordMap);

    }



    public void testCreatePayClient_ALIPAY_QR() {
        String privateKey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDbrl+dSmi0sTHKebUL0n1ySETbbQQYZJ2lgMPmGG1mnAa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EYqDsBhZXfDShvbfeSlXVqPMyzYCYcr3l2xGyKtuDDYxP3wKj/PkIT47SYTV9p618S54kSLOkeRZo/avDpBlEDPDtvW3rzXPUqu9IKgLQKBgQCLiYdpXLKElhEOQHnAG8slqj4Z6RK3ObgPoB5qjPpnSEgkIbmc0M1+f5/R++QHWYBGfXIZ1GMuvcjYxOfc34WXsCsAaEI4KREaAEf88qxthnWlxyMhrGk1dwbghFdcZLGurccySoya1CCpj0U5tiFTm3SohqfrQhpcKLLF+XKCQQKBgGSYZZ65F+WSDUrP1DfIkHzJcpVj4rFGLretIi3PJItFazq8pahWh3EllogUxMuntknOPwdbfptP/LCN/pTLCCYiwINgeCSZaEIlsSiUEJTmO5NE4YwEpSpbEWNuEDS1JRZrxRAFkh1WlL629vsV9jkS93nkDpc1OS7Cdp8ILsDE";
         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("2021000121617636");
        config.setAlipayPublicKey(alipayPublicKey);
        config.setServerUrl(AlipayPayClientConfig.SERVER_URL_SANDBOX);
        config.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
        config.setPrivateKey(privateKey);

        // 创建客户端
        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config);
        PayClient client = payClientFactory.getPayClient(channelId);
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO();
        reqDTO.setNotifyUrl(notify_url); // TODO @tina: 这里改成你的 natapp 回调地址
        CommonResult<AlipayTradePrecreateResponse> result = (CommonResult<AlipayTradePrecreateResponse>) client.unifiedOrder(reqDTO);
        System.out.println(JsonUtils.toJsonString(result));
        System.out.println(result.getData().getQrCode());
    }
    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;
    }


    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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";
               alipayPublicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA265fnUpotLExynm1C9J9ckhE220EGGSdpYDD5hhtZpwGhgVDUKW9KXJ7HjMr+rMJ1QgXx+5xSRDni6FaZMeY9v4GDCMeYuXxT7HbvHQ8j4dxcgko5g5pDJoYjYj5P/9fj/fVD5zYjdcVvJ6DmUYaYXfAYdFzPrCEVPzuhQ6/spiknckhz5Re7QClzAyC2oZBzeUgfI8ijQg3mY94GDc3A9Gcpdh2GK+YWg7X69DRNFyPHPMg+HM7Oxbrj+dbRAd+xhTdEwBs+HISn8/THPMWf2gVJSGusFJNCyRG47oz97X/iO/e0eMsmCfBocAzfWJA7KTyK8NzfmtnIYaU+Vs4rQIDAQAB";



        // 创建配置
        // 创建配置
        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));
        AlipayTradeWapPayResponse data =(AlipayTradeWapPayResponse) result.getData();
        String body = data.getBody();
        System.out.println(body);

    }




    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);
    }

    /**
     * {@link WXPubPayClient} 的 V3 版本
     */

    public void testCreatePayClient_WX_PUB_V3() throws FileNotFoundException {
        // 创建配置
        WXPayClientConfig config = new WXPayClientConfig();
        config.setAppId("wx041349c6f39b268b");
        config.setMchId("1545083881");
        config.setApiVersion(WXPayClientConfig.API_VERSION_V3);
        config.setPrivateKeyContent(IoUtil.readUtf8(new FileInputStream("/Users/yunai/Downloads/wx_pay/apiclient_key.pem")));
        config.setPrivateCertContent(IoUtil.readUtf8(new FileInputStream("/Users/yunai/Downloads/wx_pay/apiclient_cert.pem")));
        config.setApiV3Key("joerVi8y5DJ3o4ttA0o1uH47Xz1u2Ase");
        // 创建客户端
        Long channelId = RandomUtil.randomLong();
        payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config);
        PayClient client = payClientFactory.getPayClient(channelId);
        // 发起支付
        PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO();
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        System.out.println(result);
    }



//    private WxPayUnifiedOrderRequest createWxOrder(HttpServletRequest req, Map<String, Object> optional, String title, boolean isWxJs, String openId, Integer amount,String type,Long userId) {
//        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
//        String orderNum = genOrderNum();
//        request.setBody(title);
//        request.setOutTradeNo(orderNum);
//        request.setTotalFee(amount);
//        request.setSpbillCreateIp(IPUtil.getIpAddress(req));
//        String ctx = (String) req.getAttribute("_ctx_");
//        request.setNotifyUrl(ctx + "/wxpay/payover/enter/"+userId);
//        request.setTradeType(type);
//        if (StringUtils.isNotBlank(openId))
//            request.setOpenid(openId);
//        request.setProductId(RandomStringUtils.randomNumeric(20));
//        PayOrder order = new PayOrder();
//        order.setOrderNum(orderNum);
//        order.setType(PayType.WX.getVal());
//        order.setStatus(PayStatus.UN_PAY.getVal());
//        order.setTotalFee(new BigDecimal(amount).doubleValue());
//        log.debug("wxtotalfee:"+order.getTotalFee().intValue());
//        try {
//            order.setAttach(URLEncoder.encode(mapper.writeValueAsString(optional),"utf-8"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        order.setCreateTime((int) (System.currentTimeMillis() / 1000));
//        this.payOrderService.save(order);
//        return request;
//    }


    private  PayOrderUnifiedReqDTO wxbuildPayOrderUnifiedReqDTO() {
        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;
    }
    private String genOrderNum() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    private WxPayUnifiedOrderRequest createWxOrder(){
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        String orderNum = genOrderNum();
        request.setBody("aaaa");
        request.setOutTradeNo(orderNum);
        request.setTotalFee(123);
        request.setSpbillCreateIp("127.0.0.1");
        String url="http://47.102.216.51:8180/liveService//wxpay/payover/enter/";

//        String ctx = (String) req.getAttribute("_ctx_");
        request.setNotifyUrl(url);
        request.setProductId(String.valueOf(System.currentTimeMillis()));
        return request;
    }
//    public void testCreatePayClient_WX_PUB_V22() {
//        // 创建配置
//        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();
//        client.
//        CommonResult<?> result = client.unifiedOrder(reqDTO);
//        System.out.println(result);
//    }


}
