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))); }
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"); }
// 获取微信accessToken和JsapiTicket缓存 public static final String wxGetAccessToken() { Cache cache = Cache.getInstance(); long now = new Date().getTime() / 1000; if (!cache.exist("wxAccessToken") || now - (long) cache.get("wxAccessTokenTime") > Constant.W_EXPIRE_IN) { String url = Constant.W_GET_ACCESS_TOKEN_URL + "?grant_type=client_credential&appid=" + Constant.W_APP_ID + "&secret=" + Constant.W_APP_SECRET; String ret = Web.sendGet(url); JSONObject json = JSONObject.fromObject(ret); if (json.containsKey("errcode") && json.getInt("errcode") != 0) { return null; } cache.set("wxAccessToken", json.getString("access_token")); cache.set("wxAccessTokenTime", now); Constant.W_EXPIRE_IN = json.getInt("expires_in") - 200; } return (String) cache.get("wxAccessToken"); }