...
|
...
|
@@ -3,6 +3,7 @@ package com.yohoufo.order.service.impl; |
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -633,7 +634,6 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
// 预标记成功
|
|
|
TradeBills preSuccess = new TradeBills();
|
|
|
preSuccess.setId(tradeBills.getId());
|
|
|
preSuccess.setTradeStatus(100);
|
|
|
preSuccess.setDealTime(now);
|
|
|
preSuccess.setDealStatus(1);
|
|
|
preSuccess.setDealUid(req.getOperateUid());
|
...
|
...
|
@@ -646,27 +646,36 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
if (/* 退货款 */ (tradeBills.getUserType() == 1 && tradeBills.getTradeType() == 2 && tradeBills.getIncomeOutcome() == 1)
|
|
|
/* 退保证金 */ || (tradeBills.getUserType() == 2 && tradeBills.getTradeType() == 1 && tradeBills.getIncomeOutcome() == 1)) {
|
|
|
logger.warn("manualDeal人工退款 , tradeBills is {}", tradeBills);
|
|
|
PaymentRequest pReq = new PaymentRequest();
|
|
|
pReq.setOrderCode(orderCode);
|
|
|
pReq.setRefundAmount(amount.doubleValue());
|
|
|
refund(pReq);
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
tradeBillsMapper.insert(tradeBills);
|
|
|
return;
|
|
|
PayRefundBo refundBo = manualRefund(orderCode, amount);
|
|
|
if (refundBo.getRefundStatus() == RefundContant.PAYMENT_REFUND_RESULTCODE_SUCCESS) {
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
return;
|
|
|
} else {
|
|
|
throw new ServiceException(500, "退款失败:" + refundBo.getRefundMsg());
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("manualDealErr 退款失败 , tradeBillsId is {}, msg is {}", tradeBillsId, e.getMessage());
|
|
|
// TODO alarm
|
|
|
preSuccess.setTradeStatus(beforeStatus);
|
|
|
preSuccess.setDealStatus(0);
|
|
|
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
|
|
|
throw new ServiceException(500, "退款失败id=" + tradeBillsId);
|
|
|
if (e instanceof ServiceException) {
|
|
|
throw new ServiceException(((ServiceException) e).getCode(), "退款失败id=" + tradeBillsId + "," + e.getMessage());
|
|
|
} else {
|
|
|
throw new ServiceException(500, "退款失败id=" + tradeBillsId);
|
|
|
}
|
|
|
}
|
|
|
try {
|
|
|
OrdersPayTransfer transfer = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
|
|
|
if (transfer == null || transfer.getStatus() == 1) {
|
|
|
throw new ServiceException(400, "转账记录不存在或者已成功转账,取消转账,流水id=" + tradeBillsId);
|
|
|
}
|
|
|
|
|
|
JSONObject jsonObject = alipayService.transferMoney(Long.toString(orderCode), alipayAccount, amount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("manualDeal 转账失败 , req is {}", req);
|
...
|
...
|
@@ -681,19 +690,12 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
&& jsonObject.containsKey("order_id")
|
|
|
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
|
|
|
logger.info("manualDeal转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
|
|
|
|
|
|
try {
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
tradeBillsMapper.insert(tradeBills);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("manualDeal转账成功,但是记录结果失败,tradeBillsId is " + tradeBillsId, e);
|
|
|
throw new ServiceException(500, "manualDeal转账成功,但是记录结果失败,流水id=" + tradeBillsId);
|
|
|
}
|
|
|
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
} else {
|
|
|
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
// 上报
|
...
|
...
|
@@ -702,15 +704,63 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
} catch (Exception e) {
|
|
|
logger.warn("manualDealErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
|
|
|
// alarm
|
|
|
preSuccess.setTradeStatus(beforeStatus);
|
|
|
preSuccess.setDealStatus(0);
|
|
|
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
|
|
|
throw new ServiceException(500, "转账失败id=" + tradeBillsId);
|
|
|
if (e instanceof ServiceException) {
|
|
|
throw new ServiceException(((ServiceException) e).getCode(), "转账失败id=" + tradeBillsId + "," + e.getMessage());
|
|
|
} else {
|
|
|
throw new ServiceException(500, "转账失败id=" + tradeBillsId);
|
|
|
}
|
|
|
} finally {
|
|
|
logger.info("manualDeal转账结束, tradeBillsId is {}!", tradeBillsId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private PayRefundBo manualRefund(long orderCode, BigDecimal amount){
|
|
|
|
|
|
// 根据订单号,反解 卖家订单号 or 买家订单号
|
|
|
CodeMeta codeMeta = orderCodeGenerator.expId(orderCode);
|
|
|
|
|
|
// 确认状态是已支付, 存在支付状态
|
|
|
AbstractOrderPaymentService orderPaymentService = getOrderPaymentService(codeMeta, orderCode);
|
|
|
OrderInfo orderInfo = orderPaymentService.getOrderInfo(orderCode);
|
|
|
|
|
|
// 获取支付成功记录
|
|
|
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderInfo.getOrderCode(), orderInfo.getUid());
|
|
|
if(ordersPay == null || ordersPay.getAmount() == null){
|
|
|
logger.warn("refund paid record not exist.orderCode is {}", orderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
|
|
|
}
|
|
|
|
|
|
if(!CollectionUtils.isEmpty(ordersPayRefundMapper.selectByOrderCode(orderCode))) {
|
|
|
logger.warn("refund has success before .orderCode is {}", orderCode);
|
|
|
throw new ServiceException(400, "订单已经退款成功过!");
|
|
|
}
|
|
|
|
|
|
// 支付金额小于退款金额
|
|
|
if (ordersPay.getAmount().compareTo(amount) < 0) {
|
|
|
logger.warn("refund refundAmount {} > amount {}, orderCode is {}",
|
|
|
ordersPay.getAmount(), amount, orderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_REFUND_OVER_TOTAL_AMOUNT);
|
|
|
}
|
|
|
|
|
|
// 退款
|
|
|
AbstractPayService payService = getPayService(orderInfo.getPayment());
|
|
|
|
|
|
PayRefundBo payRefundBo = new PayRefundBo();
|
|
|
payRefundBo.setPayOrderCode(ordersPay.getOrderCode());
|
|
|
payRefundBo.setAmount(amount.doubleValue());
|
|
|
payRefundBo.setOrderTotalFee(ordersPay.getAmount().doubleValue());
|
|
|
payRefundBo.setRefundOrderCode(String.valueOf(ordersPay.getOrderCode()) + ordersPay.getId());
|
|
|
PayRefundBo refundBo = payService.refundOpenApi(payRefundBo);
|
|
|
|
|
|
//退款申请成功的记录添加到orders_pay_refund表
|
|
|
if (refundBo.getRefundStatus() == RefundContant.PAYMENT_REFUND_RESULTCODE_SUCCESS) {
|
|
|
recordPayRefund(refundBo);
|
|
|
}
|
|
|
return refundBo;
|
|
|
}
|
|
|
|
|
|
private void addTradeBills(TradeBills record) {
|
|
|
try {
|
|
|
tradeBillsMapper.insert(record);
|
...
|
...
|
|