美文网首页第三方接入
对接微信和支付宝退款api

对接微信和支付宝退款api

作者: SeekLife0 | 来源:发表于2022-08-06 15:15 被阅读0次

    1、微信退款

    参考:
    (41条消息) weixin-java-pay实现公众号微信支付与退款_earthhour的博客-CSDN博客_weixin-java-pay

    需要配置api证书
    退款前的相关配置

        /**
         * @author seeklife
         * 微信api配置,注意退款需要设置api证书
         * @date 2022/8/6 14:14
         * @return null
         */
        public WxPayService wxService(){
            WxPayConfig payConfig = new WxPayConfig();
            payConfig.setAppId("wx6e904ddc50e1e4d4");
            payConfig.setMchId("1625744273");
            payConfig.setMchKey("W5iU6nmf3hQYI7oJACdz9corlmh5EtGC");
            InputStream stream1 = getClass().getClassLoader().getResourceAsStream("apiclient_cert.p12");
            File targetFile1 = new File("apiclient_cert.p12");
            try {
                FileUtils.copyInputStreamToFile(stream1, targetFile1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            payConfig.setKeyPath(targetFile1.getPath());
            // 可以指定是否使用沙箱环境
            payConfig.setUseSandboxEnv(false);
            WxPayService wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(payConfig);
            return wxPayService;
        }
    

    开始执行退款操作

    /**
         * @author seeklife
         * 微信退款操作
         * @date 2022/8/5 16:38
         * @param orderNo
         * @param refundNo
         * @param money
         * @return java.lang.String
         */
        public String wxRefund(String mergeOrderNo,String orderNo,String refundNo,BigDecimal money) {
            WxPayService wxPayService = wxService();
            //申请退款
            WxPayRefundRequest refundInfo = WxPayRefundRequest.newBuilder()
                    //订单号
                    .outTradeNo(mergeOrderNo)
                    //退款订单号
                    .outRefundNo(refundNo)
                    //金额 单位分
                    .totalFee(yuanToFee(money))
                    //退款金额
                    .refundFee(yuanToFee(money))
                    .notifyUrl("http://你的域名:8002/api/wx/refundNotify")
                    .build();
            WxPayRefundResult wxPayRefundResult;
            try {
                wxPayRefundResult = wxPayService.refund(refundInfo);
                //判断退款信息是否正确
                if (REFUND_SUCCESS.equals(wxPayRefundResult.getReturnCode()) && REFUND_SUCCESS.equals(wxPayRefundResult.getResultCode())) {
                    /**
                     * 系统内部业务逻辑
                     */
                    updateRefundApply(orderNo,RefundListEnum.REFUND_HANDLE_1.getValue());
                    return "正在退款中...";
                }
            } catch (WxPayException e) {
                log.error("微信退款接口错误信息= {}", e);
            }
            return "退款失败";
        }
    

    回调

        /**
         * 仅支持一次性退款,多次退款需要修改逻辑
         * @param xmlData 微信返回的流数据
         * @return
         */
        @PostMapping("/wx/refundNotify")
        public String refundNotify(@RequestBody String xmlData) {
            WxPayService wxPayService = wxService();
            WxPayRefundNotifyResult wxPayRefundNotifyResult;
            try {
                wxPayRefundNotifyResult = wxPayService.parseRefundNotifyResult(xmlData);
            } catch (WxPayException e) {
                log.error("退款失败,失败信息:{}", e);
                return WxPayNotifyResponse.fail("退款失败");
            }
            //判断你返回状态信息是否正确
            if (REFUND_SUCCESS.equals(wxPayRefundNotifyResult.getReturnCode())) {
                WxPayRefundNotifyResult.ReqInfo reqInfo = wxPayRefundNotifyResult.getReqInfo();
                //判断退款状态
                if (REFUND_SUCCESS.equals(reqInfo.getRefundStatus())) {
                    //内部订单号
                    String outTradeNo = reqInfo.getOutTradeNo();
                    /**
                     * 成功后的业务逻辑
                     */
                        return WxPayNotifyResponse.success("退款成功!");
                    }
                }
            }
            return WxPayNotifyResponse.fail("回调有误!");
        }
    

    2、支付宝退款

    参考:
    统一收单交易退款接口 - 支付宝文档中心 (alipay.com)

    必须要使用公钥证书验签方式
    详见:
    springboot后端接入支付宝提现到余额功能 - 简书 (jianshu.com)

    /**
     * @author seeklife
     * alipay退款操作
     * @date 2022/8/5 16:38
     * @param orderNo
     * @param refundNo
     * @param money
     * @return java.lang.String
     */
    public String aliRefund(String mergeOrderNo,String orderNo,String refundNo,BigDecimal money) {
        AlipayClient alipayClient = Alipay.getInstance().getAlipayClient();
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", mergeOrderNo);
        bizContent.put("refund_amount", money);
        bizContent.put("out_request_no", refundNo); //"HZ01RF001"
        //// 返回参数选项,按需传入
        //JSONArray queryOptions = new JSONArray();
        //queryOptions.add("refund_detail_item_list");
        //bizContent.put("query_options", queryOptions);
    
        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.certificateExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            //支付宝退款成功
            updateRefundApply(orderNo,RefundListEnum.REFUND_HANDLE_2.getValue());
        }else{
            //支付宝支付失败
            updateRefundApply(orderNo,RefundListEnum.REFUND_HANDLE_3.getValue());
        }
        return response.getSubCode() +  response.getSubMsg();
    }
    

    相关文章

      网友评论

        本文标题:对接微信和支付宝退款api

        本文链接:https://www.haomeiwen.com/subject/dixvwrtx.html