public Map<String, Object> h5Pay(HttpServletRequest request, PaymentUser paymentUser) throws IOException {
//2.调用下单API,接口调用地址 HttpPost httpPost = new HttpPost("www.baidu.com")
//设置请求体(接口必要参数) Map<String, Object> paramMap = new HashMap<>(); paramMap.put("appid", wxPayConfig.getAppid()); paramMap.put("mchid", wxPayConfig.getMchId()); paramMap.put("description",paymentUser.getDeptName()+"-智慧党建缴费"); paramMap.put("out_trade_no", StringUtils.createOrderNum()); Map<String, Object> amountMap = new HashMap<>(); amountMap.put("total",paymentUser.getTotal()*100); amountMap.put("currency", "CNY"); paramMap.put("amount",amountMap); //参数转成json格式 Gson gson = new Gson(); String requestJson = gson.toJson(paramMap); //将json转成StringEntity对象 StringEntity entity = new StringEntity(requestJson, "utf-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json");
//发送请求 CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); CloseableHttpResponse response = closeableHttpClient.execute(httpPost); //4.获取相应结果 String bodyString = EntityUtils.toString(response.getEntity()); //请求后得到的响应体 //状态码 int statusCode = response.getStatusLine().getStatusCode(); //如果处理成功(带响应体的) if (statusCode == 200) { log.info("请求成功 : " + bodyString); }
//把body中的信息转成map ,直接返回 Map<String, String> resultMap = gson.fromJson(bodyString, HashMap.class);
return resultMap ; }