...
|
...
|
@@ -23,6 +23,7 @@ import com.yohoufo.order.constants.RefundContant; |
|
|
import com.yohoufo.order.model.PayQueryBo;
|
|
|
import com.yohoufo.order.model.PayRefundBo;
|
|
|
import com.yohoufo.order.model.PaymentData;
|
|
|
import com.yohoufo.order.model.TransferData;
|
|
|
import com.yohoufo.order.model.request.PaymentRequest;
|
|
|
import com.yohoufo.order.model.request.TranseferCellNode;
|
|
|
import com.yohoufo.order.model.request.TransferMoneyRequest;
|
...
|
...
|
@@ -53,6 +54,7 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.function.BiFunction;
|
|
|
|
...
|
...
|
@@ -329,6 +331,71 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
logger.info("paySuccess finished. orderCode is {}", orderCode);
|
|
|
abstractOrderService.processAfterPay(orderInfo);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void transferSuccess(TransferData transferData) {
|
|
|
int now = (int) (System.currentTimeMillis()/1000);
|
|
|
Integer tradeBillsId = transferData.getTradeBillsId();
|
|
|
Integer transferId = transferData.getTransferId();
|
|
|
String orderCode = transferData.getOrderCode();
|
|
|
|
|
|
TradeBills tradeBills = tradeBillsMapper.selectByPrimaryKey(tradeBillsId);
|
|
|
if (tradeBills == null) {
|
|
|
throw new ServiceException(400, "transferSuccess:流水不存在");
|
|
|
}
|
|
|
if (tradeBills.getTradeStatus() != TradeBills.Status.TRANSFER_WAITING.getCode()) {
|
|
|
throw new ServiceException(400, "transferSuccess:该流水不是异步转账的");
|
|
|
}
|
|
|
OrdersPayTransfer transfer = ordersPayTransferMapper.selectByPrimaryKey(transferId);
|
|
|
if (transfer == null) {
|
|
|
throw new ServiceException(400, "transferSuccess:转账记录不存在");
|
|
|
}
|
|
|
if (transfer.getStatus() == 1) {
|
|
|
throw new ServiceException(400, "transferSuccess:转账记录已经成功");
|
|
|
}
|
|
|
logger.info("transferSuccess 参数 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
if(StringUtils.equals("S", transferData.getStatus())) {
|
|
|
// 更新成OK
|
|
|
TradeBills success = new TradeBills();
|
|
|
success.setId(tradeBills.getId());
|
|
|
success.setDealStatus(1);
|
|
|
|
|
|
if (tradeBillsMapper.updateToOkByPrimaryKey(success) == 0) {
|
|
|
logger.warn("transferSuccess 流水已经被并发处理过 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
logger.info("transferSuccess 旧流水更新成功,准备加新流水 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
|
|
|
logger.info("transferSuccess 加新流水成功,准备修改转账状态 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
OrdersPayTransfer newTransfer = new OrdersPayTransfer();
|
|
|
newTransfer.setId(transfer.getId());
|
|
|
newTransfer.setStatus(1);
|
|
|
newTransfer.setUpdateTime(now);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(newTransfer);
|
|
|
logger.info("transferSuccess 修改转账状态成功 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
} else {
|
|
|
TradeBills fail = new TradeBills();
|
|
|
fail.setId(tradeBills.getId());
|
|
|
fail.setDealStatus(0);
|
|
|
fail.setTradeStatus(299);
|
|
|
tradeBillsMapper.updateToOkByPrimaryKey(fail);
|
|
|
logger.info("transferSuccess 旧流水(失败)更新成功,准备改转账表 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
|
|
|
OrdersPayTransfer newTransfer = new OrdersPayTransfer();
|
|
|
newTransfer.setId(transfer.getId());
|
|
|
newTransfer.setStatus(3);
|
|
|
newTransfer.setUpdateTime(now);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(newTransfer);
|
|
|
logger.info("transferSuccess 转账表(失败)更新成功 tradeBillsId={}, tradeBillsId={}, orderCode={}", tradeBillsId, tradeBillsId, orderCode);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private OrdersPay saveOrdersPay(PaymentData paymentData, long orderType, OrderInfo orderInfo) {
|
|
|
|
...
|
...
|
@@ -553,30 +620,8 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
}
|
|
|
int now = (int) (System.currentTimeMillis()/1000);
|
|
|
// 查看是否已经有转账记录
|
|
|
OrdersPayTransfer exist = ordersPayTransferMapper.selectByBuyerOrderCode(buyerOrderCode);
|
|
|
if (exist != null) {
|
|
|
logger.warn("transferMonErr OrdersPayTransfer has exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(400, "订单已经处理过");
|
|
|
}
|
|
|
|
|
|
OrdersPayTransfer transfer = new OrdersPayTransfer();
|
|
|
transfer.setBuyerOrderCode(buyerOrderCode);
|
|
|
transfer.setSellerOrderCode(sellerOrderCode);
|
|
|
transfer.setAlipayTradeId("");
|
|
|
transfer.setAlipayTradeResult("");
|
|
|
transfer.setTransferType(transferCase.getCode());
|
|
|
transfer.setUid(targetUid);
|
|
|
transfer.setAmount(BigDecimal.ZERO);
|
|
|
transfer.setCreateTime(now);
|
|
|
transfer.setStatus(0);
|
|
|
|
|
|
try {
|
|
|
logger.info("transferMon插入初始化转账信息buyerOrderCode is {}, sellerOrderCode is {}", buyerOrderCode, sellerOrderCode);
|
|
|
ordersPayTransferMapper.insert(transfer);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("transferMonErr insert ordersPayTransfer failed, orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
|
|
|
throw new ServiceException(400, "交易记录创建失败");
|
|
|
}
|
|
|
checkTransferExist(buyerOrderCode);
|
|
|
OrdersPayTransfer transfer = createTransfer(buyerOrderCode, sellerOrderCode, transferCase.getCode(), null, targetUid, BigDecimal.ZERO, now);
|
|
|
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
...
|
...
|
@@ -686,31 +731,8 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
int now = (int) (System.currentTimeMillis()/1000);
|
|
|
// 查看是否已经有转账记录
|
|
|
OrdersPayTransfer exist = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
|
|
|
if (exist != null) {
|
|
|
logger.warn("transAllEarnestErr OrdersPayTransfer has exist, orderCode is {}", orderCode);
|
|
|
throw new ServiceException(400, "订单已经处理过");
|
|
|
}
|
|
|
|
|
|
OrdersPayTransfer transfer = new OrdersPayTransfer();
|
|
|
transfer.setBuyerOrderCode(orderCode);
|
|
|
transfer.setSellerOrderCode(orderCode);
|
|
|
transfer.setAlipayTradeId("");
|
|
|
transfer.setAlipayTradeResult("");
|
|
|
transfer.setAlipayAccount(alipayAccount);
|
|
|
transfer.setTransferType(6);
|
|
|
transfer.setUid(uid);
|
|
|
transfer.setAmount(amount);
|
|
|
transfer.setCreateTime(now);
|
|
|
transfer.setStatus(0);
|
|
|
|
|
|
try {
|
|
|
logger.info("transAllEarnest插入初始化转账信息orderCode is {}", orderCode);
|
|
|
ordersPayTransferMapper.insert(transfer);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("transAllEarnestErr insert ordersPayTransfer failed, orderCode is {}, msg is {}", orderCode, e.getMessage());
|
|
|
throw new ServiceException(400, "交易记录创建失败");
|
|
|
}
|
|
|
checkTransferExist(orderCode);
|
|
|
OrdersPayTransfer transfer = createTransfer(orderCode, orderCode, 6, alipayAccount, uid, amount, now);
|
|
|
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
...
|
...
|
@@ -986,7 +1008,18 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
if (transfer == null || transfer.getStatus() == 1) {
|
|
|
throw new ServiceException(400, "转账记录已成功转账,流水id=" + tradeBillsId);
|
|
|
}
|
|
|
|
|
|
|
|
|
if(exceedMillion()) {
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), preSuccess, orderCode, account, amount, now);
|
|
|
String resultStr = JSON.toJSONString(mapResult);
|
|
|
jsonObject = JSON.parseObject(resultStr);
|
|
|
if(!StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
throw new ServiceException(500, "转账失败:返回={}" + resultStr);
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
jsonObject = alipayService.transferMoney(Long.toString(orderCode), account.getAlipayId(), account.getAlipayAccount(), amount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("manualDeal 转账失败 , req is {}", req);
|
...
|
...
|
@@ -1007,6 +1040,15 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
} else if(exceedMillion(jsonObject)) {
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), tradeBills, orderCode, account, amount, now);
|
|
|
String resultStr = JSON.toJSONString(mapResult);
|
|
|
jsonObject = JSON.parseObject(resultStr);
|
|
|
if(!StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
throw new ServiceException(500, "转账失败:返回={}" + resultStr);
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
|
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
// 上报
|
...
|
...
|
@@ -1031,6 +1073,81 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
logger.info("manualDeal转账结束, tradeBillsId is {}!", tradeBillsId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private Map<String, String> transferWhenExceedMillion(Integer transferId, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account,
|
|
|
BigDecimal amount, int now) throws Exception {
|
|
|
Map<String, String> mapResult = alipayService.transferMoneyWhenExceedMillion(Long.toString(orderCode),
|
|
|
transferId + "_" + tradeBills.getId(), account.getAlipayId(), account.getAlipayAccount(),
|
|
|
account.getCertName(), amount);
|
|
|
logger.info("manualDeal 转账阿里接口(超过100万)返回 {}", mapResult);
|
|
|
// success to wait
|
|
|
if (StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
// TODO 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
|
|
|
tradeBills.setDealStatus(0);
|
|
|
tradeBills.setTradeStatus(99);
|
|
|
// wait
|
|
|
tradeBillsMapper.updateToOkByPrimaryKey(tradeBills);
|
|
|
}
|
|
|
return mapResult;
|
|
|
}
|
|
|
|
|
|
private boolean exceedMillion(JSONObject jsonObject) {
|
|
|
// {"msg":"Business Failed","code":"40004","sub_msg":"单日最多可转100万元","sub_code":"EXCEED_LIMIT_DM_MAX_AMOUNT"}
|
|
|
if (StringUtils.equals("40004", jsonObject.getString("code"))
|
|
|
&& StringUtils.equals("EXCEED_LIMIT_DM_MAX_AMOUNT", jsonObject.getString("sub_code"))) {
|
|
|
exceedMillion = true;
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
private boolean exceedMillion() {
|
|
|
return exceedMillion;
|
|
|
}
|
|
|
|
|
|
private volatile boolean exceedMillion = false;
|
|
|
|
|
|
private void checkTransferExist(long orderCode) {
|
|
|
// 查看是否已经有转账记录
|
|
|
OrdersPayTransfer exist = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
|
|
|
if (exist != null) {
|
|
|
logger.warn("OrdersPayTransfer has exist, orderCode is {}", orderCode);
|
|
|
throw new ServiceException(400, "订单已经处理过");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private OrdersPayTransfer createTransfer(long buyerOrderCode, long sellerOrderCode, Integer transferType, String alipayAccount,
|
|
|
Integer uid, BigDecimal amount, int time) {
|
|
|
OrdersPayTransfer transfer = new OrdersPayTransfer();
|
|
|
transfer.setBuyerOrderCode(buyerOrderCode);
|
|
|
transfer.setSellerOrderCode(sellerOrderCode);
|
|
|
transfer.setAlipayTradeId("");
|
|
|
transfer.setAlipayTradeResult("");
|
|
|
transfer.setAlipayAccount(alipayAccount);
|
|
|
transfer.setTransferType(transferType);
|
|
|
transfer.setUid(uid);
|
|
|
transfer.setAmount(amount);
|
|
|
transfer.setCreateTime(time);
|
|
|
transfer.setStatus(0);
|
|
|
|
|
|
try {
|
|
|
logger.info("转账 插入初始化转账信息 buyerOrderCode is {},sellerOrderCode is {}", buyerOrderCode, sellerOrderCode);
|
|
|
ordersPayTransferMapper.insert(transfer);
|
|
|
return transfer;
|
|
|
} catch (Exception e) {
|
|
|
logger.warn(
|
|
|
"transferCreateErr insert ordersPayTransfer failed, buyerOrderCode is {},sellerOrderCode is {}, msg is {}",
|
|
|
buyerOrderCode, sellerOrderCode, e.getMessage());
|
|
|
throw new ServiceException(400, "交易记录创建失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public PayQueryBo queryTransferResult(String buyerOrderCode) {
|
|
|
return alipayService.transferQuery(buyerOrderCode);
|
...
|
...
|
|