public static String wxCloseOrder(Order o) { Map<String, Object> map = new HashMap<String, Object>(); map.put("appid", Constant.W_APP_ID); map.put("mch_id", Constant.W_MCH_ID); map.put("nonce_str", Constant.wxRandomString()); map.put("out_trade_no", String.format("%032d", o.getOrderId())); String sign = Constant.wxSign(map); map.put("sign", sign); return Json.xml2json( Web.sendPost( Constant.W_CLOSE_ORDER_URL, new HashMap<String, String>(), Json.json2xml(map))); }
public static String wxUnifiedorder(Order o, String openId, String userIp) { Map<String, Object> map = new HashMap<String, Object>(); map.put("appid", Constant.W_APP_ID); map.put("mch_id", Constant.W_MCH_ID); map.put("nonce_str", Constant.wxRandomString()); map.put("body", "夕食餐饮订单"); map.put("out_trade_no", String.format("%032d", o.getOrderId())); map.put("total_fee", (int) o.getTotalPrice() * 100); System.out.println(map.get("total_fee")); map.put("spbill_create_ip", userIp); map.put("notify_url", Constant.W_PAY_NOTIFY_URL); map.put("trade_type", "JSAPI"); map.put("openid", openId); String sign = Constant.wxSign(map); map.put("sign", sign); return Json.xml2json( Web.sendPost( Constant.W_UNIFIED_ORDER_URL, new HashMap<String, String>(), Json.json2xml(map))); }
// 微信js签名生成算法 public static final String wxJsSign(long timestamp, String nonceStr, String url) { String jsapiTicket = Constant.wxGetJsapiTicket(); System.out.println("===============" + jsapiTicket + "==============="); if (jsapiTicket == null) { return null; } String str = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + url; // return Encrypt.SHA1(str); return str; }
public static final String wxGetJsapiTicket() { Cache cache = Cache.getInstance(); long now = new Date().getTime() / 1000; if (!cache.exist("wxJsapiTicket") || now - (long) cache.get("wxJsapiTicketTime") > Constant.W_EXPIRE_IN) { String accessToken = Constant.wxGetAccessToken(); if (accessToken == null) { return null; } String url = Constant.W_GET_JSAPI_TICKET + "?access_token=" + accessToken + "&type=jsapi"; String ret = Web.sendGet(url); JSONObject json = JSONObject.fromObject(ret); if (json.containsKey("errcode") && json.getInt("errcode") != 0) { return null; } cache.set("wxJsapiTicket", json.getString("ticket")); cache.set("wxJsapiTicketTime", now); Constant.W_EXPIRE_IN = json.getInt("expires_in") - 200; } return (String) cache.get("wxJsapiTicket"); }