/**
* 微信支付信息封装。java版
*
* @return
* @throws IOException
* @throws QQConnectException
*/
@SuppressWarnings("unchecked")
public String wxPaymentPackage() throws IOException, QQConnectException {
logger.error("token token========" + token);
Account account = (Account) getSession().getAttribute(FrontContainer.USER_INFO);
token = (Token) getSession().getAttribute(ConfigUtil.APPID + "token");
logger.error("token token========" + token);
if (account == null || StringUtils.isBlank(account.getAccount()) || token == null) {
return this.wxLogin();
}
//自己订单信息自己封装
order = (Order) getSession().getAttribute("order");// 订单信息
getSession().setAttribute("order", null);
ordership = (Ordership) getSession().getAttribute("ordership");// 配送信息
getSession().setAttribute("ordership", null);
logger.error("微信 payapireturnurl.order.id ============" + order.getId());
logger.error("微信 payapireturnurl.ordership.id ============" + ordership.getId());
// 封装
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
parameters.put("appid", ConfigUtil.APPID);
parameters.put("mch_id", ConfigUtil.MCH_ID);
parameters.put("device_info", "WEB");
parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
parameters.put("body", order.getRemark());
parameters.put("out_trade_no", order.getId() + 12);
parameters.put("total_fee", String.valueOf((int) (Float.parseFloat(order.getAmount()) * 100)));// order.getTotal()
// 必须是字符串
// 否则报转换错误
// 单位为分;
String spbill_create_ip = AddressUtils.getIp(getRequest());
parameters.put("spbill_create_ip", spbill_create_ip);
parameters.put("notify_url", ConfigUtil.NOTIFY_URL);
parameters.put("trade_type", "JSAPI");
parameters.put("openid", token.getOpenId());// 需要获取
String sign = PayCommonUtil.createSign("UTF-8", parameters);
parameters.put("sign", sign);
String requestXML = PayCommonUtil.getRequestXml(parameters);
logger.error("requestXML ===" + requestXML);
String result = CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML);
Map<String, String> map = new HashMap();
String json = "";
try {
map = XMLUtil.doXMLParse(result);
} catch (JDOMException e) {
logger.error("JDOMException===" + e.getMessage());
throw new NullPointerException("JDOMException参数异常");
} catch (IOException e) {
logger.error("IOException====" + e.getMessage());
throw new NullPointerException("IOException参数异常");
}// 解析微信返回的信息,以Map形式存储便于取值
SortedMap<Object, Object> params = new TreeMap<Object, Object>();
String timeStamp = Long.toString(new Date().getTime()).substring(0, 10);
String nonceStr = PayCommonUtil.CreateNoncestr();
if (!map.isEmpty()) {
params.put("appId", ConfigUtil.APPID);
params.put("timeStamp", timeStamp);
params.put("nonceStr", nonceStr);
params.put("package", "prepay_id=" + map.get("prepay_id"));
params.put("signType", ConfigUtil.SIGN_TYPE);
String paySign = PayCommonUtil.createSign("UTF-8", params);
params.put("packageValue", "prepay_id=" + map.get("prepay_id")); // 这里用packageValue是预防package是关键字在js获取值出错
params.put("paySign", paySign); // paySign的生成规则和Sign的生成规则一致
params.put("sendUrl", ConfigUtil.SUCCESS_URL); // 付款成功后跳转的页面
params.put("orderid", order.getId()); // 订单号
String userAgent = getRequest().getHeader("user-agent");
char agent = userAgent.charAt(userAgent.indexOf("MicroMessenger") + 15);
params.put("agent", new String(new char[] { agent }));// 微信版本号,用于前面提到的判断用户手机微信的版本是否是5.0以上版本。
}
json = JSONObject.fromObject(params).toString();
logger.error("json===" + json);
getRequest().setAttribute("json", json);
return null;
}