package com.winsun.tenpay.business;

import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.log4j.Logger;

import com.winsun.tenpay.CorporatePrepayResponse;
import com.winsun.tenpay.TenpayNotify;
import com.winsun.tenpay.WxCorpRequestHandler;
import com.winsun.tenpay.util.ConstantUtil;
import com.winsun.tenpay.util.HttpUtil;
import com.winsun.tenpay.util.StringUtil;
import com.winsun.tenpay.util.WXUtil;
import com.winsun.tenpay.util.XmlParser;

public class TenpayXyjlBusiness {
	private static Logger log = Logger.getLogger(TenpayXyjlBusiness.class);

	public static CorporatePrepayResponse xyjlTenPayRequest(String orderno, String openid, String check_name,
			String re_user_name, int amount, String desc, String createIp) throws Exception {

		CorporatePrepayResponse response = new CorporatePrepayResponse();
		WxCorpRequestHandler handler = new WxCorpRequestHandler(null, null);
		handler.setGateUrl(ConstantUtil.PAY_URL);
		handler.setKey(ConstantUtil.APP_KEY);
		handler.setParameter("mch_appid", ConstantUtil.APP_ID);
		handler.setParameter("mchid", ConstantUtil.MCH_ID);
		handler.setParameter("nonce_str", WXUtil.getNonceStr());
		handler.setParameter("partner_trade_no", orderno);
		handler.setParameter("openid", openid);
		handler.setParameter("check_name", check_name);
		handler.setParameter("re_user_name", re_user_name);
		handler.setParameter("amount", amount + "");
		// handler.setParameter("desc", URLEncoder.encode(desc,"UTF-8"));
		handler.setParameter("desc", desc);
		handler.setParameter("spbill_create_ip", createIp);
		// handler.setParameter("notify_url", notifyUrl);
		String xml = handler.sendPost();
		//String xml = "";
		
		String err_code = "";
		String err_code_des = "";
		
		try {
			XmlParser xmlParser = new XmlParser(xml);
			String payment_no = xmlParser.getSingleValue("//payment_no");// 企业付款成功，返回的微信订单号

			if (StringUtil.isBlank(payment_no)) {
				err_code = xmlParser.getSingleValue("//err_code");
				err_code_des = xmlParser.getSingleValue("//err_code_des");
				response.setErrCodeDesc(err_code_des);
				
				//log.info("支付失败 ,code=" + err_code + "失败原因：msg= " + err_code_des);
			}

			String partner_trade_no = xmlParser.getSingleValue("//partner_trade_no");
			String mchid = xmlParser.getSingleValue("//mchid");

			String mch_appid = xmlParser.getSingleValue("//mch_appid");

			String device_info = xmlParser.getSingleValue("//device_info");
			String payment_time = xmlParser.getSingleValue("//payment_time");
			String nonce_str = xmlParser.getSingleValue("//nonce_str");

			SortedMap<String, String> paramMap = new TreeMap();
			paramMap.put("appid", ConstantUtil.APP_ID);
			paramMap.put("timestamp", System.currentTimeMillis() / 1000L + "");
			paramMap.put("noncestr", nonce_str);
			paramMap.put("mchid", mchid);
			paramMap.put("mchAppid", mch_appid);
			paramMap.put("partner_trade_no", partner_trade_no);
			paramMap.put("payment_no", payment_no);
			paramMap.put("deviceInfo", device_info);
			paramMap.put("payment_time", payment_time);

			TenpayNotify.buildSign(paramMap);
			response.setAppId((String) paramMap.get("appid"));
			response.setAppKey((String) paramMap.get("appkey"));
			response.setNonceStr((String) paramMap.get("noncestr"));

			response.setPaymentNo((String) paramMap.get("payment_no"));
			response.setTimeStamp((String) paramMap.get("payment_time"));
			response.setPaySign((String) paramMap.get("sign"));
			response.setAmount(amount);
			response.setOpenid(openid);

			return response;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public static String query(String out_trade_no, String tenpay_no) {
		String url = ConstantUtil.QUERY_GATEURL;
		String app_id = ConstantUtil.APP_ID;
		String partner = ConstantUtil.MCH_ID;
		String nonce_str = WXUtil.getNonceStr();
		SortedMap<String, String> paramMap = new TreeMap();
		paramMap.put("appid", app_id);
		paramMap.put("mch_id", partner);
		paramMap.put("transaction_id", tenpay_no);
		paramMap.put("out_trade_no", out_trade_no);
		paramMap.put("nonce_str", nonce_str);
		String sign = TenpayNotify.buildTradeSign(paramMap);
		StringBuffer xml = new StringBuffer();
		xml.append("<xml><appid>" + app_id + "</appid><mch_id>" + partner + "</mch_id>");
		xml.append("<nonce_str>" + nonce_str + "</nonce_str><out_trade_no>" + out_trade_no + "</out_trade_no>");
		xml.append("<sign>" + sign + "</sign>" + "</xml>");
		return HttpUtil.doPostXml(url, xml.toString());
	}

	public static void main(String[] args) throws Exception {
		String orderNo = "1000018284310";
		String desc = "test201630";
		int amount = 101;
		String openid = "oJodewFb4QpzLzcp4neLllT1XDoc";
		String createIp = "10.17.66.44";
		String check_name = "NO_CHECK";
		String re_user_name = "test1";
		String notifyUrl = "dianyuanjiangli.com";

		xyjlTenPayRequest(orderNo, openid, check_name, re_user_name, amount, desc, createIp);

		// xyWapRequest( orderNo, amount, desc, "", createIp, openid,
		// notifyUrl);
	}
}
