Authored by chenchao

buyer cancel use new calculate args

... ... @@ -18,6 +18,7 @@ public class BuyerPenaltyCalResult {
private Integer uid;
private Long orderCode;
private BigDecimal orderActualAmount;
private BigDecimal goodsActualAmount;
private BigDecimal penaltyAmount;
private BigDecimal leftAmount;
private BigDecimal penaltyRate;
... ...
... ... @@ -69,7 +69,7 @@ public class BuyerOrderCancelService {
private CouponProxyService couponProxyService;
@Autowired
private IPaymentService paymentService;
private TransferService transferService;
@Autowired
private SellerOrderMapper sellerOrderMapper;
... ... @@ -100,6 +100,7 @@ public class BuyerOrderCancelService {
*/
public BuyerPenaltyCalResult calBuyerPenalty(Integer buyerUid, Long orderCode, OrderStatus orderStatus,
BigDecimal orderActualAmount,
BigDecimal shipFee,
OrderAttributes orderAttributes
){
if (Objects.isNull(orderAttributes)){
... ... @@ -144,7 +145,7 @@ public class BuyerOrderCancelService {
}
}
//TODO quick deliver
BuyerPenaltyCalResult bpcr = calBuyerPenalty( mfee,buyerUid, orderCode, orderActualAmount);
BuyerPenaltyCalResult bpcr = calBuyerPenalty( mfee,buyerUid, orderCode, orderActualAmount, shipFee);
if (bpcr.getLeftAmount().compareTo(BigDecimal.ZERO)<=0){
throw new UfoServiceException(501, "left amount not enough");
}
... ... @@ -152,16 +153,19 @@ public class BuyerOrderCancelService {
}
public BuyerPenaltyCalResult calBuyerPenalty(BuyerPenalty.Fee mfee,Integer buyerUid, Long orderCode,
BigDecimal orderActualAmount){
BigDecimal orderActualAmount, BigDecimal shipFee){
BigDecimal penaltyRate = mfee.getRate();
BigDecimal penaltyAmount = BigDecimalHelper.halfUp(orderActualAmount.multiply(penaltyRate));
BigDecimal goodsActualAmount = orderActualAmount.subtract(shipFee);
BigDecimal penaltyAmount = BigDecimalHelper.halfUp(goodsActualAmount.multiply(penaltyRate));
BuyerPenalty.Range moneyRange = mfee.getMoneyRange();
if (penaltyAmount.compareTo(moneyRange.getMin()) < 0){
penaltyAmount = moneyRange.getMin();
}
BigDecimal leftAmount = orderActualAmount.subtract(penaltyAmount);
BuyerPenaltyCalResult bpcr = BuyerPenaltyCalResult.builder().uid(buyerUid).orderCode(orderCode)
.orderActualAmount(orderActualAmount).penaltyRate(penaltyRate)
.orderActualAmount(orderActualAmount)
.goodsActualAmount(goodsActualAmount)
.penaltyRate(penaltyRate)
.penaltyAmount(penaltyAmount).leftAmount(leftAmount).build();
logger.info("calBuyerPenalty mfee {} BuyerPenaltyCalResult {}", mfee, bpcr);
return bpcr;
... ... @@ -331,6 +335,87 @@ public class BuyerOrderCancelService {
this.transferCase = transferCase;
}
private PayRefundBo refundSellerEarnestMoney(SellerOrder sellerOrder){
PayRefundBo prb = null;
int sellerUid = sellerOrder.getUid();
long sellerOrderCode = sellerOrder.getOrderCode();
BigDecimal earnestMoney = sellerOrder.getEarnestMoney();
PaymentRequest refundReqOfSeller = PaymentRequest.builder().uid(sellerUid).refundCase(RefundCase.SELLER_EARNEST_MONEY)
.orderCode(sellerOrderCode).refundAmount(earnestMoney.doubleValue())
.build();
boolean sellerIsSuper = sellerService.isSuperEntrySeller(sellerUid);
refundReqOfSeller.setSuper(sellerIsSuper);
if (!sellerIsSuper && PaymentHelper.isWallet(sellerOrder.getPayment())){
MerchantOrderAttachInfo moai = MerchantOrderAttachInfo.builder().uid(sellerUid)
.orderCode(sellerOrderCode).skup(skup).earnestMoney(sellerOrder.getEarnestMoney())
.type(swdType.getValue()).build();
refundReqOfSeller.setRefundattch(moai);
}
BillLogEvent.BillLogEventBuilder bleb = BillLogEvent.builder()
.buyerUid(buyerUid).sellerUid(sellerUid).orderCode(sellerOrder.getOrderCode())
.payType(sellerOrder.getPayment()).refundCase(RefundCase.SELLER_EARNEST_MONEY)
.skup(skup);
logger.info("in BuyerCancelAfterProcessTask call begin payRefundService.refund seller, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, refundReqOfSeller );
try {
prb = payRefundService.refund(refundReqOfSeller, bleb);
}catch (Exception ex){
if (!sellerIsSuper) {
BillLogEvent sellerBillLogEvent = bleb.tradeStatus(BillTradeStatus.FAIL.getCode())
.build();
EventBusPublisher.publishEvent(sellerBillLogEvent);
}
String content = "用户" + sellerUid + "退保证金订单"+ orderCode +"失败";
SmsAlarmEvent smsAlarmEvent = new SmsAlarmEvent("buyerPaidOrder.selfCancel", "cancel", content);
EventBusPublisher.publishEvent(smsAlarmEvent);
}
logger.info("in BuyerCancelAfterProcessTask call after payRefundService.refund seller, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, refundReqOfSeller );
return prb;
}
void transferPenalty(BuyerPenaltyCalResult bpcr){
TranseferCellNode transeferCellNode = new TranseferCellNode();
transeferCellNode.setUid(buyerUid);
transeferCellNode.setAmount(bpcr.getPenaltyAmount());
TransferMoneyRequest tmReq = TransferMoneyRequest.builder().buyerOrderCode(orderCode)
.type(transferCase.getCode()).transferCase(transferCase)
.transeferCellNode(transeferCellNode).build();
logger.info("in BuyerCancelAfterProcessTask call begin paymentService.transferMon, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, tmReq );
transferService.transfer(tmReq);
logger.info("in BuyerCancelAfterProcessTask call after paymentService.transferMon, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, tmReq );
}
void refundLeftOrderAmount(BuyerPenaltyCalResult bpcr, BuyerOrder buyerOrder){
Integer sellerUid = buyerOrder.getSellerUid();
BigDecimal leftBuyerMoney = bpcr.getLeftAmount();
PaymentRequest rprOfBuyer = PaymentRequest.builder().uid(buyerUid)
.orderCode(orderCode).refundAmount(leftBuyerMoney.doubleValue())
.build();
BillLogEvent.BillLogEventBuilder blebOfBuyer = BillLogEvent.builder()
.buyerUid(buyerUid).sellerUid(sellerUid).orderCode(orderCode)
.payType(buyerOrder.getPayment()).refundCase(RefundCase.BUYER_GOODS_MONEY)
.amount(leftBuyerMoney)
.skup(skup);
logger.info("in BuyerCancelAfterProcessTask call begin payRefundService.refund buyer, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, rprOfBuyer );
try {
payRefundService.refund(rprOfBuyer, blebOfBuyer);
}catch (Exception ex){
BillLogEvent buyerBillLogEvent = blebOfBuyer.tradeStatus(BillTradeStatus.FAIL.getCode()).build();
EventBusPublisher.publishEvent(buyerBillLogEvent);
String content = "用户" + buyerUid + "订单"+ orderCode +"退货款失败";
SmsAlarmEvent smsAlarmEvent = new SmsAlarmEvent("buyerPaidOrder.selfCancel", "cancel", content);
EventBusPublisher.publishEvent(smsAlarmEvent);
}
logger.info("in BuyerCancelAfterProcessTask call after payRefundService.refund buyer, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, rprOfBuyer );
}
@Override
public PayRefundBo call() throws Exception {
// 整个过程异步去执行(考虑退费依赖订单状态)
... ... @@ -342,80 +427,21 @@ public class BuyerOrderCancelService {
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(skup);
logger.info("in BuyerCancelAfterProcessTask call sellerOrderMapper.selectBySkup, buyerUid {}, orderCode {}, skup {}",
buyerUid, orderCode, skup );
int sellerUid = sellerOrder.getUid();
long sellerOrderCode = sellerOrder.getOrderCode();
BigDecimal earnestMoney = sellerOrder.getEarnestMoney();
PaymentRequest refundReqOfSeller = PaymentRequest.builder().uid(sellerUid).refundCase(RefundCase.SELLER_EARNEST_MONEY)
.orderCode(sellerOrderCode).refundAmount(earnestMoney.doubleValue())
.build();
boolean sellerIsSuper = sellerService.isSuperEntrySeller(sellerUid);
refundReqOfSeller.setSuper(sellerIsSuper);
if (!sellerIsSuper && PaymentHelper.isWallet(sellerOrder.getPayment())){
MerchantOrderAttachInfo moai = MerchantOrderAttachInfo.builder().uid(sellerUid)
.orderCode(sellerOrderCode).skup(skup).earnestMoney(sellerOrder.getEarnestMoney())
.type(swdType.getValue()).build();
refundReqOfSeller.setRefundattch(moai);
}
prb = refundSellerEarnestMoney(sellerOrder);
BillLogEvent.BillLogEventBuilder bleb = BillLogEvent.builder()
.buyerUid(buyerUid).sellerUid(sellerUid).orderCode(sellerOrder.getOrderCode())
.payType(sellerOrder.getPayment()).refundCase(RefundCase.SELLER_EARNEST_MONEY)
.skup(skup);
logger.info("in BuyerCancelAfterProcessTask call begin payRefundService.refund seller, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, refundReqOfSeller );
try {
prb = payRefundService.refund(refundReqOfSeller, bleb);
}catch (Exception ex){
if (!sellerIsSuper) {
BillLogEvent sellerBillLogEvent = bleb.tradeStatus(BillTradeStatus.FAIL.getCode())
.build();
EventBusPublisher.publishEvent(sellerBillLogEvent);
}
String content = "用户" + sellerUid + "退保证金订单"+ orderCode +"失败";
SmsAlarmEvent smsAlarmEvent = new SmsAlarmEvent("buyerPaidOrder.selfCancel", "cancel", content);
EventBusPublisher.publishEvent(smsAlarmEvent);
}
logger.info("in BuyerCancelAfterProcessTask call after payRefundService.refund seller, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, refundReqOfSeller );
//(转账)瓜分指定赔偿款给卖家和平台
TransferMoneyRequest tmReq = TransferMoneyRequest.builder().buyerOrderCode(orderCode)
.type(transferCase.getCode()).transferCase(transferCase).build();
logger.info("in BuyerCancelAfterProcessTask call begin paymentService.transferMon, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, tmReq );
paymentService.transferMon(tmReq);
logger.info("in BuyerCancelAfterProcessTask call after paymentService.transferMon, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, tmReq );
//(退费)扣除赔偿款,计算剩余的货款,退给买家
BigDecimal compensateMoney = compensate.getBuyerCompensateMoney();
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
logger.info("in BuyerCancelAfterProcessTask call begin buyerOrderMapper.selectByOrderCode, buyerUid {}, orderCode {}, skup {} buyerOrder {}",
buyerUid, orderCode, skup, buyerOrder );
BigDecimal leftBuyerMoney = buyerOrder.getAmount().subtract(compensateMoney);
PaymentRequest rprOfBuyer = PaymentRequest.builder().uid(buyerUid)
.orderCode(orderCode).refundAmount(leftBuyerMoney.doubleValue())
.build();
BillLogEvent.BillLogEventBuilder blebOfBuyer = BillLogEvent.builder()
.buyerUid(buyerUid).sellerUid(sellerUid).orderCode(orderCode)
.payType(buyerOrder.getPayment()).refundCase(RefundCase.BUYER_GOODS_MONEY)
.amount(leftBuyerMoney)
.skup(skup);
logger.info("in BuyerCancelAfterProcessTask call begin payRefundService.refund buyer, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, rprOfBuyer );
try {
payRefundService.refund(rprOfBuyer, blebOfBuyer);
}catch (Exception ex){
BillLogEvent buyerBillLogEvent = blebOfBuyer.tradeStatus(BillTradeStatus.FAIL.getCode()).build();
EventBusPublisher.publishEvent(buyerBillLogEvent);
String content = "用户" + buyerUid + "订单"+ orderCode +"退货款失败";
SmsAlarmEvent smsAlarmEvent = new SmsAlarmEvent("buyerPaidOrder.selfCancel", "cancel", content);
EventBusPublisher.publishEvent(smsAlarmEvent);
}
logger.info("in BuyerCancelAfterProcessTask call after payRefundService.refund buyer, buyerUid {}, orderCode {}, skup {} refundReqOfSeller {}",
buyerUid, orderCode, skup, rprOfBuyer );
OrderStatus orderStatus = OrderStatus.getOrderStatus(buyerOrder.getStatus());
BigDecimal orderActualAmount = buyerOrder.getAmount();
BigDecimal shipFee = buyerOrder.getShipFee();
OrderAttributes orderAttributes = OrderAttributes.getOrderAttributes(buyerOrder.getAttributes());
//TODO try catch to record fail transfer
BuyerPenaltyCalResult bpcr = calBuyerPenalty(buyerUid, orderCode, orderStatus, orderActualAmount, shipFee, orderAttributes);
transferPenalty(bpcr);
//(退费)扣除赔偿款,计算剩余的货款,退给买家
refundLeftOrderAmount(bpcr, buyerOrder);
logger.info("out BuyerCancelAfterProcessTask call buyerUid {}, orderCode {}, skup {}, transferCase {}, compensate {}",
buyerUid, orderCode, skup, transferCase, compensate);
}catch (Exception ex){
... ... @@ -423,7 +449,6 @@ public class BuyerOrderCancelService {
buyerUid, orderCode, skup, transferCase, compensate, ex);
}
return prb;
}
}
... ...
... ... @@ -24,6 +24,7 @@ import com.yohoufo.order.model.PayQueryBo;
import com.yohoufo.order.model.PayRefundBo;
import com.yohoufo.order.model.PaymentData;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TranseferCellNode;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.model.response.PaymentConfirmRsp;
import com.yohoufo.order.model.response.PrepayResponse;
... ... @@ -577,8 +578,10 @@ public class PaymentServiceImpl implements IPaymentService {
record.setCreateTime(now);
logger.info("transferMon参数检查成功!插入初始化转账信息成功,接下来计算费用");
TranseferCellNode transeferCellNode = request.getTranseferCellNode();
// 算费
BigDecimal transferAmount = calcTransferAmount(sellerOrder.getUid(), sellerOrder.getSkup(), transferCase);
BigDecimal transferAmount = calcTransferAmount(sellerOrder.getUid(),
sellerOrder.getSkup(), transferCase, transeferCellNode);
logger.info("transferMon计算费用结果为 {}", transferAmount);
if (transferAmount == null) {
logger.warn("transferMonErr transferMon计算费用结果为 null, 不合法的金额");
... ... @@ -1151,21 +1154,15 @@ public class PaymentServiceImpl implements IPaymentService {
addTradeBills(record);
}
private BigDecimal getBuyerCancelBeforeSellerDeliver(){
//todo
return buyerCancelCompensateComputeHandler.calBeforeSellerDeliver();
}
private BigDecimal getBuyerCancelBeforeDepotReceive(){
//todo
return buyerCancelCompensateComputeHandler.calBeforeDepotReceive();
}
private BigDecimal calcTransferAmount(Integer sellerUid, Integer skup, TransferCase transferCase) {
if (transferCase == TransferCase.BUYER_CANCEL_BEFORE_SELLER_DELIVER) {
return getBuyerCancelBeforeSellerDeliver();
}
if (transferCase == TransferCase.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE) {
return getBuyerCancelBeforeDepotReceive();
private BigDecimal calcTransferAmount(Integer sellerUid, Integer skup,
TransferCase transferCase,TranseferCellNode transeferCellNode) {
//buyer cancel is first priory
boolean isBuyerCancel = transferCase == TransferCase.BUYER_CANCEL_BEFORE_SELLER_DELIVER
|| transferCase == TransferCase.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE;
if (isBuyerCancel) {
return transeferCellNode.getAmount();
}
SellerOrderMeta meta = sellerOrderMetaMapper.selectByMetaKey(sellerUid, skup, "fee");
... ...
... ... @@ -30,9 +30,10 @@ public class BuyerOrderCancelServiceTest extends BaseWebTest {
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
Integer buyerUid = buyerOrder.getUid();
OrderStatus orderStatus = OrderStatus.BUYER_CANCEL_BEFORE_SELLER_DELIVER;
BigDecimal orderActualAmount = buyerOrder.getAmount().subtract(buyerOrder.getShipFee());
BigDecimal orderActualAmount = buyerOrder.getAmount(), shipFee = buyerOrder.getShipFee();
OrderAttributes orderAttributes = OrderAttributes.getOrderAttributes(buyerOrder.getAttributes());
BuyerPenaltyCalResult bpcr = buyerOrderCancelService.calBuyerPenalty(buyerUid, orderCode, orderStatus, orderActualAmount, orderAttributes);
BuyerPenaltyCalResult bpcr = buyerOrderCancelService.calBuyerPenalty(buyerUid, orderCode, orderStatus,
orderActualAmount, shipFee, orderAttributes);
System.out.println(JSONObject.toJSONString(bpcr));
}
... ... @@ -43,9 +44,10 @@ public class BuyerOrderCancelServiceTest extends BaseWebTest {
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
Integer buyerUid = buyerOrder.getUid();
OrderStatus orderStatus = OrderStatus.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE;
BigDecimal orderActualAmount = buyerOrder.getAmount().subtract(buyerOrder.getShipFee());
BigDecimal orderActualAmount = buyerOrder.getAmount(), shipFee = buyerOrder.getShipFee();
OrderAttributes orderAttributes = OrderAttributes.getOrderAttributes(buyerOrder.getAttributes());
BuyerPenaltyCalResult bpcr = buyerOrderCancelService.calBuyerPenalty(buyerUid, orderCode, orderStatus, orderActualAmount, orderAttributes);
BuyerPenaltyCalResult bpcr = buyerOrderCancelService.calBuyerPenalty(buyerUid, orderCode,
orderStatus, orderActualAmount,shipFee, orderAttributes);
System.out.println(JSONObject.toJSONString(bpcr));
}
}
... ...