admin管理员组文章数量:1558103
微信退款流程比收款流程简单很多,在调取微信退款之前应处理好退款逻辑
处理好退款逻辑后进行微信退款
调取回调地址过程与微信收款也基本一致
可以采用同一个回调接口,接收数据
/**
* 微信退款调用
* @param param
* @return
*/
public static String wxRefund(Map<String, String> param){
String regDiscount = param.get("regDiscount");//原金额
String refundDiscount = param.get("refundDiscount");//退款金额
String tradeno = param.get("tradeno");//票号
String outRefundNo = param.get("outRefundNo");//退号票号
String url = "https://api.mch.weixin.qq/v3/refund/domestic/refunds";//退款路径
String notify_url = "回调地址";//回调地址
int refund = new BigDecimal(refundDiscount).multiply(new BigDecimal(100)).intValue();//退款金额==>以分为单位
int total = new BigDecimal(regDiscount).multiply(new BigDecimal(100)).intValue();//原订单金额
String currency = "CNY";//退款币种
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("notify_url", notify_url)
.put("out_trade_no", tradeno)
.put("out_refund_no", outRefundNo);
rootNode.putObject("amount")
.put("total", total)
.put("currency", currency)
.put("refund", refund);
String bodyAsString = WxUtils.getHttpClient(url,objectMapper,rootNode);
return bodyAsString;
}
调取微信接口,POST请求,需加签名
public static String getHttpClient(String url, ObjectMapper objectMapper,ObjectNode rootNode){
String bodyAsString = "";
try{
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
objectMapper.writeValue(bos, rootNode);//传入参数
PrivateKey merchantPrivateKey =getPrivateKey();
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
String merchantSerialNumber = getMerchantSerialNumber();
Verifier verifier = getVerifier();
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId , merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
CloseableHttpClient httpClient = builder.build();
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
bodyAsString = EntityUtils.toString(response.getEntity());
}catch (Exception e) {
e.printStackTrace();
}
return bodyAsString;
}
微信退费和收费可以共用一个getHttpClient去完成,只需要返回bodyAsString,然后转为JSON串后获得相应数据
退款回调通知示例:
本文标签: 流程
版权声明:本文标题:微信退款流程 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1727371623a1111240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论