废话不多说,直接上代码

生成prepay_id给前段返回
(如果是web端的就把 return 改成 (WebUtil.response(),如果是APP就直接用不用变)

这个写的一个Dome连接地址 里面有用到的工具类 还有微信支付的jar包:

其中的一个工具类,Dome里面没有 如果就web端有就不需要AjaxReturnUtils
public static Map<Object, Object> ajaxReturn(Integer code, String describe) {

        Map<Object, Object> map = new HashMap<>();
        map.put(code, describe);
        return map;

    }
/**
     * 
     * 
     * @param request
     * @param response
     * @param zhongduan
     *            终端(app,web)
     * @return
     */
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public Map weiXinPay(SysOrder orderId, HttpServletRequest request, HttpServletResponse response, String zhongduan,
            String callback) {
        //我是根据订单号查的判断,有没有订单号 没有订单号就直接返回
        if (orderId.getId() == null && orderId.getId() > 0L) {
            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback, JSON
             * .toJSONString(new JsonResult(-1, "商品不存在", new ResponseData()),
             * SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(-1, "商品不存在");
        }

        Map<String, String> restmap = null;
        boolean flag = true; // 是否订单创建成功
        try {
            String total_fee = BigDecimal.valueOf(orderId.getTotal_fee()).multiply(BigDecimal.valueOf(100))
                    .setScale(0, BigDecimal.ROUND_HALF_UP).toString();
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("appid", APP_ID);
            parm.put("mch_id", MCH_ID);
            parm.put("nonce_str", PayUtil.getNonceStr());// 创建支付随机字符串
            parm.put("body", orderId.getBody());// APP——需传入应用市场上的APP名字-实际商品名称,天天爱消除-游戏充值。

            parm.put("out_trade_no", PayUtil.getTradeNo());
            parm.put("total_fee", total_fee);
            parm.put("spbill_create_ip", PayUtil.getRemoteAddrIp(request));
            parm.put("notify_url", "http://app.xiaotongkids.com/xiaotong/payApp/orderPayNotify"); // 微信服务器异步通知支付结果地址

            // 下面的order/notify

            // 方法
            parm.put("trade_type", zhongduan);
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));
            // 发起post 请求
            String restxml = HttpUtils.post(ORDER_PAY, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            // LOG.error(e.getMessage(), e);
            System.out.println(e.getMessage());
            e.printStackTrace();

        }

        Map<String, String> payMap = new HashMap<String, String>();
        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            payMap.put("appid", APP_ID);
            payMap.put("partnerid", MCH_ID);
            payMap.put("prepayid", restmap.get("prepay_id"));
            payMap.put("package", "Sign=WXPay");
            payMap.put("noncestr", PayUtil.getNonceStr());
            payMap.put("timestamp", PayUtil.payTimestamp());
            try {
                payMap.put("sign", PayUtil.getSign(payMap, API_SECRET));
            } catch (Exception e) {
                flag = false;
            }
        }

        if (flag) {
            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback,
             * JSON.toJSONString(new JsonResult(1, "订单获取成功", new
             * ResponseData(null, payMap)), SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(1, payMap.toString());
        } else {

            if (CollectionUtil.isNotEmpty(restmap)) {
                // LOG.info("订单创建失败:" + restmap.get("err_code") + ":" +
                // restmap.get("err_code_des"));
                System.out.println("订单创建失败:" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback, JSON
             * .toJSONString(new JsonResult(-1, "订单获取失败", new ResponseData()),
             * SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(-1, "订单获取失败");
        }
    }
查询支付结果
/**
     * 查询支付结果
     * 
     * @param request
     * @param response
     * @param tradeid
     *            微信交易订单号
     * @param tradeno
     *            商品订单号
     * @param callback
     */
     @RequestMapping(value = "/pay/query", method = RequestMethod.POST)
    public Map orderPayQuery(HttpServletRequest request, HttpServletResponse response, String tradeid, String tradeno,
            String callback) {
        // LOG.info("[/order/pay/query]");
        if (StringUtil.isEmpty(tradeno) && StringUtil.isEmpty(tradeid)) {
            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback, JSON
             * .toJSONString(new JsonResult(-1, "订单号不能为空", new ResponseData()),
             * SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(-1, "订单获取失败");
        }

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("appid", APP_ID);
            parm.put("mch_id", MCH_ID);
            parm.put("transaction_id", tradeid);
            parm.put("out_trade_no", tradeno);
            parm.put("nonce_str", PayUtil.getNonceStr());
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));

            String restxml = HttpUtils.post(ORDER_PAY_QUERY, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            // LOG.error(e.getMessage(), e);
            e.getMessage();
        }

        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            // 订单查询成功 处理业务逻辑
            // LOG.info("订单查询:订单" + restmap.get("out_trade_no") + "支付成功");
            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback, JSON
             * .toJSONString(new JsonResult(1, "订单支付成功", new ResponseData()),
             * SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(1, "支付成功");
        } else {

            /*
             * WebUtil.response(response, WebUtil.packJsonp(callback, JSON
             * .toJSONString(new JsonResult(-1, "订单支付失败", new ResponseData()),
             * SerializerFeatureUtil.FEATURES)));
             */
            return AjaxReturnUtils.ajaxReturn(-1, "订单支付失败");
        }
    }
微信异步通知(我是当工具类直接掉的)
/**
     * 订单支付微信服务器异步通知
     * 
     * @param request
     * @param response
     */
     public Map orderPayNotify(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("[/order/pay/notify]");


        try {
            ServletInputStream in = request.getInputStream();
            String resxml = FileUtil.readInputStream2String(in);
            Map<String, String> restmap = XmlUtil.xmlParse(resxml);
            System.out.println("支付结果通知:" + restmap);

            //判断是否成功
            if ("SUCCESS".equals(restmap.get("return_code"))) {
                // 订单支付成功 业务处理
                String out_trade_no = restmap.get("out_trade_no"); // 商户订单号
                String transaction_id = restmap.get("transaction_id");// 微信订单号
                String  time_end  = restmap.get("time_end");
                // 通过商户订单判断是否该订单已经处理 如果处理跳过 如果未处理先校验sign签名 再进行订单业务相关的处理
                String sing = restmap.get("sign");
                restmap.remove("sign");
                String signnow = PayUtil.getSign(restmap, API_SECRET);
                if (signnow.equals(sing)) {
                    // 进行业务处理
                    System.out.println("订单支付通知: 支付成功,订单号" + out_trade_no);
                    return AjaxReturnUtils.ajaxPay(1, out_trade_no, transaction_id,time_end);

                } else {
                    System.out.println("订单支付通知:签名错误");
                    // map.put("订单支付通知:签名错误", "订单支付通知:签名错误");
                    // return map;
                }
            } else {
                System.out.println("订单支付通知:支付失败," + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
                //return AjaxReturnUtils.ajaxPay(2, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
微信异步通知使用方法
@RequestMapping(value = "/orderPayNotify")
    public void orderPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //实例化微信异步通知的类
        PayWeiXinController PayWeiXinController = new PayWeiXinController();
        //调用异步通知 返回map
        Map<String, Object> map = PayWeiXinController.orderPayNotify(request, response);
        Integer code = (Integer) map.get("code");// 状态码
        String v = (String) map.get("describe");// 商品订单号
        String transaction_id = (String) map.get("transaction_id");// 支付订单号
        String time_end = (String) map.get("time_end");// 成功的时间
        if (code != null) {
            // 微信异步回调成功
            if (code == 1) {
                //支付成功.....处理自己的业务逻辑
                }
            } else if (code == 2) {
                System.out.println("支付失败");
            }

        } else {
            //支付失败.....处理自己的业务逻辑
            System.out.println(map.toString());
        }

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/xml");
        String a = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        response.getWriter().print(a);
    }
订单退款 需要双向证书验证
/**
     * 订单退款 需要双向证书验证
     * 
     * @param request
     * @param response
     * @param tradeno
     *            微信订单号
     * @param orderno
     *            商家订单号
     * @param callback
     */
    @RequestMapping(value = "/pay/refund", method = RequestMethod.POST)
    public void orderPayRefund(HttpServletRequest request, HttpServletResponse response, String tradeno, String orderno,
            String callback) {
        System.out.println("[/pay/refund]");
        if (StringUtil.isEmpty(tradeno) && StringUtil.isEmpty(orderno)) {
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(-1, "订单号不能为空", new ResponseData()), SerializerFeatureUtil.FEATURES)));
        }

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("appid", APP_ID);
            parm.put("mch_id", MCH_ID);
            parm.put("nonce_str", PayUtil.getNonceStr());
            parm.put("transaction_id", tradeno);
            parm.put("out_trade_no", orderno);// 订单号
            parm.put("out_refund_no", PayUtil.getRefundNo()); // 退款单号
            parm.put("total_fee", "10"); // 订单总金额 从业务逻辑获取
            parm.put("refund_fee", "10"); // 退款金额
            parm.put("op_user_id", MCH_ID);
            parm.put("refund_account", "REFUND_SOURCE_RECHARGE_FUNDS");// 退款方式
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));

            // String restxml = HttpUtils.posts(ORDER_REFUND,
            // XmlUtil.xmlFormat(parm, false));
            String restxml = HttpUtils.posts(ORDER_REFUND, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        Map<String, String> refundMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            refundMap.put("transaction_id", restmap.get("transaction_id"));
            refundMap.put("out_trade_no", restmap.get("out_trade_no"));
            refundMap.put("refund_id", restmap.get("refund_id"));
            refundMap.put("out_refund_no", restmap.get("out_refund_no"));
            System.out.println("订单退款:订单" + restmap.get("out_trade_no") + "退款成功,商户退款单号" + restmap.get("out_refund_no")
                    + ",微信退款单号" + restmap.get("refund_id"));
            WebUtil.response(response,
                    WebUtil.packJsonp(callback,
                            JSON.toJSONString(new JsonResult(1, "订单获取成功", new ResponseData(null, refundMap)),
                                    SerializerFeatureUtil.FEATURES)));
        } else {
            if (CollectionUtil.isNotEmpty(restmap)) {
                System.out.println("订单退款失败:" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(-1, "订单退款失败", new ResponseData()), SerializerFeatureUtil.FEATURES)));
        }
    }
订单退款查询
/**
     * 订单退款查询
     * 
     * @param request
     * @param response
     * @param tradeid
     *            微信订单号
     * @param tradeno
     *            商户订单号
     * @param refundid
     *            微信退款号
     * @param refundno
     *            商家退款号
     * @param callback
@RequestMapping(value = "/pay/refund/query", method = RequestMethod.POST)
    public void orderPayRefundQuery(HttpServletRequest request, HttpServletResponse response, String refundid,
            String refundno, String tradeid, String tradeno, String callback) {
        System.out.println("[/pay/refund/query]");
        if (StringUtil.isEmpty(tradeid) && StringUtil.isEmpty(tradeno) && StringUtil.isEmpty(refundno)
                && StringUtil.isEmpty(refundid)) {
            WebUtil.response(response,
                    WebUtil.packJsonp(callback, JSON.toJSONString(new JsonResult(-1, "退单号或订单号不能为空", new ResponseData()),
                            SerializerFeatureUtil.FEATURES)));
        }

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("appid", APP_ID);
            parm.put("mch_id", MCH_ID);
            parm.put("transaction_id", tradeid);
            parm.put("out_trade_no", tradeno);
            parm.put("refund_id", refundid);
            parm.put("out_refund_no", refundno);
            parm.put("nonce_str", PayUtil.getNonceStr());
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));

            String restxml = HttpUtils.post(ORDER_REFUND_QUERY, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        Map<String, String> refundMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))
                && "SUCCESS".equals(restmap.get("result_code"))) {
            // 订单退款查询成功 处理业务逻辑
            System.out
                    .println("退款订单查询:订单" + restmap.get("out_trade_no") + "退款成功,退款状态" + restmap.get("refund_status_0"));
            refundMap.put("transaction_id", restmap.get("transaction_id"));
            refundMap.put("out_trade_no", restmap.get("out_trade_no"));
            refundMap.put("refund_id", restmap.get("refund_id_0"));
            refundMap.put("refund_no", restmap.get("out_refund_no_0"));
            refundMap.put("refund_status", restmap.get("refund_status_0"));
            WebUtil.response(response,
                    WebUtil.packJsonp(callback,
                            JSON.toJSONString(new JsonResult(1, "订单退款成功", new ResponseData(null, refundMap)),
                                    SerializerFeatureUtil.FEATURES)));
        } else {
            if (CollectionUtil.isNotEmpty(restmap)) {
                System.out.println("订单退款失败:" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(-1, "订单退款失败", new ResponseData()), SerializerFeatureUtil.FEATURES)));
        }
    }
     */