Authored by LUOXC

退款

... ... @@ -7,51 +7,10 @@ import java.util.List;
public interface OrdersPayRefundMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table orders_pay_refund
*
* @mbggenerated
*/
int insert(OrdersPayRefund record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table orders_pay_refund
*
* @mbggenerated
*/
int insertSelective(OrdersPayRefund record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table orders_pay_refund
*
* @mbggenerated
*/
OrdersPayRefund selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table orders_pay_refund
*
* @mbggenerated
*/
int updateByPrimaryKeySelective(OrdersPayRefund record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table orders_pay_refund
*
* @mbggenerated
*/
int updateByPrimaryKey(OrdersPayRefund record);
//根据订单号获取退款记录
List<OrdersPayRefund> selectByOrderCode(long orderCode);
//查看部分退款记录是否存在
//OrdersPayRefund selectByRefundOrderCode(@Param("refundOrderCode") String refundOrderCode);
List<OrdersPayRefund> selectByOrderCode(Long orderCode);
//查看部分退款记录是否存在 -- (新)
OrdersPayRefund selectByRefundCode(@Param("refundCode") String refundCode);
... ...
... ... @@ -26,6 +26,8 @@ public class TradeBills {
private Long orderCode;
private Long paidOrderCode;
//1:买家uid; 2:卖家uid
private Integer userType;
... ... @@ -64,11 +66,11 @@ public class TradeBills {
private Integer dealRelateId = 0;
@Deprecated
public static enum Status {
public enum Status {
TRANSFER_WAITING(90);
private Status(int code) {
Status(int code) {
this.code = code;
}
private int code;
... ...
... ... @@ -2,10 +2,6 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yohoufo.dal.order.OrdersPayRefundMapper" >
<resultMap id="BaseResultMap" type="com.yohoufo.dal.order.model.OrdersPayRefund" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" property="id" jdbcType="INTEGER" />
<result column="order_code" property="orderCode" jdbcType="BIGINT" />
<result column="serial_no" property="serialNo" jdbcType="VARCHAR" />
... ... @@ -16,23 +12,8 @@
<result column="refund_code" property="refundCode" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, order_code, serial_no, amount, status,
create_time, update_time
id, order_code, serial_no, amount, status, create_time, update_time, refund_code
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from orders_pay_refund
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByOrderCode" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
... ... @@ -50,113 +31,11 @@
</select>
<insert id="insert" parameterType="com.yohoufo.dal.order.model.OrdersPayRefund" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into orders_pay_refund (id, order_code,
serial_no, amount,
status, create_time,
update_time, refund_code, order_type)
insert into orders_pay_refund (id, order_code, serial_no, amount, status, create_time, update_time, refund_code, order_type)
values (#{id,jdbcType=INTEGER}, #{orderCode,jdbcType=BIGINT},
#{serialNo,jdbcType=VARCHAR}, #{amount,jdbcType=DECIMAL},
#{status,jdbcType=TINYINT}, #{createTime,jdbcType=INTEGER},
#{updateTime,jdbcType=INTEGER}, #{refundCode,jdbcType=VARCHAR}, #{orderType,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yohoufo.dal.order.model.OrdersPayRefund" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into orders_pay_refund
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="orderCode != null" >
order_code,
</if>
<if test="serialNo != null" >
serial_no,
</if>
<if test="amount != null" >
amount,
</if>
<if test="status != null" >
status,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="updateTime != null" >
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="orderCode != null" >
#{orderCode,jdbcType=BIGINT},
</if>
<if test="serialNo != null" >
#{serialNo,jdbcType=VARCHAR},
</if>
<if test="amount != null" >
#{amount,jdbcType=DECIMAL},
</if>
<if test="status != null" >
#{status,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
<if test="updateTime != null" >
#{updateTime,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.order.model.OrdersPayRefund" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update orders_pay_refund
<set >
<if test="orderCode != null" >
order_code = #{orderCode,jdbcType=BIGINT},
</if>
<if test="serialNo != null" >
serial_no = #{serialNo,jdbcType=VARCHAR},
</if>
<if test="amount != null" >
amount = #{amount,jdbcType=DECIMAL},
</if>
<if test="status != null" >
status = #{status,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="updateTime != null" >
update_time = #{updateTime,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.order.model.OrdersPayRefund" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update orders_pay_refund
set
order_code = #{orderCode,jdbcType=BIGINT},
serial_no = #{serialNo,jdbcType=VARCHAR},
amount = #{amount,jdbcType=DECIMAL},
status = #{status,jdbcType=TINYINT},
create_time = #{createTime,jdbcType=INTEGER},
update_time = #{updateTime,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -5,6 +5,7 @@
<id column="id" jdbcType="INTEGER" property="id" />
<result column="uid" jdbcType="INTEGER" property="uid" />
<result column="order_code" jdbcType="BIGINT" property="orderCode" />
<result column="paid_order_code" jdbcType="BIGINT" property="paidOrderCode" />
<result column="user_type" jdbcType="TINYINT" property="userType" />
<result column="pay_type" jdbcType="TINYINT" property="payType" />
<result column="trade_type" jdbcType="INTEGER" property="tradeType" />
... ... @@ -21,7 +22,7 @@
<result column="deal_user_name" jdbcType="VARCHAR" property="dealUserName" />
</resultMap>
<sql id="Base_Column_List">
id,uid, order_code, user_type,pay_type,trade_type,income_outcome,amount,system_amount,trade_status,create_time,deal_uid,deal_status,deal_time,deal_relate_id,deal_user_name
id,uid, order_code,paid_order_code, user_type,pay_type,trade_type,income_outcome,amount,system_amount,trade_status,create_time,deal_uid,deal_status,deal_time,deal_relate_id,deal_user_name
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
... ... @@ -39,10 +40,10 @@
</select>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.TradeBills" useGeneratedKeys="true">
insert into trade_bills (id, uid, order_code, user_type,pay_type,trade_type,
insert into trade_bills (id, uid, order_code, paid_order_code, user_type,pay_type,trade_type,
income_outcome,amount,system_amount,trade_status,create_time
,deal_uid,deal_status,deal_time,deal_relate_id,deal_user_name)
values (#{id},#{uid},#{orderCode},#{userType},#{payType},#{tradeType},
values (#{id},#{uid},#{orderCode},#{paidOrderCode},#{userType},#{payType},#{tradeType},
#{incomeOutcome},#{amount},#{systemAmount},
#{tradeStatus},#{createTime},#{dealUid},#{dealStatus},#{dealTime},#{dealRelateId},#{dealUserName})
</insert>
... ...
... ... @@ -129,9 +129,10 @@ public class PaymentController {
*/
@RequestMapping(value = "/erp/order/refund")
public ApiResponse refund(@RequestParam(name = "orderCode") long orderCode,
@RequestParam(name = "paidOrderCode",required = false) Long paidOrderCode,
@RequestParam(name = "refundAmount") double refundAmount){
PaymentRequest request = PaymentRequest.builder().orderCode(orderCode).refundAmount(refundAmount).build();
PaymentRequest request = PaymentRequest.builder().orderCode(orderCode).paidOrderCode(paidOrderCode).refundAmount(refundAmount).build();
logger.info("ufo.order.refund {}", request);
PayRefundBo payRefundBo = paymentService.refund(request);
... ...
... ... @@ -19,6 +19,8 @@ public class PaymentRequest {
private long orderCode;
private Long paidOrderCode;
private int payment;
private int skup;
... ...
... ... @@ -115,7 +115,7 @@ public class RefundManager {
}
// 确认状态是已支付, 存在支付状态
AbstractOrderPaymentService orderPaymentService = orderPaymentServiceFactory.get(orderCode);
AbstractOrderPaymentService orderPaymentService = orderPaymentServiceFactory.get(paidOrderCode);
OrderInfo orderInfo = orderPaymentService.getOrderInfo(orderCode);
if (!orderPaymentService.canRefund(orderInfo)) {
... ... @@ -125,7 +125,7 @@ public class RefundManager {
// 获取支付成功记录
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(paidOrderCode, orderInfo.getUid());
if (ordersPay == null || ordersPay.getAmount() == null) {
if (Objects.isNull(ordersPay) || Objects.isNull(ordersPay.getAmount())) {
log.warn("refund paid record not exist.orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
}
... ... @@ -137,6 +137,9 @@ public class RefundManager {
throw new ServiceException(ServiceError.ORDER_REFUND_OVER_TOTAL_AMOUNT);
}
throwServiceExceptionIf(Objects.isNull(orderInfo.getPayment()),
exceptionMessage.apply("退款渠道不存在"));
// 退款
AbstractPayService payService = payServiceFactory.getPayService(orderInfo.getPayment());
throwServiceExceptionIf(Objects.isNull(payService),
... ... @@ -144,6 +147,9 @@ public class RefundManager {
PayRefundBo payRefundBo = buildRefundRequest(orderInfo, ordersPay);
throwServiceExceptionIf(Objects.nonNull(ordersPayRefundMapper.selectByRefundCode(payRefundBo.getRefundOrderCode())),
exceptionMessage.apply("已经退款成功过"));
RefundContext context = new RefundContext();
context.orderInfo = orderInfo;
context.ordersPay = ordersPay;
... ... @@ -167,7 +173,7 @@ public class RefundManager {
payRefundBo.setPayOrderCode(ordersPay.getPayOrderCode());
payRefundBo.setAmount(refundAmount);
payRefundBo.setOrderTotalFee(ordersPay.getAmount().doubleValue());
payRefundBo.setRefundOrderCode(String.valueOf(orderCode) + ordersPay.getId());
payRefundBo.setRefundOrderCode(String.valueOf(orderCode));
payRefundBo.setUid(ordersPay.getUid());
payRefundBo.setOrderCreateTime(orderInfo.getCreateTime());
payRefundBo.setAttach(refundAttach);
... ... @@ -203,17 +209,15 @@ public class RefundManager {
// 钱包支付或其他渠道支付成功,添加退款申请成功的记录
if (Payment.WALLET.getCode() == context.orderInfo.getPayment().intValue()
|| context.response.getRefundStatus() == RefundContant.PAYMENT_REFUND_RESULTCODE_SUCCESS) {
recordPayRefund(context.response);
recordPayRefund(context.ordersPay, context.response);
}
}
/**
* 数据库记录退款申请
*
* @param refundBo
*/
private void recordPayRefund(PayRefundBo refundBo) {
private void recordPayRefund(OrdersPay ordersPay, PayRefundBo refundBo) {
log.info("refund record, refundOrderCode: {}, serialNo: {}", refundBo.getRefundOrderCode(), refundBo.getSerialNo());
// 一个订单号,只能退款一次?
OrdersPayRefund refundRecord = ordersPayRefundMapper.selectByRefundCode(refundBo.getRefundOrderCode());
... ... @@ -223,7 +227,7 @@ public class RefundManager {
}
OrdersPayRefund newRecord = new OrdersPayRefund();
newRecord.setOrderCode(Long.valueOf(refundBo.getOrderCode()));
newRecord.setOrderCode(ordersPay.getOrderCode());
newRecord.setRefundCode(refundBo.getRefundOrderCode());
newRecord.setAmount(BigDecimal.valueOf(refundBo.getAmount()));
newRecord.setStatus((byte) 0);
... ...
... ... @@ -411,6 +411,7 @@ public class PaymentServiceImpl implements IPaymentService {
public PayRefundBo refund(PaymentRequest request) {
return refundManager.create(request.getOrderCode(), request.getRefundAmount())
.withPaidOrderCode(request.getPaidOrderCode())
.withRefundAttach(request.getRefundattch())
.refund();
... ... @@ -852,24 +853,7 @@ public class PaymentServiceImpl implements IPaymentService {
try {
logger.info("{}, refund money is {}", logTag, tradeBills);
refundManager.create(orderCode, amount.doubleValue())
.withRefundPreHandler(context -> {
OrderInfo orderInfo = context.getOrderInfo();
Function<Object, String> exceptionMessage = appendMessage -> String.format("订单[%s],%s!", orderCode, appendMessage);
// 获取支付成功记录
OrdersPay ordersPay = context.getOrdersPay();
throwServiceExceptionIf(Objects.isNull(ordersPay) || Objects.isNull(ordersPay.getAmount()) || Objects.isNull(ordersPay.getPayment()),
exceptionMessage.apply("尚未支付"));
throwServiceExceptionIf(CollectionUtils.isNotEmpty(ordersPayRefundMapper.selectByOrderCode(orderCode)),
exceptionMessage.apply("已经退款成功过"));
throwServiceExceptionIf(ordersPay.getAmount().compareTo(amount) < 0,
exceptionMessage.apply("退款金额大于支付金额"));
throwServiceExceptionIf(Objects.isNull(orderInfo.getPayment()),
exceptionMessage.apply("退款渠道不存在"));
})
.withPaidOrderCode(tradeBills.getPaidOrderCode())
.withRefundPostHandler(context -> {
PayRefundBo refundBo = context.getResponse();
//退款申请成功
... ...