package com.winsun.tenpay;

import java.io.UnsupportedEncodingException;
import java.util.SortedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.winsun.tenpay.util.HttpUtil;

public class WxCorpRequestHandler extends NewIntfRequestHandler {
	public WxCorpRequestHandler(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
	}

	public String getXmlBody() throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();

		SortedMap map = super.getAllParameters();
		/*
		 * <xml> <mch_appid>wxe062425f740c30d8</mch_appid>
		 * <mchid>10000098</mchid>
		 * <nonce_str>3PG2J4ILTKCH16CQ2502SI8ZNMTM67VS</nonce_str>
		 * <partner_trade_no>100000982014120919616</partner_trade_no>
		 * <openid>ohO4Gt7wVPxIT1A9GjFaMYMiZY1s</openid>
		 * <check_name>OPTION_CHECK</check_name> <re_user_name>张三</re_user_name>
		 * <amount>100</amount> <desc>节日快乐!</desc>
		 * <spbill_create_ip>10.2.3.10</spbill_create_ip>
		 * <sign>C97BDBACF37622775366F38B629F45E3</sign> </xml>
		 */

		sb.append("<xml>\r\n");
		sb.append("<mch_appid>" + map.get("mch_appid") + "</mch_appid>" + "\r\n");
		sb.append("<mchid>" + map.get("mchid") + "</mchid>" + "\r\n");

		sb.append("<nonce_str>" + map.get("nonce_str") + "</nonce_str>" + "\r\n");

		sb.append("<partner_trade_no>" + map.get("partner_trade_no") + "</partner_trade_no>" + "\r\n");

		sb.append("<openid>" + map.get("openid") + "</openid>" + "\r\n");

		sb.append("<check_name>" + map.get("check_name") + "</check_name>" + "\r\n");

		sb.append("<re_user_name>" + map.get("re_user_name") + "</re_user_name>" + "\r\n");
		sb.append("<amount>" + map.get("amount") + "</amount>" + "\r\n");

		sb.append("<desc>" + map.get("desc") + "</desc>" + "\r\n");

		sb.append("<spbill_create_ip>" + map.get("spbill_create_ip") + "</spbill_create_ip>" + "\r\n");

		sb.append("<sign>" + map.get("sign") + "</sign>" + "\r\n");

		sb.append("</xml>");

		return sb.toString();
	}

	public String sendPost() throws Exception {
		createSign();
		// return HttpUtil.doPostXml(getGateUrl(), getXmlBody());

		return HttpUtil.httpPost(getGateUrl(), getXmlBody());
	}
}
