[置顶] 微信公众号开发---退款(java)
2016-09-04 18:48
330 查看
微信公众号支付请看上一篇文章:
http://blog.csdn.net/aofavx/article/details/52220394
需要用到的java类都在:
http://download.csdn.net/detail/aofavx/9606697
微信公众号退款相对支付来说就简单多了,唯一需要注意的地方就是:在支付的时候需要用到商户的证书。
退款代码如下:
jsp提交退款申请:
2
3
4
java代码:
http://blog.csdn.net/aofavx/article/details/52220394
需要用到的java类都在:
http://download.csdn.net/detail/aofavx/9606697
微信公众号退款相对支付来说就简单多了,唯一需要注意的地方就是:在支付的时候需要用到商户的证书。
退款代码如下:
jsp提交退款申请:
//退款 function refundWXPay() { window.location.href="refundPay?orderId=${orderId}"; }1
2
3
4
java代码:
/** *微信公众号申请退款 * @param orderId 订单id * @param total_fee 退款金额 * @param refund_fee 退款金额 * @param response * @return * @throws Exception */ @RequestMapping(value="/refundPay") @ResponseBody private ModelAndView refundwx(HttpServletRequest request,HttpServletResponse response) throws Exception{ logBefore(logger, "退款"); ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); /*--------1.初始化数据参数----------*/ String appId= "wxfd7c065eee11112222"; String secret= "b5b3a627f5d1f8888888888888"; String shh= "111111111; String key= "mmmmmmmmmmmmmmm"; String filePath = "E:\\证书\\apiclient_cert.p12"; //退款需要提供证书数据,所以需要根据证书路径读取证书 //需要退款的商户订单号,对应提交订单中的out_trade_no String orderId = request.getParameter("orderId").toString(); String total_fee=WeChat.getMoney(request.getParameter("total_fee")); //也可以根据业务场景从数据库中获取总金额和退款金额 String refund_fee=WeChat.getMoney(request.getParameter("refund_fee")); Map<String,String> result = (Map<String, String>) wxRefund(request,response,appId,secret,shh,key,orderId,total_fee,refund_fee,filePath); /* 根据result的返回状态,处理你的业务逻辑 ..... */ mv.setViewName("wechat/refundFind"); return mv; } private Object wxRefund(HttpServletRequest request,HttpServletResponse response,String appId, String secret,String shh,String key,String orderId,String total_fee,String refund_fee,String path){ Map<String,String> result=new HashMap<String,String>(); PageData pd = new PageData(); pd = this.getPageData(); String refundid = UuidUtil.get32UUID(); String nonce_str = MD5.getMessageDigest(String.valueOf(new Random().nextInt(10000)).getBytes()); /*----- 1.生成预支付订单需要的的package数据-----*/ SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put("appid", appId); packageParams.put("mch_id", shh); packageParams.put("nonce_str", nonce_str); packageParams.put("op_user_id", shh); packageParams.put("out_trade_no", orderId); packageParams.put("out_refund_no", refundid); packageParams.put("total_fee",total_fee); packageParams.put("refund_fee", refund_fee); /*----2.根据package生成签名sign---- */ RequestHandler reqHandler = new RequestHandler(request, response); reqHandler.init(appId, secret, key); String sign = reqHandler.createSign(packageParams); /*----3.拼装需要提交到微信的数据xml---- */ String xml="<xml>" +"<appid>"+appId+"</appid>" + "<mch_id>"+shh+"</mch_id>" + "<nonce_str>"+nonce_str+"</nonce_str>" + "<op_user_id>"+shh+"</op_user_id>" + "<out_trade_no>"+orderId+"</out_trade_no>" + "<out_refund_no>"+refundid+"</out_refund_no>" + "<refund_fee>"+refund_fee+"</refund_fee>" + "<total_fee>"+total_fee+"</total_fee>" + "<sign>"+sign+"</sign>" +"</xml>"; try { /*----4.读取证书文件,这一段是直接从微信支付平台提供的demo中copy的,所以一般不需要修改---- */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(path)); try { keyStore.load(instream, shh.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, shh.toCharArray()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); /*----5.发送数据到微信的退款接口---- */ String url="https://api.mch.weixin.qq.com/secapi/pay/refund"; HttpPost httpost= HttpClientConnectionManager.getPostMethod(url); httpost.setEntity(new StringEntity(xml, "UTF-8")); HttpResponse weixinResponse = httpClient.execute(httpost); String jsonStr = EntityUtils.toString(weixinResponse.getEntity(), "UTF-8"); logger.info(jsonStr); Map map = GetWxOrderno.doXMLParse(jsonStr); if("success".equalsIgnoreCase((String) map.get("return_code"))){ logger.info("退款成功"); result.put("returncode", "ok"); result.put("returninfo", "退款成功"); }else{ logger.info("退款失败"); result.put("returncode", "error"); result.put("returninfo", "退款失败"); } } catch (Exception e) { logger.info("退款失败"); result.put("returncode", "error"); result.put("returninfo", "退款失败"); } return result; }
相关文章推荐
- [置顶] think in java interview-高级开发人员面试宝典(二)
- [置顶] think in java interview-高级开发人员面试宝典(八)
- java 微信与支付宝退款接口开发总结
- 用java开发微信公众号:公众号接入和access_token管理(二)
- java微信公众号开发(搭建本地测试环境)
- [置顶] think in java interview-高级开发人员面试宝典(一)
- java springMVC 开发微信公众号
- 零java基础搞定微信Server_5:启用微信公众号开发模式
- 微信公众号开发 java版本
- java微信公众号开发总结(2)——文本消息处理
- [置顶] Java Web开发教程来袭
- 微信开发 PC搭建服务器 开发 微信公众号【Java版本】
- Java微信公众号开发
- Java微信公众号开发--开发环境的搭建
- 【JavaWeb】(10)微信公众号开发进阶
- [置顶] think in java interview-高级开发人员面试宝典(七)
- Wechat4j之Hello world——使用wechat4j快速开发java版微信公众号
- 微信公众号开发(公交线路查询-JAVA
- 第六篇 :微信公众平台开发实战Java版之如何自定义微信公众号菜单
- java微信公众号开发总结(3)——调试方法总结(包括本地调试)