统一下单获取完prepay_id怎么把prepay_id在xml里面解析出来    来生成paysign

解决方案 »

  1.   

    https://edu.csdn.net/course/detail/4604/82573?auto_start=2 教程
      

  2.   

    /**
         * 获取支付信息
         * @param orderBody
         * @param money
         * @param out_trade_no
         * @param ip
         * @return
         */
        private WxSubmitOrderExtended getPrePayOrder(String orderBody, BigDecimal money, String out_trade_no, String ip) {
            money = new BigDecimal(Math.round(Double.valueOf(df.format(money.multiply(ONE_HUNDRED)))));
            String nonceStr = UUID.randomUUID().toString().replace("-", "");
            SortedMap<Object, Object> map = new TreeMap<Object, Object>();
            map.put("appid", appid);
            map.put("body", orderBody);
            map.put("mch_id", mchid);
            map.put("nonce_str", nonceStr);
            map.put("notify_url", notifyUrl);
            map.put("out_trade_no", out_trade_no);
            map.put("spbill_create_ip", ip);
            map.put("total_fee", money + "");
            map.put("trade_type", "APP");
            String sign = WeiXinUtil.createSign("UTF-8", map, apikey);
            map.put("sign", sign);
            String xmlStr = WeiXinUtil.getRequestXml(map);
            log.info(xmlStr);
            String response = HttpClientUtil.httpsRequest(unifiedorderUrl, RequestMethod.POST, new String(xmlStr));
            log.info(response);
            if (StringUtils.isBlank(response))
                throw new RuntimeException(ResponseCode.SERVER_ERROR.getMessage());
            Map result = XmlUtil.xml2mapResult(response);
            if (FAIL_CODE.equals(result.get(RETURN_CODE_KEY))) {
                log.error("请求微信失败:" + result.get(RETURN_MSG_KEY));
                throw new OrderException(SERVER_ERROR_20060);
            }
            return getOrder(result.get("prepay_id"), result.get("nonce_str"));    }    /**
         * 执行第二次签名,才能返回给客户端使用
         * @param prepayid
         * @param noncestr
         * @return
         */
        public WxSubmitOrderExtended getOrder(Object prepayid, Object noncestr) {
            SortedMap<Object, Object> map = new TreeMap<Object, Object>();
            String time = Long.toString(System.currentTimeMillis() / 1000);
            map.put("appid", appid);
            map.put("noncestr", noncestr);
            map.put("package", "Sign=WXPay");
            map.put("partnerid", mchid);
            map.put("prepayid", prepayid);
            map.put("timestamp", time);
            String sign = WeiXinUtil.createSign("utf-8", map, apikey);
            WxSubmitOrderExtended wx = new WxSubmitOrderExtended() {{
                setAppid(appid);
                setNonceStr((String) noncestr);
                setPackageStr("Sign=WXPay");
                setPartnerid(mchid);
                setPrepayid(prepayid);
                setTimestamp(time);
                setSign(sign);
            }};
            log.info(JacksonUtil.toJSon(wx));
            return wx;
        }
      

  3.   

    /**
         * xml转map 不带属性
         *
         * @param xmlStr
         * @param needRootKey 是否需要在返回的map里加根节点键
         * @return
         *
         * @throws DocumentException
         */
        public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {
            Document doc = DocumentHelper.parseText(xmlStr);
            Element root = doc.getRootElement();
            Map<String, Object> map = (Map<String, Object>) xml2map(root);
            if (root.elements().size() == 0 && root.attributes().size() == 0) {
                return map;
            }
            if (needRootKey) {
                //在返回的map里加根节点键(如果需要)
                Map<String, Object> rootMap = new HashMap<String, Object>();
                rootMap.put(root.getName(), map);
                return rootMap;
            }
            return map;
        }