当前位置: 首页 > article >正文

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;
    }


http://www.kler.cn/news/353725.html

相关文章:

  • 深度学习500问——Chapter17:模型压缩及移动端部署(4)
  • 【环境搭建】远程服务器搭建ElasticSearch
  • python取字典的任意一项的value
  • pytorh学习笔记——手写数字识别mnist
  • Linux 命令—— ping、telnet、curl、wget(网络连接相关命令)
  • 改变TikTok零播放情况的6个解决方法
  • C++学习笔记----9、发现继承的技巧(一)---- 使用继承构建类(1)
  • OpenVAS—— 强大的开源漏洞扫描工具从安装到使用全攻略
  • Java工具类--OkHttp工具类
  • indicatorTree-v10练习(有问题)
  • Leetcode 跳跃游戏 二
  • Elasticsearch介绍和使用
  • Java项目:157 基于springboot技术的美食烹饪互动平台的设计与实现(含论文+说明文档)
  • Android应用性能优化的方法
  • 【哈工大_操作系统理论】L2223 多级页表与快表段页结合的实际内存管理
  • 【黑马redis高级篇】持久化
  • 除GOF23种设计模式之简单工厂模式
  • langchain更新再体验:加入一个prompt
  • 15分钟学Go 第3天:编写第一个Go程序
  • JavaWeb 22.Node.js_简介和安装