/* 
 * Copyrights 尚果科技平台研发项目组 @ 2015，尚果科技 SEOCOO Computer Co., Ltd.<br>
 * 项目名称：尚果科技-平台开发组 <br>
 * 文件名称：PayUnitedRequest.java <br>
 * 创建时间：2015-6-15-下午03:58:22 <br>
 * Description： 
 * History：<br> [ Author Date Version Content ]<br>
 *  
 */
package cn.pluss.platform.wap;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.wechat.PayInfo;
public class PayUnitedRequest {
	
	 private static final Logger logger = LoggerFactory.getLogger( PayUnitedRequest.class);

	private String appid;
	private String mch_id;
	private String device_info;
	private String nonce_str;
	private String body;
	private String out_trade_no;
	private String total_fee;
	private String spbill_create_ip;
	private String notify_url;
	private String trade_type;
	private String openid;

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMch_id() {
		return mch_id;
	}

	public void setMch_id(String mch_id) {
		this.mch_id = mch_id;
	}

	public String getNonce_str() {
		return nonce_str;
	}

	public void setNonce_str(String nonce_str) {
		this.nonce_str = nonce_str;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	public String getTotal_fee() {
		return total_fee;
	}

	public void setTotal_fee(String total_fee) {
		this.total_fee = total_fee;
	}

	public String getSpbill_create_ip() {
		return spbill_create_ip;
	}

	public void setSpbill_create_ip(String spbill_create_ip) {
		this.spbill_create_ip = spbill_create_ip;
	}

	public String getNotify_url() {
		return notify_url;
	}

	public void setNotify_url(String notify_url) {
		this.notify_url = notify_url;
	}

	public String getTrade_type() {
		return trade_type;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public void setTrade_type(String trade_type) {
		this.trade_type = trade_type;
	}

	public String getDevice_info() {
		return device_info;
	}

	public void setDevice_info(String device_info) {
		this.device_info = device_info;
	}

	@Override
	public String toString() {
		return "PayUnitedRequest [appid=" + appid + ", mch_id=" + mch_id + ", device_info=" + device_info
				+ ", nonce_str=" + nonce_str + ", body=" + body + ", out_trade_no=" + out_trade_no + ", total_fee="
				+ total_fee + ", spbill_create_ip=" + spbill_create_ip + ", notify_url=" + notify_url + ", trade_type="
				+ trade_type + ", openid=" + openid + "]";
	}

	public Map<String, String> getMap(PayInfo payInfo, String notify_url, String ip) {
		Double orderPrice = payInfo.getOrderPrice();
		int price=(int) (orderPrice*100);
		String billno = StringUtil.getBillno();
		Map<String, String> map = new HashMap<String, String>(16);
		map.put("body", payInfo.getGoodsName());// 商品描述
		map.put("sub_mch_id", payInfo.getSubMchId());
		map.put("mch_id", payInfo.getPid());// 商户平台id
		map.put("appid", payInfo.getAppId());// 公众号id
		map.put("nonce_str", billno);// 随机字符串
		map.put("notify_url", notify_url);// 异步回调api
		map.put("spbill_create_ip", ip);// 支付ip
		map.put("out_trade_no", payInfo.getOrderNumber());// 商品订单号
		map.put("total_fee", price+"");// 真实金额
		map.put("trade_type", "JSAPI");// JSAPI、h5调用
		map.put("openid", payInfo.getOpenId());
		System.out.println(map);
		String sign = WxPaySignatureUtils.signature(map, payInfo.getPkey());
		System.out.println(sign);
		StringBuffer xml = new StringBuffer();
		xml.append("<xml>");
		xml.append("<appid>" + payInfo.getAppId() + "</appid>");
		xml.append("<body>" + payInfo.getGoodsName() + "</body>");
		xml.append("<sub_mch_id>"+payInfo.getSubMchId()+"</sub_mch_id>");
		xml.append("<mch_id>" + payInfo.getPid() + "</mch_id>");
		xml.append("<nonce_str>" + billno + "</nonce_str>");
		xml.append("<notify_url>" + notify_url + "</notify_url>");
		xml.append("<openid>" + payInfo.getOpenId() + "</openid>");
		xml.append("<out_trade_no>"+payInfo.getOrderNumber()+"</out_trade_no>");
		xml.append("<spbill_create_ip>" + ip + "</spbill_create_ip>");
		xml.append("<total_fee>" + price + "</total_fee>");
		xml.append("<trade_type>JSAPI</trade_type>");
		xml.append("<sign>" + sign + "</sign>");
		xml.append("</xml>");
		map.put("xml", xml.toString());
		return map;
	}
	
	
	public Map<String, String> getMerchantMap(MerchantWechatPayDTO merchantWechatPayDTO,String storeName,String pkey) {
		String orderPrice = merchantWechatPayDTO.getComsumeMoney();
		int price=(int) (Double.valueOf(orderPrice)*100);
		String billno = StringUtil.getBillno();
		Map<String, String> map = new HashMap<String, String>(16);
		map.put("body",storeName);//商品描述
		map.put("detail",storeName);//商品详情
		map.put("sub_mch_id", merchantWechatPayDTO.getSubMchId());
		map.put("mch_id",merchantWechatPayDTO.getMchId());// 商户平台id
		map.put("appid", merchantWechatPayDTO.getAppid());// 公众号id
		map.put("nonce_str", billno);// 随机字符串
		map.put("notify_url", merchantWechatPayDTO.getNotifyUrl());// 异步回调api
		map.put("spbill_create_ip", merchantWechatPayDTO.getIp());// 支付ip
		map.put("out_trade_no", merchantWechatPayDTO.getOrderSn());// 商品订单号
		map.put("total_fee", price+"");// 真实金额
		map.put("trade_type", "JSAPI");// JSAPI、h5调用
		map.put("openid", merchantWechatPayDTO.getOpenid());
		System.out.println(map);
		String sign = WxPaySignatureUtils.signature(map, pkey);
		System.out.println(sign);
		StringBuffer xml = new StringBuffer();
		xml.append("<xml>");
		xml.append("<appid>" + merchantWechatPayDTO.getAppid() + "</appid>");
		xml.append("<body>"+storeName+"</body>");
		xml.append("<detail>"+storeName+"</detail>");
		xml.append("<sub_mch_id>"+merchantWechatPayDTO.getSubMchId()+"</sub_mch_id>");
		xml.append("<mch_id>" + merchantWechatPayDTO.getMchId() + "</mch_id>");
		xml.append("<nonce_str>" + billno + "</nonce_str>");
		xml.append("<notify_url>" + merchantWechatPayDTO.getNotifyUrl() + "</notify_url>");
		xml.append("<openid>" + merchantWechatPayDTO.getOpenid() + "</openid>");
		xml.append("<out_trade_no>"+merchantWechatPayDTO.getOrderSn()+"</out_trade_no>");
		xml.append("<spbill_create_ip>" + merchantWechatPayDTO.getIp() + "</spbill_create_ip>");
		xml.append("<total_fee>" + price + "</total_fee>");
		xml.append("<trade_type>JSAPI</trade_type>");
		xml.append("<sign>" + sign + "</sign>");
		xml.append("</xml>");
		map.put("xml", xml.toString());
		return map;
	}
	

}
