package com.iformall.pos.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iformall.PosApplication;
import com.iformall.enums.EnumNeuPosPamentType;
import com.iformall.enums.EnumPosSceneType;
import com.iformall.pay.WxPayConstant;
import com.iformall.pay.WxPayment;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Stormeye
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { PosApplication.class})
@ActiveProfiles("dev")
@AutoConfigureMockMvc
@FixMethodOrder(MethodSorters.JVM)
public class PosAppTest {

	@Autowired
	private MockMvc mockMvc;

	private static final SimpleDateFormat neuDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	private static final String devId = "neupos";
	private static final String devReqKey = "qVH2YDmXJ$ok4a95";
	private static final String devResKey = "VE#1fB2PhUe@&3Of";

	private static final String tenantId = "456";
	private static final String merchantId = "320833942159982592"; // 东软专用
	private static final String buUserId = "320834180694245376";
	private static final String phone = "13120223636";
	private static final String password = "drpos345";

	private static final String posSN = "1";

	/*
	可用的交易券, 支付可用(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
	*/

	@Test
	public void versionTest() throws Exception	{
		// 1. version test
		this.mockMvc
				.perform(MockMvcRequestBuilders.get("/version"))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.content().string("{\"code\":200,\"message\":\"success\",\"data\":\"1.0\"}"))
				.andDo(MockMvcResultHandlers.print());

	}

	@Test
	public void signErrorTest() throws Exception	{
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.DEV_ID, "");
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, "");
		reqObj.put(WxPayConstant.COUPON_ORDER_ID, "");
		reqObj.put(WxPayConstant.NONCE_STR, "");
		reqObj.put(WxPayConstant.POS_ORDER_ID, "");
		reqObj.put(WxPayConstant.SIGN, WxPayment.createSignHMAC(reqObj, devReqKey));
		String reqJsonStr = JSONObject.toJSONString(reqObj);
		this.mockMvc.perform(
				MockMvcRequestBuilders.post("/checkCouponOrderForVerify")
				.contentType(MediaType.APPLICATION_JSON)
				.content(reqJsonStr)
		)
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				.andExpect(MockMvcResultMatchers.jsonPath("$.return_code").value("FAIL"))
				.andDo(MockMvcResultHandlers.print());
	}

	@Test
	public void checkUserPasswordTest() throws Exception {
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.PHONE, phone);
        reqObj.put(WxPayConstant.PASSWORD, password);
		boolean resSigned = sendAndRes("/checkUserPassword", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void payCalculate1Test() throws Exception	{
		String sceneType = String.valueOf(EnumPosSceneType.PAY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "123456");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.MEM_PHONE, "18240016029");
		reqObj.put(WxPayConstant.ORDER_AMOUNT, "100");
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, "100");
		boolean resSigned = sendAndRes("/queryAndCalc", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void verifyCalculateTest() throws Exception	{
		String selCoList = "[\"321872988134408192\"]";
		String sceneType = String.valueOf(EnumPosSceneType.VERIFY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.COUPON_ORDER_LIST, selCoList);
		boolean resSigned = sendAndRes("/queryAndCalc", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void payCalculateTest_NoCouponOrder() throws Exception	{
		//String memId = "321178010928119808";
		//String phone = "13910154397";
		String cardId = "321883878446825472";
		String orderAmount = "2000";
		String orderAmountLeft = "2000";
		String selCoList = "[]";
		String sceneType = String.valueOf(EnumPosSceneType.PAY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		//reqObj.put(WxPayConstant.MEM_ID, memId);
		//reqObj.put(WxPayConstant.MEM_PHONE, phone);
		reqObj.put(WxPayConstant.CARD_ID, cardId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmountLeft);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.COUPON_ORDER_LIST, selCoList);
		boolean resSigned = sendAndRes("/queryAndCalc", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void payCalculateTest_WithOneCouponOrder() throws Exception	{
		String memId = "321178010928119808";
		String phone = "13910154397";
		String orderAmount = "20000";
		String orderAmountLeft = "20000";
		String selCoList = "[\"321878185355214848\"]";
		String sceneType = String.valueOf(EnumPosSceneType.PAY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.MEM_ID, memId);
		reqObj.put(WxPayConstant.MEM_PHONE, phone);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmountLeft);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.COUPON_ORDER_LIST, selCoList);
		boolean resSigned = sendAndRes("/queryAndCalc", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void verifyTest() throws Exception {
		String posOrderId = "1";
		String selCoList = "[\"321878223800205312\"]";
		String sceneType = String.valueOf(EnumPosSceneType.VERIFY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.COUPON_ORDER_LIST, selCoList);
		boolean resSigned = sendAndRes("/couponOrderVerify", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void couponOrderVerifyCancelTest() throws Exception	{
		String posOrderId = "1";
		String couponOrderId = "321878223800205312";
		String orderAmount = "1000";
		String amountLeft = "0";
		String sceneType = String.valueOf(EnumPosSceneType.VERIFY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		JSONObject paymentInfo = new JSONObject();
		paymentInfo.put(WxPayConstant.COUPON_ID, couponOrderId);
		paymentInfo.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		paymentInfo.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmount);
		paymentInfo.put(WxPayConstant.PROMOTION_AMOUNT, orderAmount);
		paymentInfo.put(WxPayConstant.AMOUNT_LEFT_AFTER_PAY, amountLeft);
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.COUPON_ORDER_ID, couponOrderId);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.PAYMENT_INFO, JSON.toJSONString(paymentInfo));
		boolean resSigned = sendAndRes("/couponOrderVerifyCancel", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void verifySyncTest() throws Exception	{
		String memId = "321178010928119808";
		String memPhone = "13910154397";
		String sceneType = "0"; // 核销
		String posOrderId = "1";
		String posAmount = "1000";
		String posPayOrderId = "1";
		String posPaymentType = "4";
		String posPaymentTypeDes = "优惠券";
		String posPaymentStatus = "0";

		String couponOrderId = "321878223800205312";

		Date curDate = new Date();

		Map<String, String> payDetailOne = new HashMap<>();
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_ID, posPayOrderId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TIME, neuDateFormat.format(curDate));
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE, posPaymentType);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE_DES, posPaymentTypeDes);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_PARAM, "{}");
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_STATUS, posPaymentStatus);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_NO, couponOrderId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_AMOUNT, posAmount);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_TIME, neuDateFormat.format(curDate));

		JSONArray payDetailList = new JSONArray();
		payDetailList.add(payDetailOne);

		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.MEM_ID, memId);
		reqObj.put(WxPayConstant.MEM_PHONE, memPhone);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.POS_ORDER_TIME, neuDateFormat.format(curDate));
		reqObj.put(WxPayConstant.ORDER_AMOUNT, posAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.ORDER_STATUS, "0");
		JSONObject payDetail = new JSONObject();
		payDetail.put(WxPayConstant.PAY_DETAIL_LIST, JSON.toJSONString(payDetailList));
		reqObj.put(WxPayConstant.PAY_DETAIL, JSON.toJSONString(payDetail));
		boolean resSigned = sendAndRes("/posOrderSync", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void payVerifyTest() throws Exception {
		String memId = "321178010928119808";
		String memPhone = "13910154397";
		String posOrderId = "2";
		String orderAmount = "1000";
		String selCoList = "[\"321878223800205312\"]";
		String sceneType = String.valueOf(EnumPosSceneType.PAY.getCode());
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.MEM_ID, memId);
		reqObj.put(WxPayConstant.MEM_PHONE, memPhone);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.COUPON_ORDER_LIST, selCoList);
		boolean resSigned = sendAndRes("/couponOrderVerify", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	@Test
	public void payVerifySyncTest() throws Exception	{
		String memId = "321178010928119808";
		String memPhone = "13910154397";
		String sceneType = "1"; // 支付
		String posOrderId = "2";
		String orderAmount = "1000";
		String posPayOrderId = "2";
		String posPaymentType = String.valueOf(EnumNeuPosPamentType.MEM_COUPON.getCode());
		String posPaymentTypeDes = EnumNeuPosPamentType.MEM_COUPON.getMessage();
		String posPaymentStatus = "0";

		String couponOrderId = "321878223800205312";

		Date curDate = new Date();

		Map<String, String> payDetailOne = new HashMap<>();
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_ID, posPayOrderId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TIME, neuDateFormat.format(curDate));
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE, posPaymentType);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE_DES, posPaymentTypeDes);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_PARAM, "{}");
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_STATUS, posPaymentStatus);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_NO, couponOrderId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_AMOUNT, orderAmount);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_TIME, neuDateFormat.format(curDate));

		JSONArray payDetailList = new JSONArray();
		payDetailList.add(payDetailOne);

		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.MEM_ID, memId);
		reqObj.put(WxPayConstant.MEM_PHONE, memPhone);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.POS_ORDER_TIME, neuDateFormat.format(curDate));
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.ORDER_STATUS, "0");
		JSONObject payDetail = new JSONObject();
		payDetail.put(WxPayConstant.PAY_DETAIL_LIST, JSON.toJSONString(payDetailList));
		reqObj.put(WxPayConstant.PAY_DETAIL, JSON.toJSONString(payDetail));
		boolean resSigned = sendAndRes("/posOrderSync", reqObj);
		Assert.assertEquals(resSigned, true);
	}

    @Test
    public void cardPayPreTest() throws Exception	{
        String cardId = "321883878446825472";
		String sceneType = "1"; // 支付
        String posOrderId = "1";
        String orderAmount = "2000";
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.CARD_ID, cardId);
		reqObj.put(WxPayConstant.AMOUNT_PAID_BY_CARD, orderAmount);
		boolean resSigned = sendAndRes("/cardPayPre", reqObj);
		Assert.assertEquals(resSigned, true);
    }

    @Test
    public void cardPayPreCancelTest() throws Exception	{
        String cardId = "321883878446825472";
        String cardSpendId = "329763372332511232";
		String sceneType = "1"; // 支付
        String posOrderId = "1";
		String orderAmount = "2000";
		JSONObject paymentInfo = new JSONObject();
		paymentInfo.put(WxPayConstant.CARD_ID, cardId);
		paymentInfo.put(WxPayConstant.CARD_SPEND_ID, cardSpendId);
		paymentInfo.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		paymentInfo.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmount);
		paymentInfo.put(WxPayConstant.AMOUNT_PAID_BY_CARD, orderAmount);
		paymentInfo.put(WxPayConstant.AMOUNT_LEFT_AFTER_PAY, "0");
		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.PAYMENT_INFO, JSON.toJSONString(paymentInfo));
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_AMOUNT_LEFT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		boolean resSigned = sendAndRes("/cardPayPreCancel", reqObj);
		Assert.assertEquals(resSigned, true);
    }

	@Test
	public void cardPaySyncTest() throws Exception	{
		String memId = "321178010928119808";
		String memPhone = "13910154397";
		String sceneType = "1"; // 支付
		String posOrderId = "1";
		String orderAmount = "2000";
		String posPayOrderId = "2";
		String posPaymentType = String.valueOf(EnumNeuPosPamentType.MEM_CARD.getCode());
		String posPaymentTypeDes = EnumNeuPosPamentType.MEM_CARD.getMessage();
		String posPaymentStatus = "0";
		String cardSpendId = "329763372332511232";

		Date curDate = new Date();

		Map<String, String> payDetailOne = new HashMap<>();
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_ID, posPayOrderId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TIME, neuDateFormat.format(curDate));
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE, posPaymentType);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_TYPE_DES, posPaymentTypeDes);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_PARAM, "{}");
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_STATUS, posPaymentStatus);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_NO, cardSpendId);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_AMOUNT, orderAmount);
		payDetailOne.put(WxPayConstant.NEU_PAY_ORDER_OUT_TIME, neuDateFormat.format(curDate));

		JSONArray payDetailList = new JSONArray();
		payDetailList.add(payDetailOne);

		Map<String, String> reqObj = new HashMap<>();
		reqObj.put(WxPayConstant.NONCE_STR, "1");
		reqObj.put(WxPayConstant.DEV_ID, devId);
		reqObj.put(WxPayConstant.TENANT_ID, tenantId);
		reqObj.put(WxPayConstant.MERCHANT_ID, merchantId);
		reqObj.put(WxPayConstant.BUSER_ID, buUserId);
		reqObj.put(WxPayConstant.SCENE_TYPE, sceneType);
		reqObj.put(WxPayConstant.MEM_ID, memId);
		reqObj.put(WxPayConstant.MEM_PHONE, memPhone);
		reqObj.put(WxPayConstant.POS_ORDER_ID, posOrderId);
		reqObj.put(WxPayConstant.POS_ORDER_TIME, neuDateFormat.format(curDate));
		reqObj.put(WxPayConstant.ORDER_AMOUNT, orderAmount);
		reqObj.put(WxPayConstant.ORDER_CREATE_SN, posSN);
		reqObj.put(WxPayConstant.ORDER_STATUS, "0");
		JSONObject payDetail = new JSONObject();
		payDetail.put(WxPayConstant.PAY_DETAIL_LIST, JSON.toJSONString(payDetailList));
		reqObj.put(WxPayConstant.PAY_DETAIL, JSON.toJSONString(payDetail));
		boolean resSigned = sendAndRes("/posOrderSync", reqObj);
		Assert.assertEquals(resSigned, true);
	}

	private boolean sendAndRes(String url, Map<String, String> reqObj) throws Exception {
		reqObj.put(WxPayConstant.SIGN, WxPayment.createSignHMAC(reqObj, devReqKey));
		String reqJsonStr = JSONObject.toJSONString(reqObj);
		MvcResult result = mockMvc.perform(
				MockMvcRequestBuilders.post(url)
						.contentType(MediaType.APPLICATION_JSON)
						.content(reqJsonStr))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andDo(MockMvcResultHandlers.print())
				.andReturn();
		String responseStr = result.getResponse().getContentAsString();
		ObjectMapper mapper = new ObjectMapper();
		Map<String,String> respMap = mapper.readValue(responseStr, Map.class);
		return WxPayment.verifyNotifyHMAC(respMap, devResKey);
	}
}
