自学内容网 自学内容网

Java微信支付接入(10)API V3 申请退款API

官方文档:

https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_9.shtml

1.创建退款单

接口:

    @ApiOperation("退款企业订单")
    @PostMapping("/refunds")
    public ResultMessageBean refunds(String orderId){
        return businessOrderService.refund(orderId);
    }

业务实现:

@Override
    public ResultMessageBean refund(String orderId) {
        try {
            //生成退款订单
            if(!this.createRefundByOrderId(orderId)){
                return new ResultMessageBean(ResultErrorEnum.custom_error.getCode(), "生成退款订单失败,orderId ===> " + orderId);
            }

            //调用微信退款API
            if(!wxRefund(orderId)){
                return new ResultMessageBean(ResultErrorEnum.custom_error.getCode(), "调用微信退款API失败,orderId ===> " + orderId);
            }


        }catch (Exception e){
            logger.info("订单退款失败,orderId ===> " + orderId);
            return new ResultMessageBean(ResultErrorEnum.custom_error.getCode(), "订单退款失败,orderId ===> " + orderId);
        }
        return new ResultMessageBean(ResultErrorEnum.request_success.getCode(), ResultErrorEnum.request_success.getMessage());
    }

创建退款订单方法

 private boolean createRefundByOrderId(String orderId) {
        TBusinessOrder tBusinessOrder=bussinessOrderMapper.findByOrderIdAndState(orderId,2);
        if(null==tBusinessOrder){
            return false;
        }
        TBusinessOrder refundBusinessOrder=new TBusinessOrder();
        refundBusinessOrder.setOrderId(orderId);
        refundBusinessOrder.setRefundId(UUIDTool.getUUID());
        refundBusinessOrder.setRefundPrice(tBusinessOrder.getPrice());
        refundBusinessOrder.setRefundTime(new Date());

        int flag=bussinessOrderMapper.updateBusinessOrder(refundBusinessOrder);
        if(flag!=0){
            return true;
        }
        return false;
    }

调用微信退款API 方法

/**
     * 调用微信退款接口
     * */
    private boolean wxRefund(String orderId) {
        try {
            logger.info("调用退款API orderId===>"+orderId);
            TBusinessOrder refundsInfo =bussinessOrderMapper.findByOrderIdAndState(orderId, 2);
            //调用统一下单API
            String url = wxPayConfig.getDomain().concat(WxApiTypeEnum.DOMESTIC_REFUNDS.getType());
            logger.info("微信退款API URL ===> " + url);
            HttpPost httpPost = new HttpPost(url);
            // 请求body参数
            Gson gson = new Gson();
            Map paramsMap = new HashMap();
            paramsMap.put("out_trade_no", orderId);//订单编号
            paramsMap.put("out_refund_no", refundsInfo.getRefundId());//退款单编号
            paramsMap.put("notify_url", wxPayConfig.getNotifyDomain().concat(WxNotifyTypeEnum.REFUND_NOTIFY.getType()));//退款通知地址
            Map amountMap = new HashMap();
            amountMap.put("refund", refundsInfo.getRefundPrice());//退款金额
            amountMap.put("total", refundsInfo.getPrice());//原订单金额
            amountMap.put("currency", "CNY");//退款币种
            paramsMap.put("amount", amountMap);

            //将参数转换成json字符串
            String jsonParams = gson.toJson(paramsMap);
            logger.info("微信退款API请求参数 ===> {}" + jsonParams);

            StringEntity entity = new StringEntity(jsonParams,"utf-8");
            entity.setContentType("application/json");//设置请求报文格式
            httpPost.setEntity(entity);//将请求报文放入请求对象
            httpPost.setHeader("Accept", "application/json");//设置响应报文格式

            //TODO:完成签名并执行请求,并完成验签
            //CloseableHttpResponse response = wxPayClient.execute(httpPost);
            //TODO:解析响应结果
            //String bodyAsString = EntityUtils.toString(response.getEntity());
            //int statusCode = response.getStatusLine().getStatusCode();

            //TODO:假设响应结果为200,响应内容为退款下单成功
            int statusCode=200;
            String bodyAsString="退款下单成功";
            if (statusCode == 200) {
                logger.info("成功, 退款返回结果 = " + bodyAsString);
            } else if (statusCode == 204) {
                logger.info("成功");
            } else {
                logger.info("退款下单失败"+bodyAsString);
                return false;
            }

        }catch (Exception e){
            e.printStackTrace();
            logger.infoError(e);
            return false;
        }
        //更新订单状态
        TBusinessOrder refundBusinessOrder=new TBusinessOrder();
        refundBusinessOrder.setOrderId(orderId);
        refundBusinessOrder.setState(4);
        bussinessOrderMapper.updateBusinessOrder(refundBusinessOrder);
        return true;
    }


原文地址:https://blog.csdn.net/q322359/article/details/142987129

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!