package cn.pluss.platform.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import cn.pluss.platform.wechat.MicroPayCore;

public class WechatScanUserPayUtil {
	
	public static String wechatPayOfficial(String code,String sub_mch_id,String orderNumber,String storeName,String dealFee) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		  Calendar nowTime = Calendar.getInstance();
		  nowTime.add(Calendar.MINUTE, 1);
		Map<String, String> receiveDataMap = new HashMap<String, String>();
		receiveDataMap.put("appid", ParametersUtil.APPID);
		receiveDataMap.put("mch_id", ParametersUtil.PID);
		receiveDataMap.put("sub_mch_id", sub_mch_id);//"1556940571"
		receiveDataMap.put("nonce_str", StringUtil.genRandomNum().toUpperCase());
		receiveDataMap.put("body", storeName);
		receiveDataMap.put("detail", storeName);
		receiveDataMap.put("out_trade_no", orderNumber);
		receiveDataMap.put("total_fee", dealFee);
		receiveDataMap.put("spbill_create_ip", "60.168.58.93");
		receiveDataMap.put("auth_code", code);
		receiveDataMap.put("time_expire", sdf.format(nowTime.getTime()));
		receiveDataMap.put("sign_type", "HMAC-SHA256");
		receiveDataMap.put("sign", WechatSignUtil.sha256Sign(receiveDataMap, ParametersUtil.KEY));

		return WXPayHttpUtil.WXHttpPostXMLWithoutCert("https://api.mch.weixin.qq.com/pay/micropay", receiveDataMap);
	}
	
	
	/**
	 * 
	 * getOenIdByAuthCode:(获取oenId). <br/> 
	 * 
	 * @author Administrator 
	 * @param sub_mch_id
	 * @param authCode
	 * @return 
	 * @since JDK 1.8
	 */
	public static String getOenIdByAuthCode(String sub_mch_id,String authCode) {
		Map<String, String> receiveDataMap = new HashMap<String, String>(16);
		receiveDataMap.put("appid", ParametersUtil.APPID);
		receiveDataMap.put("mch_id", ParametersUtil.PID);
		receiveDataMap.put("sub_mch_id", sub_mch_id);
		receiveDataMap.put("nonce_str", StringUtil.genRandomNum().toUpperCase());
		receiveDataMap.put("auth_code", authCode);
		Map<String, String> array = MicroPayCore.paraFilter(receiveDataMap);
		String sign = MicroPayCore.buildMysign(array, ParametersUtil.KEY);
		receiveDataMap.put("sign", sign);
		return WXPayHttpUtil.WXHttpPostXMLWithoutCert("https://api.mch.weixin.qq.com/tools/authcodetoopenid", receiveDataMap);
	}
	
	
	
}
