废话不多说,直接上代码

生成prepay_id给前段返回

(如果是web端的就把 return 改成 (WebUtil.response(),如果是APP就直接用不用变)

这个写的一个Dome连接地址 里面有用到的工具类 还有微信支付的jar包:http://download.csdn.net/download/qq_37172500/9958703

其中的一个工具类,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();//调用异步通知 返回mapMap<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)));}}*/

三方 app微信支付 java后端实现相关推荐

  1. app微信支付-java服务端接口 支付/查询/退款

    app微信支付-java服务端接口 支付-查询-退款 个人看微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录. 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然 ...

  2. APP 微信支付java后台代码(解决支付失败返回-1)

    开发之前的准备工作: APP支付申请条件 申请成为APP支付商户需要满足以下条件: 1.APP支付商户,须拥有微信开放平台(http://open.weixin.qq.com)的APPID: 2.AP ...

  3. 微信支付-java实现微信支付-后端篇

    微信支付系列文章 微信支付-java后端实现 微信支付-vue 前端实现 java demo: 下载地址文章底部 技术栈 Spring boot java XML (微信在http协议中数据传输方案) ...

  4. 微信回调 java_详解APP微信支付(java后台_统一下单和回调)

    1.微信配置信息 global.properties 2.方法wxpay用于生成预支付订单信息 方法notifyWeiXinPay用于微信支付成功后的回调, 注意: 在手机端使用微信支付成功后,微信服 ...

  5. java+uniapp对接app微信支付

    java+uniapp对接app微信支付 1.准备工作 注册app APP接入微信支付,需要先将商户APP在微信开放平台进行注册,登记APP开发参数以生成APPID.具体操作步骤如下: 一.登录微信开 ...

  6. app微信支付后端接口编写

    刚做完app微信支付,我这块主要负责的是后端,现在我将我怎么开发的以及踩过的坑给大家写出来 微信支付主要有3步, 第一步:是生成一个预付订单 第二步:第二次签名的信息传到app端 第三步:最后接受到微 ...

  7. APP微信支付(java后台_统一下单和回调)

    微信支付Java后台 1.微信配置信息 global.properties 2.方法wxpay用于生成预支付订单信息 方法notifyWeiXinPay用于微信支付成功后的回调, 注意: 在手机端使用 ...

  8. php android 微信支付,Android_Android实现微信支付功能,开发Android APP微信支付功能, - phpStudy...

    Android实现微信支付功能 开发Android APP微信支付功能,需要完成三个步骤:第一步生成预支付订单.第二步生成微信支付参数.第三步调起微信APP支付.除了需要审核通过的APP应用外,还需要 ...

  9. 视频教程-企业微信支付JAVA版_向员工收款-微信开发

    企业微信支付JAVA版_向员工收款 微信企业号星级会员.10多年软件从业经历,国家级软件项目负责人,主要从事软件研发.软件企业员工技能培训.已经取得计算机技术与软件资格考试(软考)--"信息 ...

最新文章

  1. rancher 外置 mysql_rancher使用外部数据库无法正常使用
  2. SAP RETAIL MM41维护商品主数据的时候可以维护分类数据
  3. php认识正则吗,php正则表达式有什么用
  4. java uuid静态方法_Java UUID getLeastSignificantBits()方法与示例
  5. 六神不安,生死命悬一线
  6. 珍大户《认知世界的经济学》学习笔记,更新时间 2021年01月30日22:34:35
  7. 微信开放平台开发文档
  8. Log4j有哪几种日志级别呢?
  9. R语言 循环 步长 写法
  10. 高阶函数,太有用啦!
  11. 视音频编解码技术零基础学习方法
  12. 双击xmind文件没反应
  13. Android中基于心知天气API获取天气信息
  14. matlab 2015 积分,浅谈MATLAB在数值积分中的应用
  15. mac虚拟机桌面图标隐藏_Win10 终极美化篇 | 高仿Mac ,这不是主题
  16. ILI9341的使用之【六】命令二
  17. 第六章:演示文稿软件PowerPoint 2010 ——知识点整理
  18. 教育学考研跨考计算机,用心教育学考研 | 22届教育学考研跨考,做好这三件事跨考无压力...
  19. ASEMI肖特基二极管1N5822参数,1N5822特征,1N5822应用
  20. mysql参考手册官方版(中文)

热门文章

  1. Android 自定义时钟控件 时针、分针、秒针的绘制这一篇就够了
  2. transformerOcr
  3. OAuth2.0 是什么?
  4. 电流继电器JDL-1002A
  5. 总结学习--如何列计划
  6. 世界地图可以无限放大_又双叒叕被世界地图漏掉!委屈巴巴的新西兰人民简直心累!...
  7. 预测股价——线性回归模型
  8. 粗略整理IOS招聘要求
  9. 超快恢复二极管的作用分析
  10. 【Stata】Stata论文实证分析的基础代码分享