微信退款流程

微信退款流程比收款流程简单很多,在调取微信退款之前应处理好退款逻辑

处理好退款逻辑后进行微信退款

调取回调地址过程与微信收款也基本一致

可以采用同一个回调接口,接收数据

/**
     * 微信退款调用
     * @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.com/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串后获得相应数据
在这里插入图片描述
退款回调通知示例:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值