...
|
...
|
@@ -2,7 +2,6 @@ package com.yohoufo.order.service.impl; |
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.core.dal.datasource.annotation.Database;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
...
|
...
|
@@ -29,12 +28,19 @@ 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;
|
|
|
import com.yohoufo.order.model.response.PaymentConfirmRsp;
|
|
|
import com.yohoufo.order.model.response.PrepayResponse;
|
|
|
import com.yohoufo.order.service.*;
|
|
|
import com.yohoufo.order.mq.TopicConstants;
|
|
|
import com.yohoufo.order.mq.producer.TradeMqSender;
|
|
|
import com.yohoufo.order.service.AbstractOrderPaymentService;
|
|
|
import com.yohoufo.order.service.BuyerOrderPaymentService;
|
|
|
import com.yohoufo.order.service.IPaymentService;
|
|
|
import com.yohoufo.order.service.MerchantOrderPaymentService;
|
|
|
import com.yohoufo.order.service.SellerOrderPaymentService;
|
|
|
import com.yohoufo.order.service.handler.BuyerOrderPayDiffTimeHandler;
|
|
|
import com.yohoufo.order.service.handler.transfer.AlipayTransferChancelSelector;
|
|
|
import com.yohoufo.order.service.pay.AbstractPayService;
|
|
|
import com.yohoufo.order.service.pay.alipay.AlipayOuyinService;
|
|
|
import com.yohoufo.order.service.pay.wallet.WalletPayService;
|
...
|
...
|
@@ -53,12 +59,12 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
import java.util.function.BiFunction;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
import static com.yohoufo.order.common.TransferCase.EARNEST_MONEY_TO_BUYER;
|
...
|
...
|
@@ -143,11 +149,14 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
BuyerOrderPayDiffTimeHandler buyerOrderPayDiffTimeHandler;
|
|
|
|
|
|
@Autowired
|
|
|
private OrdersPayHbfqMapper ordersPayHbfqDao;
|
|
|
private AlipayTransferChancelSelector alipayTransferChancelSelector;
|
|
|
|
|
|
@Autowired
|
|
|
private TradeBillsService tradeBillsService;
|
|
|
|
|
|
@Autowired
|
|
|
private TradeMqSender tradeMqSender;
|
|
|
|
|
|
/**
|
|
|
* 获取主场的订单service
|
|
|
* @param codeMeta
|
...
|
...
|
@@ -322,8 +331,6 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
// 记录支付成功记录
|
|
|
saveOrdersPay(paymentData, codeMeta.getType(), orderInfo);
|
|
|
|
|
|
|
|
|
abstractOrderService.processAfterPay(orderInfo);
|
|
|
logger.info("paySuccess finished. orderCode is {}", orderCode);
|
|
|
}
|
...
|
...
|
@@ -575,195 +582,97 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
*/
|
|
|
@Database(ForceMaster = true)
|
|
|
public void transferMon(TransferMoneyRequest request){
|
|
|
logger.info("transferMon转账开始,request = {}", request);
|
|
|
long buyerOrderCode = request.getBuyerOrderCode();
|
|
|
logger.info("transferMon参数检查");
|
|
|
// 订单号check
|
|
|
if (buyerOrderCode < 1L) {
|
|
|
logger.warn("transferMonErr orderCode empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
// 类型检查
|
|
|
TransferCase transferCase = TransferCase.getTransferCase(request.getType());
|
|
|
if (transferCase == null) {
|
|
|
logger.warn("transferMonErr transferType invalid");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
TransferMoneyModel transferMoneyModel = new TransferMoneyModel(request).invoke();
|
|
|
String logTag = transferMoneyModel.getLogTag();
|
|
|
long buyerOrderCode = transferMoneyModel.getBuyerOrderCode();
|
|
|
TransferCase transferCase = transferMoneyModel.getTransferCase();
|
|
|
SellerOrder sellerOrder = transferMoneyModel.getSellerOrder();
|
|
|
long sellerOrderCode = transferMoneyModel.getSellerOrderCode();
|
|
|
Integer targetUid = transferMoneyModel.getTargetUid();
|
|
|
int now = transferMoneyModel.getNow();
|
|
|
|
|
|
// 买家订单检查
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(buyerOrderCode);
|
|
|
if (buyerOrder == null) {
|
|
|
logger.warn("transferMonErr getOrderInfo buyer order not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrderCode);
|
|
|
if (buyerOrderGoods == null) {
|
|
|
logger.warn("transferMonErr buyerOrderGoods not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 卖家订单检查
|
|
|
Integer skup = buyerOrderGoods.getSkup();
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(skup);
|
|
|
if (sellerOrder == null){
|
|
|
logger.warn("transferMonErr getOrderInfo seller order not exist, skup is {}", buyerOrderGoods.getSkup());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
Integer sellerUid = sellerOrder.getUid();
|
|
|
long sellerOrderCode = sellerOrder.getOrderCode();
|
|
|
penaltyEarnestIfWalletSellerOrder(request.getPenaltyEarnestFunction(), request.getSwdType(), sellerOrder);
|
|
|
val targetUserType = transferCase == EARNEST_MONEY_TO_BUYER ? TargetUserType.buyer : TargetUserType.seller;
|
|
|
val targetUid = targetUserType == TargetUserType.buyer ? buyerOrder.getUid() : sellerUid;
|
|
|
|
|
|
if (targetUid == null || targetUid < 1) {
|
|
|
logger.warn("transferMonErr uid {} 不合法", targetUid);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]不合法");
|
|
|
}
|
|
|
int now = (int) (System.currentTimeMillis()/1000);
|
|
|
// 查看是否已经有转账记录
|
|
|
checkTransferExist(buyerOrderCode);
|
|
|
|
|
|
OrdersPayTransfer transfer = createTransfer(buyerOrderCode, sellerOrderCode, transferCase.getCode(), null, targetUid, BigDecimal.ZERO, now);
|
|
|
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
|
|
record.setUid(targetUid);
|
|
|
record.setOrderCode(buyerOrderCode);
|
|
|
record.setUserType(targetUserType.getCode());
|
|
|
record.setPayType(1);
|
|
|
// 如果[全部货款->卖家],否则[补偿款->卖家或买家] 2:货款;3:补偿款
|
|
|
record.setTradeType(transferCase == TransferCase.ALL_GOODS_MONEY_TO_SELLER ? 2 : 3);
|
|
|
record.setIncomeOutcome(1);
|
|
|
record.setTradeStatus(0);
|
|
|
record.setCreateTime(now);
|
|
|
TradeBills record = transferMoneyModel.newTradeBills();
|
|
|
|
|
|
logger.info("transferMon参数检查成功!插入初始化转账信息成功,接下来计算费用");
|
|
|
TranseferCellNode transeferCellNode = request.getTranseferCellNode();
|
|
|
// 算费
|
|
|
BigDecimal transferAmount = transeferCellNode.getAmount();
|
|
|
logger.info("transferMon计算费用结果为 {}", transferAmount);
|
|
|
if (transferAmount == null) {
|
|
|
logger.warn("transferMonErr transferMon计算费用结果为 null, 不合法的金额");
|
|
|
alarm("转账金额不合法", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为null");
|
|
|
record.setTradeStatus(BillTradeStatus.AMOUNT_IS_ILLEGAL.getCode());
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "计算金额错误!:");
|
|
|
}
|
|
|
transfer.setAmount(transferAmount);
|
|
|
// 转账费
|
|
|
BigDecimal transferAmount = request.getTranseferCellNode().getAmount();
|
|
|
|
|
|
if (transferAmount == null) {
|
|
|
logger.warn("{}, transfer amount is null", logTag);
|
|
|
alarm("转账金额不合法", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为空");
|
|
|
record.setTradeStatus(BillTradeStatus.AMOUNT_IS_ILLEGAL.getCode());
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "计算金额错误!:");
|
|
|
}
|
|
|
logger.info("{}, transfer amount is {}", logTag, transferAmount);
|
|
|
record.setAmount(transferAmount);
|
|
|
// 因为转账支付宝扣费累计计算,使用转账额
|
|
|
// ???因为转账支付宝扣费累计计算,使用转账额
|
|
|
record.setSystemAmount(transferAmount.multiply(new BigDecimal("-1")));
|
|
|
if (transferAmount.compareTo(new BigDecimal("0.1")) < 0) {
|
|
|
logger.warn("transferMonErr transferMon计算费用结果为 {}, 小于0.1", transferAmount);
|
|
|
logger.info("{}, transfer amount is {}, it less than 0.1", logTag, transferAmount);
|
|
|
alarm("转账金额小于0.1", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为" + transferAmount);
|
|
|
record.setTradeStatus(BillTradeStatus.AMOUNT_IS_ILLEGAL.getCode());
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "不合法的金额:" + transferAmount);
|
|
|
}
|
|
|
|
|
|
AuthorizeResultRespVO account = getOneValidAlipayAccount(targetUid);
|
|
|
if (account == null ) {
|
|
|
logger.warn("transferMonErr uid {} 没有获取到有效的支付宝账号", targetUid);
|
|
|
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALI_ACCOUNT.getCode());
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]没有获取到有效的支付宝账号");
|
|
|
}
|
|
|
if(StringUtils.isNotBlank(account.getAlipayId())) {
|
|
|
logger.info("transferMon uid {} 支付宝账号uid有值优先使用{}", targetUid, account.getAlipayId());
|
|
|
transfer.setAlipayAccount(account.getAlipayId());
|
|
|
} else if(StringUtils.isNotBlank(account.getAlipayAccount())) {
|
|
|
logger.info("transferMon uid {} 支付宝账号uid无值使用账号{}", targetUid, account.getAlipayAccount());
|
|
|
transfer.setAlipayAccount(account.getAlipayAccount());
|
|
|
}
|
|
|
transfer.setUpdateTime(now);
|
|
|
tradeBillsMapper.insert(record);
|
|
|
AuthorizeResultRespVO account = getOneValidAlipayAccountOr(targetUid, invalidAccount -> {
|
|
|
if ("invalid".equals(invalidAccount)) {
|
|
|
record.setTradeStatus(BillTradeStatus.YOHO_STORE_AMOUNT_WAIT_PAYMENT.getCode());
|
|
|
} else if ("invalid_hk".equals(invalidAccount)) {
|
|
|
record.setTradeStatus(BillTradeStatus.HK_AMOUNT_WAIT_PAYMENT.getCode());
|
|
|
tradeMqSender.send(TopicConstants.HK_AMOUNT_WAIT_PAYMENT, Collections.singletonMap("uid", record.getUid()));
|
|
|
} else {
|
|
|
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALIPAY_ACCOUNT.getCode());
|
|
|
}
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]没有获取到有效的支付宝账号");
|
|
|
});
|
|
|
|
|
|
tradeBillsMapper.insert(record);
|
|
|
|
|
|
String alipayAccount = StringUtils.isNotBlank(account.getAlipayId()) ? account.getAlipayId() : account.getAlipayAccount();
|
|
|
transfer.setAlipayAccount(alipayAccount);
|
|
|
transfer.setUpdateTime(now);
|
|
|
transfer.setAmount(transferAmount);
|
|
|
|
|
|
|
|
|
String alipayAccount = transfer.getAlipayAccount();
|
|
|
JSONObject jsonObject = null;
|
|
|
boolean exceedMillionAndSuccess = false;
|
|
|
// 转账
|
|
|
try {
|
|
|
logger.info("transferMon开始调用阿里接口参数buyerOrderCode={}, alipayAccount={}, transferAmount={}", buyerOrderCode, alipayAccount, transferAmount);
|
|
|
|
|
|
if(exceedMillion()) {
|
|
|
transfer.setInterfaceType(2);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), record, buyerOrderCode, account, transferAmount, now);
|
|
|
String resultStr = JSON.toJSONString(mapResult);
|
|
|
jsonObject = JSON.parseObject(resultStr);
|
|
|
if(!StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
throw new ServiceException(500, "转账失败:返回={}" + resultStr);
|
|
|
} else {
|
|
|
exceedMillionAndSuccess = true;
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
transfer.setInterfaceType(1);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
jsonObject = alipayService.transferMoney(Long.toString(buyerOrderCode), account.getAlipayId(), account.getAlipayAccount(), transferAmount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("transferMonErr 转账失败 , orderCode is {}", buyerOrderCode);
|
|
|
transfer.setStatus(3);
|
|
|
throw new ServiceException(500, "转账失败:阿里接口返回null");
|
|
|
}
|
|
|
transfer.setAlipayTradeResult(jsonObject.toJSONString());
|
|
|
// 成功
|
|
|
Integer code = null;
|
|
|
String orderId = null;
|
|
|
if (jsonObject.containsKey("code")
|
|
|
&& (code = jsonObject.getInteger("code")) == 10000
|
|
|
&& jsonObject.containsKey("order_id")
|
|
|
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
|
|
|
logger.info("转账成功,targeAccount is {}, amount is {}", alipayAccount, transferAmount);
|
|
|
transfer.setAlipayTradeId(orderId);
|
|
|
transfer.setStatus(1);
|
|
|
} else if(exceedMillion(jsonObject)) {
|
|
|
transfer.setInterfaceType(2);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), record, buyerOrderCode, account, transferAmount, now);
|
|
|
String resultStr = JSON.toJSONString(mapResult);
|
|
|
jsonObject = JSON.parseObject(resultStr);
|
|
|
if(!StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
throw new ServiceException(500, "转账失败:返回={}" + resultStr);
|
|
|
} else {
|
|
|
exceedMillionAndSuccess = true;
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
|
logger.warn("transferMonErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
throw new ServiceException(500, "转账失败:code或者order_id不是成功状态");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("transferMonErr 转账失败 , orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
|
|
|
String alarmMsg = "订单号:" + buyerOrderCode + ",操作类型:" + transferCase.getInfo() + ",msg=" + e.getMessage();
|
|
|
if (jsonObject != null) {
|
|
|
alarmMsg += ",阿里返回DETAIL=" + jsonObject.toJSONString();
|
|
|
}
|
|
|
logger.info("transferMonErr 转账失败 , alarmMsg is {}", alarmMsg);
|
|
|
alarm("转账失败", "ufo.order.transferMon", alarmMsg);
|
|
|
transfer.setStatus(3);
|
|
|
if(e instanceof ServiceException) {
|
|
|
throw new ServiceException(((ServiceException) e).getCode(), e.getMessage());
|
|
|
} else {
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
}
|
|
|
} finally {
|
|
|
if(exceedMillionAndSuccess) {
|
|
|
logger.info("transferMon exceedMillion,转账结束,等待回调 buyerOrderCode is {}!", buyerOrderCode);
|
|
|
return;
|
|
|
}
|
|
|
logger.info("transferMon最后更新状态 status= {}", transfer.getStatus());
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
// 100:成功;201:没有支付宝账号;202:金额不合法;299:转账失败
|
|
|
BillTradeStatus bts = transfer.getStatus() == 1 ? BillTradeStatus.SUCCESS : BillTradeStatus.TRANSFER_FAIL;
|
|
|
record.setTradeStatus(bts.getCode());
|
|
|
// 转账
|
|
|
try {
|
|
|
logger.info("{}, transfer alipayAccount={}, transferAmount={}", logTag, alipayAccount, transferAmount);
|
|
|
if (alipayTransferChancelSelector.isTransferWithAlipayExceedMillionTransfer()) {
|
|
|
transfer.setInterfaceType(OrdersPayTransfer.INTERFACE_TYPE_TRANSFER_WHEN_EXCEED_MILLION);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
transferWithAlipayExceedMillionTransfer(logTag, record, buyerOrderCode, account, transferAmount, transfer);
|
|
|
} else {
|
|
|
transfer.setInterfaceType(OrdersPayTransfer.INTERFACE_TYPE_TRANSFER_NON_EXCEED_MILLION);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
transferWithAlipayTransfer(logTag, record, buyerOrderCode, account, transferAmount, transfer);
|
|
|
}
|
|
|
logger.info("{}, transfer success ", logTag);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("{}, transfer fail", logTag, e);
|
|
|
String alarmMsg = "订单号:" + buyerOrderCode + ",操作类型:" + transferCase.getInfo() + ",msg=" + e.getMessage();
|
|
|
alarm("转账失败", "ufo.order.transferMon", alarmMsg);
|
|
|
transfer.setStatus(3);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
logger.warn("{}, transfer fail orders pay transfer status is updated", logTag);
|
|
|
record.setTradeStatus(BillTradeStatus.TRANSFER_FAIL.getCode());
|
|
|
tradeBillsMapper.updateSelectiveByPrimaryKey(record);
|
|
|
logger.info("transferMon最后更新状态完成,转账结束, buyerOrderCode is {}!", buyerOrderCode);
|
|
|
}
|
|
|
logger.warn("{}, transfer fail trade bills status is updated", logTag);
|
|
|
if (e instanceof ServiceException) {
|
|
|
throw e;
|
|
|
} else {
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Database(ForceMaster = true)
|
|
|
@Database(ForceMaster = true)
|
|
|
public boolean transAllEarnest(long orderCode, Integer uid, BigDecimal amount, AuthorizeResultRespVO aliPayAccount){
|
|
|
logger.info("退还商家所有保证金,转账开始,orderCode = {}, uid={}, amount={}, alipayAccount={}", orderCode, uid, amount, aliPayAccount);
|
|
|
|
...
|
...
|
@@ -802,7 +711,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
if (StringUtils.isBlank(aliPayAccount.getAlipayAccount()) && StringUtils.isBlank(aliPayAccount.getAlipayId()) ) {
|
|
|
logger.warn("transAllEarnestErr uid {} 支付宝账号不合法", account);
|
|
|
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALI_ACCOUNT.getCode());
|
|
|
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALIPAY_ACCOUNT.getCode());
|
|
|
addTradeBills(record);
|
|
|
throw new ServiceException(400, "uid[" + uid + "]支付宝账号不合法");
|
|
|
}
|
...
|
...
|
@@ -816,7 +725,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
try {
|
|
|
logger.info("transAllEarnest开始调用阿里接口参数buyerOrderCode={}, alipayAccount={}, transferAmount={}", orderCode, account, amount);
|
|
|
|
|
|
if(exceedMillion()) {
|
|
|
if(alipayTransferChancelSelector.isTransferWithAlipayExceedMillionTransfer()) {
|
|
|
transfer.setInterfaceType(2);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), record, orderCode, aliPayAccount, amount, now);
|
...
|
...
|
@@ -850,7 +759,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
transfer.setAlipayTradeId(orderId);
|
|
|
transfer.setStatus(1);
|
|
|
return true;
|
|
|
} else if(exceedMillion(jsonObject)) {
|
|
|
} else if(alipayTransferChancelSelector.isExceedMillion(jsonObject)) {
|
|
|
transfer.setInterfaceType(2);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
Map<String, String> mapResult = transferWhenExceedMillion(transfer.getId(), record, orderCode, aliPayAccount, amount, now);
|
...
|
...
|
@@ -985,7 +894,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
TradeBills lockKey = new TradeBills();
|
|
|
lockKey.setId(tradeBills.getId());
|
|
|
lockKey.setDealTime((int) (System.currentTimeMillis() / 1000));
|
|
|
lockKey.setDealTime(DateUtil.getCurrentTimeSecond());
|
|
|
lockKey.setDealStatus(1);
|
|
|
lockKey.setDealUid(req.getOperateUid());
|
|
|
lockKey.setDealUserName(req.getOperateUname());
|
...
|
...
|
@@ -1147,10 +1056,10 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
400, "转账记录已成功转账,请不要重复操作。");
|
|
|
Integer interfaceType = transfer.getInterfaceType();
|
|
|
logger.info("{}, transfer channel router {}", logTag, interfaceType);
|
|
|
if (OrdersPayTransfer.INTERFACE_TYPE_TRANSFER_WHEN_EXCEED_MILLION.equals(interfaceType)) {
|
|
|
transferWithAlipayExceedMillionTransfer(tradeBills, orderCode, logTag, account, amount, transfer);
|
|
|
if (alipayTransferChancelSelector.isTransferWithAlipayExceedMillionTransfer(interfaceType)) {
|
|
|
transferWithAlipayExceedMillionTransfer(logTag, tradeBills, orderCode, account, amount, transfer);
|
|
|
} else {
|
|
|
transferWithAlipayTransfer(logTag, tradeBills, orderCode, account, amount, transfer);
|
|
|
transferWithAlipayTransferAndAddSuccessTradeBills(logTag, tradeBills, orderCode, account, amount, transfer);
|
|
|
}
|
|
|
logger.info("{}, transfer success.", logTag);
|
|
|
} catch (Exception e) {
|
...
|
...
|
@@ -1188,7 +1097,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
addTradeBills(tradeBills);
|
|
|
}
|
|
|
|
|
|
private void transferWithAlipayTransfer(String logTag, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
private void transferWithAlipayTransferAndAddSuccessTradeBills(String logTag, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
TransferResult transferResult = alipayService.newAlipayTransfer()
|
|
|
.transferOrderCode(Long.toString(orderCode))
|
|
|
.alipayUid(account.getAlipayId())
|
...
|
...
|
@@ -1213,7 +1122,32 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
}
|
|
|
|
|
|
private void transferWithAlipayExceedMillionTransfer(TradeBills tradeBills, long orderCode, String logTag, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
private void transferWithAlipayTransfer(String logTag, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
TransferResult transferResult = alipayService.newAlipayTransfer()
|
|
|
.transferOrderCode(Long.toString(orderCode))
|
|
|
.alipayUid(account.getAlipayId())
|
|
|
.alipayAccount(account.getAlipayAccount())
|
|
|
.transferAmount(amount)
|
|
|
.transfer();
|
|
|
if (transferResult.getCode() == 200) {
|
|
|
logger.info("{}, transfer success and out trade no is {}", logTag, orderCode);
|
|
|
// 更新流水
|
|
|
tradeBills.setTradeStatus(BillTradeStatus.SUCCESS.getCode());
|
|
|
tradeBillsMapper.updateSelectiveByPrimaryKey(tradeBills);
|
|
|
// ?
|
|
|
OrdersPayTransfer transferSuccess = new OrdersPayTransfer();
|
|
|
transferSuccess.setId(transfer.getId());
|
|
|
transferSuccess.setAlipayTradeId(transferResult.getTradeNo());
|
|
|
transferSuccess.setStatus(1);
|
|
|
transferSuccess.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transferSuccess);
|
|
|
}else {
|
|
|
logger.warn("{}, transfer fail {}", logTag,transferResult);
|
|
|
throwServiceException(transferResult.getCode(), transferResult.getMsg());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void transferWithAlipayExceedMillionTransfer(String logTag, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
String businessId = transfer.getId() + "_" + tradeBills.getId();
|
|
|
TransferResult transferResult = alipayService.newAlipayExceedMillionTransfer()
|
|
|
.transferOrderCode( Long.toString(orderCode))
|
...
|
...
|
@@ -1258,32 +1192,6 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
}
|
|
|
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"))) {
|
|
|
lastTransferDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
logger.info("transferWhenExceedMillion 转账阿里接口返回 {},进入商家转账模式 lastTransferDate={}", jsonObject, lastTransferDate);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private boolean exceedMillion() {
|
|
|
boolean zkValue = configReader.getBoolean("ufo.order.pay.exceedSwitch", false);
|
|
|
logger.info("transferWhenExceedMillion ufo.order.pay.exceedSwitch={}", zkValue);
|
|
|
if (zkValue) {
|
|
|
return true;
|
|
|
}
|
|
|
String nowDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
return StringUtils.equals(lastTransferDate, nowDate);
|
|
|
}
|
|
|
|
|
|
private volatile String lastTransferDate = null;
|
|
|
|
|
|
@Autowired
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
private OrdersPayTransfer checkTransferExist(long orderCode) {
|
|
|
// 查看是否已经有转账记录
|
...
|
...
|
@@ -1325,28 +1233,45 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
public PayQueryBo queryTransferResult(String buyerOrderCode) {
|
|
|
return alipayService.transferQuery(buyerOrderCode);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void addTradeBills(TradeBills record) {
|
|
|
tradeBillsService.addTradeBills(record);
|
|
|
}
|
|
|
|
|
|
private AuthorizeResultRespVO getOneValidAlipayAccount(int targetUid) {
|
|
|
return getOneValidAlipayAccountOr(targetUid, invalidAccount -> {});
|
|
|
}
|
|
|
|
|
|
|
|
|
private AuthorizeResultRespVO getOneValidAlipayAccount(int targetUid) {
|
|
|
private AuthorizeResultRespVO getOneValidAlipayAccountOr(int targetUid, Consumer<String> invalidAccountHandler) {
|
|
|
ApiResponse<AuthorizeResultRespVO> resp = ufoServiceCaller.call("ufo.user.aliPayAccountQuery", ApiResponse.class, targetUid);
|
|
|
AuthorizeResultRespVO account = null;
|
|
|
if (resp != null) {
|
|
|
account = (AuthorizeResultRespVO) resp.getData();
|
|
|
}
|
|
|
if (account == null ||
|
|
|
(StringUtils.isBlank(account.getAlipayAccount()) && StringUtils.isBlank(account.getAlipayId()))
|
|
|
|| StringUtils.equals("invalid", account.getAlipayAccount())
|
|
|
|| StringUtils.equals("invalid", account.getAlipayId())) {
|
|
|
// 账户不存在
|
|
|
if (account == null) {
|
|
|
invalidAccountHandler.accept(null);
|
|
|
return null;
|
|
|
}
|
|
|
// 账户不存在
|
|
|
else if ((StringUtils.isBlank(account.getAlipayAccount()) && StringUtils.isBlank(account.getAlipayId()))) {
|
|
|
invalidAccountHandler.accept(null);
|
|
|
return null;
|
|
|
}
|
|
|
// 无效的支付宝账号
|
|
|
else if (StringUtils.contains(account.getAlipayAccount(), "invalid")) {
|
|
|
invalidAccountHandler.accept(account.getAlipayAccount());
|
|
|
return null;
|
|
|
}
|
|
|
return account;
|
|
|
// 无效的支付宝账号
|
|
|
else if (StringUtils.contains(account.getAlipayId(), "invalid")) {
|
|
|
invalidAccountHandler.accept(account.getAlipayId());
|
|
|
return null;
|
|
|
} else {
|
|
|
return account;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
...
|
...
|
@@ -1483,4 +1408,120 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
}
|
|
|
|
|
|
|
|
|
private class TransferMoneyModel {
|
|
|
private TransferMoneyRequest request;
|
|
|
private long buyerOrderCode;
|
|
|
private String logTag;
|
|
|
private TransferCase transferCase;
|
|
|
private SellerOrder sellerOrder;
|
|
|
private long sellerOrderCode;
|
|
|
private TargetUserType targetUserType;
|
|
|
private Integer targetUid;
|
|
|
private int now;
|
|
|
|
|
|
public TransferMoneyModel(TransferMoneyRequest request) {
|
|
|
this.request = request;
|
|
|
}
|
|
|
|
|
|
public long getBuyerOrderCode() {
|
|
|
return buyerOrderCode;
|
|
|
}
|
|
|
|
|
|
public String getLogTag() {
|
|
|
return logTag;
|
|
|
}
|
|
|
|
|
|
public TransferCase getTransferCase() {
|
|
|
return transferCase;
|
|
|
}
|
|
|
|
|
|
public SellerOrder getSellerOrder() {
|
|
|
return sellerOrder;
|
|
|
}
|
|
|
|
|
|
public long getSellerOrderCode() {
|
|
|
return sellerOrderCode;
|
|
|
}
|
|
|
|
|
|
public TargetUserType getTargetUserType() {
|
|
|
return targetUserType;
|
|
|
}
|
|
|
|
|
|
public Integer getTargetUid() {
|
|
|
return targetUid;
|
|
|
}
|
|
|
|
|
|
public int getNow() {
|
|
|
return now;
|
|
|
}
|
|
|
|
|
|
public TransferMoneyModel invoke() {
|
|
|
logger.info("transferMon转账开始,request = {}", request);
|
|
|
buyerOrderCode = request.getBuyerOrderCode();
|
|
|
logTag = String.format("transfer money orderCode is %s", buyerOrderCode);
|
|
|
logger.info("transferMon参数检查");
|
|
|
// 订单号check
|
|
|
if (buyerOrderCode < 1L) {
|
|
|
logger.warn("transferMonErr orderCode empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
// 类型检查
|
|
|
transferCase = TransferCase.getTransferCase(request.getType());
|
|
|
if (transferCase == null) {
|
|
|
logger.warn("transferMonErr transferType invalid");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 买家订单检查
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(buyerOrderCode);
|
|
|
if (buyerOrder == null) {
|
|
|
logger.warn("transferMonErr getOrderInfo buyer order not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrderCode);
|
|
|
if (buyerOrderGoods == null) {
|
|
|
logger.warn("transferMonErr buyerOrderGoods not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 卖家订单检查
|
|
|
Integer skup = buyerOrderGoods.getSkup();
|
|
|
sellerOrder = sellerOrderMapper.selectBySkup(skup);
|
|
|
if (sellerOrder == null){
|
|
|
logger.warn("transferMonErr getOrderInfo seller order not exist, skup is {}", buyerOrderGoods.getSkup());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
Integer sellerUid = sellerOrder.getUid();
|
|
|
sellerOrderCode = sellerOrder.getOrderCode();
|
|
|
|
|
|
targetUserType = transferCase == EARNEST_MONEY_TO_BUYER ? TargetUserType.buyer : TargetUserType.seller;
|
|
|
targetUid = targetUserType == TargetUserType.buyer ? buyerOrder.getUid() : sellerUid;
|
|
|
|
|
|
if (targetUid == null || targetUid < 1) {
|
|
|
logger.warn("transferMonErr uid {} 不合法", targetUid);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]不合法");
|
|
|
}
|
|
|
now = (int) (System.currentTimeMillis() / 1000);
|
|
|
// 查看是否已经有转账记录
|
|
|
checkTransferExist(buyerOrderCode);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
|
|
|
public TradeBills newTradeBills(){
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
|
|
record.setUid(targetUid);
|
|
|
record.setOrderCode(buyerOrderCode);
|
|
|
record.setUserType(targetUserType.getCode());
|
|
|
record.setPayType(1);
|
|
|
// 如果[全部货款->卖家],否则[补偿款->卖家或买家] 2:货款;3:补偿款
|
|
|
record.setTradeType(transferCase == TransferCase.ALL_GOODS_MONEY_TO_SELLER ? 2 : 3);
|
|
|
record.setIncomeOutcome(1);
|
|
|
record.setTradeStatus(BillTradeStatus.NEW.getCode());
|
|
|
record.setCreateTime(now);
|
|
|
return record;
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|