package cn.pluss.platform.wap;

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

import cn.pluss.platform.entity.MerchantOrder;
import cn.pluss.platform.util.StringUtil;

/**
 * @author yuchen
 *
 * @Description 
 * (wap端微信支付)
 */
public class MerchantWechatPayDTO {
    
	/**
	 * 支付金额
	 */
	private String comsumeMoney;
	
	/**
	 * 支付宝用户openid
	 */
	private String openid;
	
	/**
	 * 自己的数据库的商户code
	 */
	private String merchantCode;
	
	private String storeId;
	
	
	/**
	 * 随机字符串
	 */
	private String nonceStr;
	
	/**
	 * 商品描述
	 */
	private String body="payment";
	
	/**
	 * 商户平台id
	 */
	private String mchId;
	
	private String subMchId;
	
	
	/**
	 * 公众号id
	 */
	private String appid;
	
	/**
	 * 异步回调api
	 */
	private String notifyUrl;
	
	/**
	 * 用户支付ip
	 */
	private String ip;
	
	/**
	 * 商品订单号
	 */
	private String orderSn;
	
	private String userAppId;
	
	private String userImg;
	
	private String nickName;
	private String remark;
	
	
	public String getUserImg() {
		return userImg;
	}

	public void setUserImg(String userImg) {
		this.userImg = userImg;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getBody() {
		return body;
	}

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

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getAppid() {
		return appid;
	}

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

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getOrderSn() {
		return orderSn;
	}

	public void setOrderSn(String orderSn) {
		this.orderSn = orderSn;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getComsumeMoney() {
		return comsumeMoney;
	}

	public void setComsumeMoney(String comsumeMoney) {
		this.comsumeMoney = comsumeMoney;
	}

	public String getOpenid() {
		return openid;
	}

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

	public void setUserAppId(String userAppId) {
		this.userAppId = userAppId;
	}
	
	public String getStoreId() {
		return storeId;
	}

	public void setStoreId(String storeId) {
		this.storeId = storeId;
	}

	public String getSubMchId() {
		return subMchId;
	}

	public void setSubMchId(String subMchId) {
		this.subMchId = subMchId;
	}

	

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Override
	public String toString() {
		return "MerchantWechatPayDTO [comsumeMoney=" + comsumeMoney + ", openid=" + openid + ", merchantCode="
				+ merchantCode + ", nonceStr=" + nonceStr + ", body=" + body + ", mchId=" + mchId + ", subMchId="
				+ subMchId + ", appid=" + appid + ", notifyUrl=" + notifyUrl + ", ip=" + ip + ", orderSn=" + orderSn
				+ ", userAppId=" + userAppId + "]";
	}

	public MerchantOrder getMerchantOrder() {
		MerchantOrder merchantOrder=new MerchantOrder();
		merchantOrder.setConsumeFee(this.comsumeMoney==""?0.0d:Double.valueOf(this.comsumeMoney));
		merchantOrder.setMerchantCode(this.merchantCode);
		merchantOrder.setPayTypeCode("wechat");
		merchantOrder.setPayTypeName("1");
		merchantOrder.setStatus("5");
		merchantOrder.setOrderType("2");
		merchantOrder.setUserImg(userImg);
		merchantOrder.setNickName(nickName);
		merchantOrder.setOrderNumber(this.orderSn);
		merchantOrder.setRemark(remark);
		return merchantOrder;
	}
	
	
	public Map<String, String> getMap(MerchantWechatPayDTO merchantWechatPayDTO,String WX_PAY_KEY){
		String nonceStr = StringUtil.generateMixStr(10);
		String orderSn="W"+StringUtil.getBillno();
		String comsumeMoney = merchantWechatPayDTO.getComsumeMoney();
		Double money = Double.valueOf(comsumeMoney);
		int i=(int) (money*100);
		Map<String, String> map=new HashMap<String, String>(16);
		map.put("body", merchantWechatPayDTO.getBody());//商品描述
		map.put("mch_id", merchantWechatPayDTO.getMchId());//商户平台id
		map.put("sub_mchid", merchantWechatPayDTO.getSubMchId());
		map.put("appid", merchantWechatPayDTO.getAppid());//公众号id
		map.put("nonce_str", nonceStr);//随机字符串
		map.put("notify_url", merchantWechatPayDTO.getNotifyUrl());//异步回调api
		map.put("spbill_create_ip", merchantWechatPayDTO.getIp());//支付ip
		map.put("out_trade_no", orderSn);//商品订单号
		map.put("total_fee", i+"");//真实金额
		map.put("trade_type", "JSAPI");//JSAPI、h5调用
		map.put("openid", merchantWechatPayDTO.getOpenid());
		String sign = WxPaySignatureUtils.signature(map, WX_PAY_KEY);
		StringBuffer xml=new StringBuffer();
		xml.append("<xml>");
		xml.append("<appid>"+ merchantWechatPayDTO.getAppid() +"</appid>");
		xml.append("<body>"+ merchantWechatPayDTO.getBody() +"</body>");
		xml.append("<mch_id>"+ merchantWechatPayDTO.getMchId() +"</mch_id>");
		xml.append("<sub_mch_id>"+ merchantWechatPayDTO.getSubMchId() +"</sub_mch_id>");
		xml.append("<nonce_str>"+ nonceStr +"</nonce_str>");
		xml.append( "<notify_url>"+ merchantWechatPayDTO.getNotifyUrl() +"</notify_url>");
		xml.append("<openid>"+ merchantWechatPayDTO.getOpenid() +"</openid>");
		xml.append("<out_trade_no>"+ orderSn +"</out_trade_no>");
		xml.append("<spbill_create_ip>"+ merchantWechatPayDTO.getIp() +"</spbill_create_ip>");
		xml.append("<total_fee>"+ i +"</total_fee>");
		xml.append("<trade_type>JSAPI</trade_type>");
		xml.append("<sign>"+ sign +"</sign>");
		xml.append("</xml>");
		map.put("xml", xml.toString());
		return map;
	}
	
}

