java

您所在的位置:网站首页 微信咋个退款 java

java

2024-07-13 07:44| 来源: 网络整理| 查看: 265

上次写了篇微信支付的文章,有童鞋来问我如果要退款怎么写,退款比支付简单多了,废话不多说,来贴下代码。

首先在controller里:

public String refund( ) throws Exception { Map data = new HashMap(); System.err.println("进入微信退款申请"); Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式 String hehe = dateFormat.format(now); String out_refund_no = hehe + "wxrefund"; String transaction_id = "微信支付时生成的流水号"; String total_fee = "微信支付时支付的钱(单位为分)"; data.put("out_refund_no", out_refund_no); data.put("transaction_id", transaction_id); data.put("total_fee", total_fee); data.put("refund_fee", total_fee); String result = wxPayService.refund(data); //判断是否退款成功 if (result.equals("\"退款申请成功\"")) { return "已退款"; } return "微信退款失败"; }

具体传参什么的对应你们自己的业务逻辑,我只贴跟微信退款相关的。

里面调用了wxPayService这个的方法:

/** * 申请退款 * * @param data 包含商户订单号、商户退款单号、订单金额、退款金额 * @return */ @Override public String refund(Map data) throws Exception { WXMyConfigUtil config = new WXMyConfigUtil(); WXPay wxpay = new WXPay(config); data.put("appid", config.getAppID()); data.put("mch_id", config.getMchID()); data.put("nonce_str", WXPayUtil.generateNonceStr()); data.put("sign", md5Util.getSign(data)); Map resp = null; try { resp = wxpay.refund(data); } catch (Exception e) { e.printStackTrace(); } System.err.println(resp); String return_code = resp.get("return_code"); //返回状态码 String return_msg = resp.get("return_msg"); //返回信息 String resultReturn = null; if ("SUCCESS".equals(return_code)) { String result_code = resp.get("result_code"); //业务结果 String err_code_des = resp.get("err_code_des"); //错误代码描述 if ("SUCCESS".equals(result_code)) { //表示退款申请接受成功,结果通过退款查询接口查询 //修改用户订单状态为退款申请中(暂时未写) resultReturn = "退款申请成功"; } else { logger.info("订单号:{}错误信息:{}", err_code_des); resultReturn = err_code_des; } } else { logger.info("订单号:{}错误信息:{}", return_msg); resultReturn = return_msg; } return JSON.toJSONString(resultReturn); }

如何导包和配置pom文件,我在上次那篇博客里已经写了。这里涉及到了config文件的配置。

这是微信很坑的一点,它的demo里并没有告诉你如何配置。我还是一样直接放代码:

package com.kyd.callcenter.util.weixin; import com.github.wxpay.sdk.WXPayConfig; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class WXMyConfigUtil implements WXPayConfig { private byte[] certData; public WXMyConfigUtil() throws Exception { String certPath = "apiclient_cert.p12";//从微信商户平台下载的安全证书存放的目录 File file = new File(certPath); InputStream certStream = new FileInputStream(file); this.certData = new byte[(int) file.length()]; certStream.read(this.certData); certStream.close(); } @Override public String getAppID() { return "你微信账户的appid"; } //parnerid @Override public String getMchID() { return "你微信账户的商户id"; } @Override public String getKey() { return "你微信账户的api密钥"; } @Override public InputStream getCertStream() { ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3