美文网首页
支付宝退款

支付宝退款

作者: 小别墅是毛坯 | 来源:发表于2020-01-09 14:13 被阅读0次

    支付宝退款

    支付宝退款可微信退款差不多 只是配置文件和第三方调用不同
    支付宝没有退款回调通知

    @ApiOperation(value = "确认并退款", notes = "确认并退款")
        @PutMapping(value = "/updateAfter/{refundId}")
        @ApiImplicitParams({@ApiImplicitParam(name = "ACCESS_TOKEN", value = "接口调用凭证", defaultValue = "06855244f2f221da4cd0a395c0d3c68c", dataType = "string", required = true, paramType = "query")})
        public void updateAfter(@ApiParam(value = "售后单Id", required = true) @PathVariable("refundId") Integer refundId,
                                @ApiParam(value = "订单类型(0 购物车付款 1 直接支付)")@RequestParam(value = "orderType")Integer orderType)  {
             AdminUser adminUser = SessionUtil.getUser(request, AdminUser.class);
            goodsOrderRefundService.updateAfter(refundId,adminUser.getId(), HttpUtil.getIpAddr(request),orderType);
        }
    
     
    
    
        /**
         * 确认收货并退款
         *
         * @param refundId
         * @param orderType
         */
        @Transactional(rollbackFor = Exception.class)
        public void updateAfter(Integer refundId, Integer adminId, String ip, Integer orderType) {
            GoodsOrderRefund goodsOrderRefund = new GoodsOrderRefund();
            if (Objects.equals(orderType, shoppingPay.getCode())){
                 goodsOrderRefund = shoppingPay(refundId);
    
            }else {
    
                OrderRefundBo orderRefundBo = goodsOrderRefundMapper.getOrderRefundBo(refundId);
                AssertUtil.notNull(orderRefundBo.getPayType(), ExceptionEnum.OrderRefundInvalid);
    
                goodsOrderRefund.setId(refundId);
                goodsOrderRefund.setDeleteFlag(DeleteFlagEnum.NotDeleted.getCode());
                goodsOrderRefund = goodsOrderRefundMapper.selectOne(goodsOrderRefund);
                AssertUtil.notNull(goodsOrderRefund, ExceptionEnum.invalidOrderRefund);
                //生成退款单号
                String refundNo = "R" + DateUtils.getTimeString(DateUtils.DATE_YYYYMMDDHHMMSS, new Date()) + RandomStringUtils.randomNumeric(6);
    
                //更新退款单状态为-退款中
                GoodsOrderRefund temp = new GoodsOrderRefund();
                temp.setRefundStatus(OrderRefundStatusEnum.refunding.getCode());
                temp.setRefundNo(refundNo);
                Example examples = new Example(GoodsOrderRefund.class);
                examples.createCriteria()
                        .andEqualTo("id", refundId)
                        .andEqualTo("deleteFlag", DeleteFlagEnum.NotDeleted.getCode());
                goodsOrderRefundMapper.updateByExampleSelective(temp, examples);
                //更新订单详情为-退款中
                GoodsOrderDetail goodsOrderDetail = new GoodsOrderDetail();
                goodsOrderDetail.setRefundStatus(OrderRefundStatusEnum.refunding.getCode());
                Example goodsOrderDetailExample = new Example(GoodsOrderDetail.class);
                goodsOrderDetailExample.createCriteria().andEqualTo("orderId", goodsOrderRefund.getOrderId())
                        .andEqualTo("goodsProductId", goodsOrderRefund.getGoodsProductId())
                        .andEqualTo("refundStatus", OrderRefundStatusEnum.checking.getCode());
                goodsOrderDetailMapper.updateByExampleSelective(goodsOrderDetail, goodsOrderDetailExample);
    
                WechatPayDto dto = goodsOrderRefundMapper.selectRefund(refundId);
                //订单id
                String outTradeNo = dto.getOutTradeNo();
                //订单总金额
                String totalFee = dto.getTotalFee();
                //退款金额
                String refundFee = dto.getRefundFee();
                //退款单号
                String outRefundNo = dto.getOutRefundNo();
                //微信反的订单号
                String transactionId = dto.getTransactionId();
      
                /*************支付宝退款************/
                if (Objects.equals(orderRefundBo.getPayType(), PayTypeEnum.waitReceipt.getCode())) {
                    AilPayDto ailPayDto = goodsOrderRefundMapper.selectAilPayDto(refundId);
                    //生成的退款单号
                    String refundNum = ailPayDto.getOutRefundNo();
                    //支付单号
                    String payOrderNo = ailPayDto.getPayOrderNo();
                    System.out.println(payOrderNo);
                    //退款金额
                    BigDecimal dtoRefundFee = ailPayDto.getRefundFee();
                    //第三方交易单号
                    String dtoTransactionId = ailPayDto.getTransactionId();
                    //退款原因
                    String remark = ailPayDto.getRemark();
                    Map<String, String> request = AlipayUtil.generateRefund(refundNum, remark, payOrderNo, dtoRefundFee, dtoTransactionId);
                    AssertUtil.notNull(request, ExceptionEnum.AliPayRequestError);
                    System.out.println(request.get("msg"));
                    AssertUtil.isTrue(Objects.equals(request.get("msg"), "Success"), ExceptionEnum.AlipayRefundError);
                    //修改退款订单状态
                    alipayNotify(request);
                }
            }
    
    
    /***********支付宝回调改状态************/
      public boolean alipayNotify(Map<String, String> map) {
    
            //退款状态(SUCCESS—退款成功、REFUNDCLOSE—退款关闭、PROCESSING—退款处理中、CHANGE—退款异常)
            String refundStatus = map.get("msg");
    
            if (!Objects.equals(refundStatus, "Success")) {
                return false;
            } else {
                AilPayDto dto = new AilPayDto();
    
                //第三方返回的退款单号
                dto.setTradeNo(map.get("trade_no"));
    
                //第三方交易订单号
                dto.setTransactionId(map.get("out_trade_no"));
    
                //生成的退款单号
                dto.setOutRefundNo(map.get("refund_order_no"));
                //退款原因
                dto.setRemark(map.get("remark"));
                //退款金额
                dto.setRefundFee(new BigDecimal(map.get("refund_fee")));
                //手续费
                dto.setFee(new BigDecimal(map.get("fee")));
                //交易单号
                dto.setPayOrderNo(map.get("pay_order_no"));
                //退款成功时间
                dto.setSuccessTime(map.get("gmt_refund_pay"));
    
                /**修改订单等状态**/
                goodsAliOrderStatus(dto);
                return true;
            }
    
    @Data
    @ApiModel("支付宝退款信息数据")
    public class AilPayDto implements Serializable {
    
        @ApiModelProperty("第三方交易单号")
        private String transactionId;
    
        @ApiModelProperty("订单金额")
        private BigDecimal refundFee;
    
        @ApiModelProperty("支付单号")
        private String payOrderNo;
    
        @ApiModelProperty("退款订单单号")
        private String outRefundNo;
    
        @ApiModelProperty("退款原因")
        private String remark;
    
        @ApiModelProperty("退款成功时间")
        private String successTime;
    
        @ApiModelProperty("系统退款单号")
        private String tradeNo;
    
        @ApiModelProperty("手续费")
        private BigDecimal fee;
    }
    
      /*************AlipayUtils**********/
       private static final PropertiesUtil ALI_PAY = new PropertiesUtil("/configs/alipay.properties");
        private static AlipayClient alipayClient;
    
        public static final String ALIPAY_REFUND_URL;
    
        static {
            alipayClient = new DefaultAlipayClient(ALI_PAY.getProperty("alipay_getway"),
                ALI_PAY.getProperty("alipay.app.id"),
                ALI_PAY.getProperty("rsa_private_key"),
                ALI_PAY.getProperty("formatter"),
                ALI_PAY.getProperty("charset"),
                ALI_PAY.getProperty("alipay_public_key"),
                ALI_PAY.getProperty("sign_type")
            );
            ALIPAY_REFUND_URL = ALI_PAY.getProperty("alipay.refund.url");
        }
    
    
        private static final String TRADE_STATUS_SUCCESS = "SUCCESS";
    
    
        //https://docs.open.alipay.com/api_1/alipay.trade.refund/   统一收单交易退款接口
        //https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query/   统一收单交易退款查询
        //https://opensupport.alipay.com/support/knowledge/27585/201602348776?ant_source=zsearch 退款是否支持回调
     /**              
         * 支付宝订单退款
         *
         * @param refundDesc    退款原因
         * @param payOorderNo   支付单号(支付时生成的)
         * @param refundAmount  退款金额
         * @param dtoTransactionId 退款单号
         * @return 支付宝订单退款
         */
        public static Map<String, String> generateRefund(String refundNum, String refundDesc, String payOorderNo, BigDecimal refundAmount, String dtoTransactionId) {
    
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
                AlipayTradeRefundModel model = new AlipayTradeRefundModel();
                //交易订单号
                model.setOutTradeNo(payOorderNo);
                //退款金额
                model.setRefundAmount(refundAmount.setScale(2, BigDecimal.ROUND_HALF_DOWN) + "");
                model.setRefundReason(refundDesc);
                request.setBizModel(model);
            try {
                AlipayTradeRefundResponse response = alipayClient.execute(request);
                System.out.println("sss" +response);
                Map<String, String> result = Maps.newHashMap();
    
                result.put("code", response.getCode());
                result.put("msg",response.getMsg());
                //退款后反的单号
                result.put("trade_no", response.getTradeNo());
                //支付单号
                result.put("pay_order_no",payOorderNo);
                //退款金额
                result.put("refund_fee",response.getRefundFee());
                //退款原因
                result.put("remark",refundDesc);
                //生成的退款单号
                result.put("refund_order_no",refundNum);
                //退款成功时间
                result.put("gmt_refund_pay", DateUtils.getTimeString(DateUtils.DATE_YYYY_MM_DD_HHMMSS,response.getGmtRefundPay()));
                //退款手续费
                result.put("fee",response.getSendBackFee());
                //退款单号
                result.put("out_trade_no",dtoTransactionId);
                System.out.println(response);
                System.out.println(response.getCode());
                System.out.println(response.getMsg());
                System.out.println(response.getTradeNo());
                return result;
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    #alipay.app.id=++++++
    #数据加密类型
    sign_type=RSA2
    #数据传输格式
    formatter=json
    #数据编码格式
    charset=UTF-8
    #退款回调根地址(此为地址为根地址并非业务回调地址)
    #alipay.refund.url=https://-----------
    #支付宝支付网关
    #alipay_getway=https://openapi.alipay.com/gateway.do
    #支付宝公钥
    #alipay_public_key=
    #商户私钥
    #rsa_private_key=
    

    相关文章

      网友评论

          本文标题:支付宝退款

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