package cn.hmsoft.pay.impl.weixin.entity;

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

import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.RandomHelper;
import cn.hmsoft.helper.SecureHelper;
import cn.hmsoft.pay.impl.PayChannelAbstract;
import cn.hmsoft.pay.util.PayUtil;

public class WeiXinJsApiPay {
	private String appId;
	private String timeStamp;
	private String nonceStr;
	private String package_r;
	private String signType = "MD5";
	private String paySign;

	public void sign(String merchant_id,String channel_id) {
		PayChannelAbstract pay =  PayUtil.getPayChannelImpl(merchant_id, channel_id);
		this.appId = pay.getPayMerchantChannel().getBank_merchant_id();
		FrameAssertUtil.isNotNull(pay, "该商户的微信支付未启用，请选择其他支付方式！");
		this.nonceStr = RandomHelper.getRandomValue(16);
		this.timeStamp = new Date().getTime() / 1000 + "";
		String[] array = new String[] { "appId", "timeStamp", "nonceStr",
				"package", "signType" };
		// 对参数进行排序
		java.util.Arrays.sort(array);
		Map<String, String> map = new HashMap<String, String>();
		map.put("nonceStr", nonceStr);
		map.put("timeStamp", timeStamp + "");
		map.put("package", package_r);
		map.put("signType", signType);
		map.put("appId", this.appId);

		String total = "";
		for (String string : array) {
			total = total + string + "=" + map.get(string) + "&";
		}
		total = total + "key=" +pay.getPayMerchantChannel().getBank_merchant_pass();
		// System.out.println(total);
		paySign = SecureHelper.md5(total);
	}

	public String getAppId() {
		return appId;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getNonceStr() {
		return nonceStr;
	}

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

	public String getPackage_r() {
		return package_r;
	}

	public void setPackage_r(String package_r) {
		this.package_r = package_r;
	}

	public String getSignType() {
		return signType;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public String getPaySign() {
		return paySign;
	}

	public void setPaySign(String paySign) {
		this.paySign = paySign;
	}

}
