package com.dxhy.erp.utils;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.erp.entity.CheckInvoiceParam;
import com.dxhy.erp.entity.FP;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@Slf4j
public class CheckUtil {

    private static final String URL = "https://cypt.ele-cloud.com/recipt/checkFp/barcode";

    public static FP getCheck(CheckInvoiceParam checkInvoiceParam) throws Exception {
        String hmacsha1 = CheckHmacsha1Util.getHmacsha1(checkInvoiceParam);
        String json = HttpUtil.post(URL, hmacsha1, 30000);
        if (!"".equals(json)) {
            JSONObject job = JSONObject.parseObject(Base64.decodeStr(json));
            JSONObject jArray = job.getJSONObject("FPXT");
            JSONObject jsonObj = jArray.getJSONObject("RESPONSE_CONTENT");
            String string = jsonObj.getString("FP");
            return JSONObject.parseObject(string, FP.class);
        }
        return null;
    }
}
