package com.keao.edu.thirdparty.yqpay;

import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.*;

public class YqPayUtil {

    private static final String merNo = "0021677"; //商户号
    private static final String version = "1.1";
    private static final String signType = "CFCA";
    private static final String payUrl = "https://qyfapi.95epay.com/api/api/hPay/toPayHtml";//支付提交地址
    private static final String payChannels = "{\"weChatPay\":true,\"weChatPayMobile\":false,\"aliPay\":true,\"fastpayXy\":true,\"aliPayMobile\":false,\"balancePay\":false}";//支付方式配置

    public static Map<String, Object> getRequestMap(String notifyUrl, Map<String, Object> resultMap) throws Exception {
        Map<String, Object> rqMap = new LinkedHashMap<>();
        rqMap.put("merNo", merNo);
        rqMap.put("version", version);
        rqMap.put("notifyUrl", notifyUrl);
        rqMap.put("timestamp", DateUtils.getDateTime());
        rqMap.put("apiContent", JSON.toJSONString(resultMap));
        rqMap.put("signType", signType);
        String beforeSignedData = CFCARAUtil.joinMapValue(rqMap, '&');
        String sign = CFCARAUtil.signMessageByP1(beforeSignedData, "config/certificate/yqpay.pfx", "aaa123123");
        rqMap.put("sign", sign);
        return rqMap;
    }

    /**
     * 获取支付Map
     *
     * @param amount
     * @param orderNo
     * @param notifyUrl
     * @param returnUrl
     * @param orderSubject
     * @param orderBody
     * @param routingMerNo
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sellerNo, String routingMerNo) throws Exception {
        List<Map> tempRoutingList = new ArrayList();
        Map<String, Object> routingList = new HashMap<>();
        routingList.put("routingMerNo", routingMerNo);//分佣账户
        routingList.put("routingFee", amount.subtract((amount.multiply(new BigDecimal("0.28")).divide(new BigDecimal(100))).setScale(2, BigDecimal.ROUND_HALF_UP))); //分佣金额
        tempRoutingList.add(routingList);

        Map<String, Object> contentMap = new LinkedHashMap<>();
        contentMap.put("sellerNo", sellerNo); //收款商户号
        contentMap.put("payChannels", payChannels); //支付方式
        contentMap.put("orderBody", orderBody); //订单信息
        contentMap.put("payAmount", amount); //支付金额
        contentMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/
        contentMap.put("tradeType", "0"); //*交易类型1—充值，0—收款*
        contentMap.put("merMerOrderNo", orderNo); //商户订单号
        contentMap.put("orderSubject", orderSubject); //订单标题
        contentMap.put("returnUrl", returnUrl); //前台页面地址
        if (routingMerNo != null && !routingMerNo.isEmpty() && !sellerNo.equals(routingMerNo)) {
            contentMap.put("tempRoutingList", JSON.toJSONString(tempRoutingList));//分账设置
        }
        Map<String, Object> payMap = getRequestMap(notifyUrl, contentMap);
        payMap.put("host", payUrl);
        return payMap;
    }

    /**
     * 获取支付Map
     *
     * @param amount
     * @param orderNo
     * @param notifyUrl
     * @param returnUrl
     * @param orderSubject
     * @param orderBody
     * @param sellerNo
     * @param tempRoutingList
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sellerNo, List<Map<String, Object>> tempRoutingList) throws Exception {
        Map<String, Object> contentMap = new LinkedHashMap<>();
        contentMap.put("sellerNo", sellerNo); //收款商户号
        contentMap.put("payChannels", payChannels); //支付方式
        contentMap.put("orderBody", orderBody); //订单信息
        contentMap.put("payAmount", amount); //支付金额
        contentMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/
        contentMap.put("tradeType", "0"); //*交易类型1—充值，0—收款*
        contentMap.put("merMerOrderNo", orderNo); //商户订单号
        contentMap.put("orderSubject", orderSubject); //订单标题
        contentMap.put("returnUrl", returnUrl); //前台页面地址
        if (tempRoutingList != null) {
            contentMap.put("tempRoutingList", JSON.toJSONString(tempRoutingList));//分账设置
        }
        Map<String, Object> payMap = getRequestMap(notifyUrl, contentMap);
        payMap.put("host", payUrl);
        return payMap;
    }


    public static Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody) throws Exception {
        Map<String, Object> contentMap = new LinkedHashMap<>();
        contentMap.put("sellerNo", merNo); //收款商户号
        contentMap.put("payChannels", payChannels); //支付方式
        contentMap.put("orderBody", orderBody); //订单信息
        contentMap.put("payAmount", amount); //支付金额
        contentMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/
        contentMap.put("tradeType", "0"); //*交易类型1—充值，0—收款*
        contentMap.put("merMerOrderNo", orderNo); //商户订单号
        contentMap.put("orderSubject", orderSubject); //订单标题
        contentMap.put("returnUrl", returnUrl); //前台页面地址
        Map<String, Object> payMap = getRequestMap(notifyUrl, contentMap);
        payMap.put("host", payUrl);
        return payMap;
    }


    /**
     * 验签
     *
     * @param rsMap
     * @return
     */
    public static boolean verify(Map<String, Object> rsMap) {
        String sign = (String) rsMap.get("sign");
        rsMap.remove("sign");
        String beforeSignedData = CFCARAUtil.joinMapValue(rsMap, '&');
        try {
            return CFCARAUtil.verifyMessageByP1(beforeSignedData, sign, "/config/certificate/sq_formal_sign.cer");
        } catch (Exception e) {
            return false;
        }
    }

}
