package com.ym.mec.collectfee.utils.yqpay;

import com.alibaba.fastjson.JSON;
import com.ym.mec.collectfee.service.YqPayService;

import java.util.LinkedHashMap;
import java.util.Map;

public class YqPayUtil {
    private YqPayService yqPayService;

    private String merNo = "0021677";
    private static final String version = "1.1";
    private String notifyUrl;
    private String timestamp;
    private String apiContent;
    private static final String signType = "CFCA";
    private String sign;
    private Map<String, Object> resultMap;
    private Map<String, Object> requestMap;

    public YqPayUtil(String notifyUrl, Map<String, Object> resultMap) throws Exception {
        Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
        rqMap.put("merNo", this.merNo);
        rqMap.put("version", this.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, '&');
        this.sign = CFCARAUtil.signMessageByP1(beforeSignedData, "certificate/yqpay.pfx", "aaa123123");
        rqMap.put("sign", this.sign);
        this.requestMap = rqMap;
    }

    public Map<String, Object> getRequestMap() {
        return this.requestMap;
    }

    //验签
    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, "certificate/sq_formal_sign.cer");
        } catch (Exception e) {
            return false;
        }
    }

}
