package com.iformall.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iformall.common.ErrorCode;
import com.iformall.exception.MallinkException;
import com.iformall.pay.WxPayConstant;
import com.iformall.pay.WxPayment;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: POS接口
 * @author: Stormeye
 * @date: 2018/3/8 17:41
 * @version: V1.0
 */
public class PosUtil {
    private static final Logger logger = LoggerFactory.getLogger(PosUtil.class);

    private static final String domain            = "http://localhost:4000"; // "https://pos.youlane.cn/api";
    private static final String devId             = "fmpos";
    private static final String reqKey            = "ZiGFLC4@3c5sTLZT";
    private static final String resKey            = "ugPAM7wd&%p6I0W8";

    // url
    private static final String URL_Version                     = "/version";
    private static final String URL_CheckUserPassword           = "/checkUserPassword";
    private static final String URL_GetPosMemConfig             = "/getPosMemConfig";
    private static final String URL_GetQrCode                   = "/getQrCode";
    private static final String URL_CheckMem                    = "/checkMem";
    private static final String URL_CheckCouponOrderForVerify   = "/checkCouponOrderForVerify";
    private static final String URL_CouponOrderPreVerify        = "/couponOrderPreVerify";
    private static final String URL_CouponOrderPreVerifyCancel  = "/couponOrderPreVerifyCancel";
    private static final String URL_CouponOrderIndependentVerify  = "/couponOrderIndependentVerify";
    private static final String URL_CouponOrderPayVerifyOne     = "/couponOrderPayVerifyOne";
    private static final String URL_CouponOrderPayVerifyList    = "/couponOrderPayVerifyList";
    private static final String URL_CouponOrderVerifyCancel     = "/couponOrderVerifyCancel";
    private static final String URL_CheckCardPay                = "/checkCardPay";
    private static final String URL_CardPayPre                  = "/cardPayPre";
    private static final String URL_CardPayPreCancel            = "/cardPayPreCancel";
    private static final String URL_CardPay                     = "/cardPay";
    private static final String URL_CardPayCancel               = "/cardPayCancel";


    /*
	可用的交易券, 支付可用(1, 2, 6), 独立核销可用(4, 6, 8, 9)
	coupon_order_id,	coupon_type,	售价,	面额, cancellable
	321872988134408192,	4,	0,	2000,   NO
	321877927162249216,	2,	0,	10000,	YES
	321878123232591872,	2,	1,	10000,	NO
	321878185355214848,	1,	0,	10000,	NO
	321878223800205312,	6,	0,	1000,	YES
	321878329906921472,	6,	1,	1000,	NO
	321882224846667776,	8,	1,	1500,	NO
	321883878446825472,	100,	1,	50000,	NO
	*/

    private final OkHttpClient client            = new OkHttpClient();
    private String doPost(String url, Map<String, String> paramMap) {
        String result = null;
        MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
        JSONObject json = new JSONObject();
        for (Map.Entry<String, String> entry: paramMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            // 略过空值
            if (StringUtils.isBlank(value))
                continue;
            json.put(key, value);
        }
        RequestBody requestBody = RequestBody.create(jsonType, JSON.toJSONString(json));
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return result;
    }
    private String doGet(String url) {
        String result = null;
        HttpUrl reqUrl = HttpUrl.parse(url).newBuilder().build();
        Request request = new Request.Builder().url(reqUrl).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    private boolean checkRespFailed(String resp) {
        if(resp.startsWith("<html>") || resp.startsWith("<!DOCTYPE html>")) {
            return true;
        }
        return false;
    }


    /**
     *  0. 检查POS会员server是否正常
     * @param baseUrl
     * @return B端用户信息
     */
    public String checkPosMemServer(String baseUrl) throws MallinkException {
        String respStr = doGet(baseUrl + URL_Version);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }
    /**
     *  1. 商户POS用户/B端用户登录检查
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param phone
     * @param password
     * @return B端用户信息
     */
    public String checkUserPassword(String baseUrl, String devId, String reqKey,
                             String tenantId, String phone, String password) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.PHONE, phone);
        paramMap.put(WxPayConstant.PASSWORD, password);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CheckUserPassword, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  2. 获取会员折扣/优惠券/消费卡是否启用
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @return 会员折扣信息
     */
    public String getPosMemConfig(String baseUrl, String devId, String reqKey,
                                    String tenantId) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_GetPosMemConfig, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  3. 获取注册二维码及小票二维码规则
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @return 二维码url及小票二维码规则
     */
    public String getQrCode(String baseUrl, String devId, String reqKey,
                                  String tenantId) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_GetQrCode, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }
    /**
     *  4. 会员识别
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param memId
     * @param memPhone
     * @param posOrderId
     * @param posAmount
     * @return 会员信息+会员折扣+会员可用优惠券列表
     */
    public String checkMem(String baseUrl, String devId, String reqKey,
                            String tenantId, String merchantId,
                            String memId, String memPhone,
                            String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        if (StringUtils.isNotBlank(memId)) {
            paramMap.put(WxPayConstant.MEM_ID, memId);
        }
        if (StringUtils.isNotBlank(memPhone)) {
            paramMap.put(WxPayConstant.MEM_PHONE, memPhone);
        }
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CheckMem, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  5. 券核销检查
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param verifyType
     * @param couponOrderId
     * @param posOrderId
     * @param posAmount
     * @return 券是否可核销
     */
    public String checkCouponOrderForVerify(String baseUrl, String devId, String reqKey,
                           String tenantId, String merchantId, String buUserId,
                           String verifyType, String couponOrderId,
                           String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.SCENE_TYPE, verifyType);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        if (StringUtils.isNotBlank(posOrderId)) {
            paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        }
        if (StringUtils.isNotBlank(posAmount)) {
            paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        }
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CheckCouponOrderForVerify, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  6. 券预核销
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderId
     * @param posOrderId
     * @param posAmount
     * @return
     */
    public String couponOrderPreVerify(String baseUrl, String devId, String reqKey,
                                            String tenantId, String merchantId, String buUserId,
                                            String couponOrderId,
                                            String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderPreVerify, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  7. 券预核销取消
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderId
     * @param posOrderId
     * @param posAmount
     * @return
     */
    public String couponOrderPreVerifyCancel(String baseUrl, String devId, String reqKey,
                                       String tenantId, String merchantId, String buUserId,
                                       String couponOrderId,
                                       String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderPreVerifyCancel, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  8. 券独立核销
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderId
     * @param posOrderId
     * @return
     */
    public String couponOrderIndependentVerify(String baseUrl, String devId, String reqKey,
                                             String tenantId, String merchantId, String buUserId,
                                             String couponOrderId,
                                             String posOrderId) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderIndependentVerify, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  9. 券交易核销-单个优惠券接口
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderId
     * @param posOrderId
     * @param posAmount
     * @return
     */
    public String couponOrderPayVerifyOne(String baseUrl, String devId, String reqKey,
                                               String tenantId, String merchantId, String buUserId,
                                               String couponOrderId,
                                               String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderPayVerifyOne, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  10. 券交易核销-优惠券列表
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderIdList
     * @param posOrderId
     * @param posAmount
     * @return
     */
    public String couponOrderPayVerifyList(String baseUrl, String devId, String reqKey,
                                          String tenantId, String merchantId, String buUserId,
                                          List<String> couponOrderIdList,
                                          String posOrderId, String posAmount) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_LIST, JSON.toJSONString(couponOrderIdList));
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap.put(WxPayConstant.ORDER_AMOUNT, posAmount);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderPayVerifyList, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }

    /**
     *  11. 券核销取消
     * @param baseUrl
     * @param devId
     * @param reqKey
     * @param tenantId
     * @param merchantId
     * @param buUserId
     * @param couponOrderId
     * @param posOrderId
     * @return
     */
    public String couponOrderVerifyCancel(String baseUrl, String devId, String reqKey,
                                          String tenantId, String merchantId, String buUserId,
                                          String couponOrderId,
                                          String posOrderId) throws MallinkException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(WxPayConstant.DEV_ID, devId);
        paramMap.put(WxPayConstant.TENANT_ID, tenantId);
        paramMap.put(WxPayConstant.MERCHANT_ID, merchantId);
        paramMap.put(WxPayConstant.BUSER_ID, buUserId);
        paramMap.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
        paramMap.put(WxPayConstant.POS_ORDER_ID, posOrderId);
        paramMap = WxPayment.buildSignAfterParasMapForHMAC(paramMap, reqKey);

        String respStr = doPost(baseUrl + URL_CouponOrderVerifyCancel, paramMap);
        if(respStr == null) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        if(checkRespFailed(respStr)) {
            throw new MallinkException(ErrorCode.POS_CMD_FAIL);
        }
        return respStr;
    }



    public static void main(String[] args) {
        String tenantId = "456";
        String phone = "13120223636";
        String password = "drpos345";
        String merchantId = "320833942159982592";
        String buUserId = "320834180694245376";
        String memId = "";
        String memPhone = "13910154397";
        String posOrderId = "1";
        String posAmount = "200";
        String couponOrderId = "321877927162249216";
        String verifyType = "independent";
        PosUtil posUtil = new PosUtil();

        String resStr = posUtil.checkPosMemServer(domain);
        if (resStr != null) {
            System.out.println(resStr);
        }

        /*
        String resStr = posUtil.checkUserPassword(domain, devId, reqKey, tenantId, phone, password);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        resStr = posUtil.getPosMemConfig(domain, devId, reqKey, tenantId);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }

        resStr = posUtil.getQrCode(domain, devId, reqKey, tenantId);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }

        resStr = posUtil.checkMem(domain, devId, reqKey,
                tenantId, merchantId,
                memId, memPhone,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        verifyType = "independent";
        resStr = posUtil.checkCouponOrderForVerify(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId, verifyType,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }

        couponOrderId = "321877927162249216";
        resStr = posUtil.couponOrderPreVerify(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }

        couponOrderId = "321877927162249216";
        resStr = posUtil.couponOrderPreVerifyCancel(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }

        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderIndependentVerify(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId, posOrderId);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderPreVerify(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderPayVerifyOne(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderVerifyCancel(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId, posOrderId);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }


        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderPreVerify(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        List<String> couponOrderIdList = new ArrayList<>();
        couponOrderIdList.add(couponOrderId);
        resStr = posUtil.couponOrderPayVerifyList(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderIdList,
                posOrderId, posAmount);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        couponOrderId = "321878223800205312";
        resStr = posUtil.couponOrderVerifyCancel(domain, devId, reqKey,
                tenantId, merchantId, buUserId,
                couponOrderId, posOrderId);
        if (resStr != null) {
            System.out.println(resStr);
            JSONObject retObj = JSON.parseObject(resStr);
            Map options = retObj;
            if (!WxPayment.verifyNotifyHMAC(options, resKey)) {
                System.out.println("verify sign error");
            } else {
                System.out.println("verify sign ok");
            }
        }
        */
    }
}
