@Transactional(rollbackFor = Exception.class)@Overridepublic Map<String, Object> nativePay(Long productId) throws Exception {log.info("生成订单");OrderInfo orderInfo = orderInfoService.createOrderByProductId(productId);String codeUrl = orderInfo.getCodeUrl();if(orderInfo != null && !StringUtils.isEmpty(codeUrl)){log.info("订单已存在,二维码已保存");Map<String, Object> map = new HashMap<>();map.put("codeUrl", codeUrl);map.put("orderNo", orderInfo.getOrderNo());return map;}log.info("调用统一下单API");HttpPost httpPost = new HttpPost(wxPayConfig.getDomain().concat(WxApiType.NATIVE_PAY.getType()));Gson gson = new Gson();Map paramsMap = new HashMap();paramsMap.put("appid", wxPayConfig.getAppid());paramsMap.put("mchid", wxPayConfig.getMchId());paramsMap.put("description", orderInfo.getTitle());paramsMap.put("out_trade_no", orderInfo.getOrderNo());paramsMap.put("notify_url", wxPayConfig.getNotifyDomain().concat(WxNotifyType.NATIVE_NOTIFY.getType()));Map amountMap = new HashMap();amountMap.put("total", orderInfo.getTotalFee());amountMap.put("currency", "CNY");paramsMap.put("amount", amountMap);String jsonParams = gson.toJson(paramsMap);log.info("请求参数 ===> {}" + jsonParams);StringEntity entity = new StringEntity(jsonParams,"utf-8");entity.setContentType("application/json");httpPost.setEntity(entity);httpPost.setHeader("Accept", "application/json");CloseableHttpResponse response = wxPayClient.execute(httpPost);try {String bodyAsString = EntityUtils.toString(response.getEntity());int statusCode = response.getStatusLine().getStatusCode();if (statusCode == 200) { log.info("成功, 返回结果 = " + bodyAsString);} else if (statusCode == 204) { log.info("成功");} else {log.info("Native下单失败,响应码 = " + statusCode+ ",返回结果 = " + bodyAsString);throw new IOException("request failed");}Map<String, String> resultMap = gson.fromJson(bodyAsString, HashMap.class);codeUrl = resultMap.get("code_url");String orderNo = orderInfo.getOrderNo();orderInfoService.saveCodeUrl(orderNo, codeUrl);Map<String, Object> map = new HashMap<>();map.put("codeUrl", codeUrl);map.put("orderNo", orderInfo.getOrderNo());return map;} finally {response.close();}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!