...
|
...
|
@@ -40,13 +40,16 @@ import com.yohoufo.order.service.handler.BuyerCancelCompensateComputeHandler; |
|
|
import com.yohoufo.order.service.handler.BuyerOrderPayDiffTimeHandler;
|
|
|
import com.yohoufo.order.service.pay.AbstractPayService;
|
|
|
import com.yohoufo.order.service.pay.alipay.AlipayOuyinService;
|
|
|
import com.yohoufo.order.service.pay.alipay.AlipayServiceAbstract;
|
|
|
import com.yohoufo.order.service.pay.wallet.WalletPayService;
|
|
|
import com.yohoufo.order.service.pay.weixin.WeixinMiniappPayService;
|
|
|
import com.yohoufo.order.service.pay.weixin.WeixinPayUFORealAppService;
|
|
|
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
|
|
|
import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
|
|
|
import com.yohoufo.order.service.transfer.TransferResult;
|
|
|
import com.yohoufo.order.utils.LoggerUtils;
|
|
|
import com.yohoufo.order.utils.PaymentHelper;
|
|
|
import com.yohoufo.order.utils.ServiceExceptions;
|
|
|
import lombok.val;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
...
|
...
|
@@ -1001,6 +1004,8 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
if (tradeBillsMapper.selectByDealRelateId(tradeBillsId) != null) {
|
|
|
throw new ServiceException(400, "manualDeal:该流水已经处理过");
|
|
|
}
|
|
|
long orderCode = tradeBills.getOrderCode();
|
|
|
String logTag = "manualDeal orderCode is " + orderCode + "tradeBillId is " + tradeBillsId;
|
|
|
AuthorizeResultRespVO account = getAlipayAccount(tradeBills.getUid());
|
|
|
if (account == null ||
|
|
|
(StringUtils.isBlank(account.getAlipayAccount()) && StringUtils.isBlank(account.getAlipayId()))) {
|
...
|
...
|
@@ -1015,7 +1020,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
logger.info("manualDeal uid {} 支付宝账号uid无值使用账号{}", tradeBills.getUid(), account.getAlipayAccount());
|
|
|
alipayAccount = account.getAlipayAccount();
|
|
|
}
|
|
|
long orderCode = tradeBills.getOrderCode();
|
|
|
|
|
|
|
|
|
BigDecimal amount = null;
|
|
|
try {
|
...
|
...
|
@@ -1098,104 +1103,112 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
throw new ServiceException(500, "退款失败id=" + tradeBillsId);
|
|
|
}
|
|
|
}
|
|
|
JSONObject jsonObject = null;
|
|
|
Integer interfaceType = null;
|
|
|
try {
|
|
|
OrdersPayTransfer transfer = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
|
|
|
if (transfer == null) {
|
|
|
throw new ServiceException(400, "转账记录不存在,流水id=" + tradeBillsId);
|
|
|
}
|
|
|
if (transfer == null || transfer.getStatus() == 1) {
|
|
|
throw new ServiceException(400, "转账记录已成功转账,流水id=" + tradeBillsId);
|
|
|
}
|
|
|
interfaceType = transfer.getInterfaceType();
|
|
|
|
|
|
logger.info("manualDeal orderCode={}, tradeBillId={}, 转账接口类型为 {}", orderCode, tradeBillsId, interfaceType);
|
|
|
if (interfaceType != null && interfaceType == 2) {
|
|
|
logger.info("manualDeal orderCode={}, tradeBillId={}, 转账接口类型为 超过100万的接口", orderCode, tradeBillsId);
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
logger.info("manualDeal orderCode={}, tradeBillId={}, 转账接口类型为 小于100万的接口", orderCode, tradeBillsId);
|
|
|
jsonObject = alipayService.transferMoney(Long.toString(orderCode), account.getAlipayId(), account.getAlipayAccount(), amount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("manualDeal 转账失败 , req is {}", req);
|
|
|
throw new ServiceException(500, "转账失败:阿里接口返回null");
|
|
|
}
|
|
|
logger.info("manualDeal 转账阿里接口返回 {}", 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("manualDeal转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
|
|
|
// 加新流水
|
|
|
tradeBills.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
|
|
|
OrdersPayTransfer transferSuccess = new OrdersPayTransfer();
|
|
|
transferSuccess.setId(transfer.getId());
|
|
|
transferSuccess.setAlipayTradeId(orderId);
|
|
|
transferSuccess.setStatus(1);
|
|
|
transferSuccess.setUpdateTime(now);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transferSuccess);
|
|
|
} else {
|
|
|
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
// 上报
|
|
|
throw new ServiceException(500, "转账失败:返回code="+code+",order_id="+orderId);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
/*if (interfaceType != null && interfaceType == 2) {
|
|
|
return;
|
|
|
}*/
|
|
|
logger.warn("manualDealErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
|
|
|
String alarmMsg = "订单号:" + orderCode + ",操作类型(转账),流水id=" + tradeBillsId + ",msg=" + e.getMessage();
|
|
|
if (jsonObject != null) {
|
|
|
alarmMsg += ",阿里返回DETAIL=" + jsonObject.toJSONString();
|
|
|
}
|
|
|
logger.info("manualDealErr 转账失败 , alarmMsg is {}", alarmMsg);
|
|
|
alarm("人工处理转账失败", "ufo.order.manualDeal", alarmMsg);
|
|
|
preSuccess.setDealStatus(0);
|
|
|
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
|
|
|
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);
|
|
|
}
|
|
|
try {
|
|
|
OrdersPayTransfer transfer = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
|
|
|
ServiceExceptions.throwServiceExceptionIf(() -> Objects.isNull(transfer),
|
|
|
400, "转账记录不存在[" + tradeBillsId + "]");
|
|
|
ServiceExceptions.throwServiceExceptionIf(() -> transfer.getStatus() == 1,
|
|
|
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);
|
|
|
} else {
|
|
|
transferWithAlipayTransfer(logTag, tradeBills, orderCode, account, amount, now, transfer);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("{}, transfer fail", logTag, e);
|
|
|
alarm("人工处理转账失败",
|
|
|
"ufo.order.manualDeal",
|
|
|
"订单号:" + orderCode + ",操作类型(转账),流水id=" + tradeBillsId + ",msg=" + e.getMessage());
|
|
|
logger.info("{}, rollback lock", logTag);
|
|
|
preSuccess.setDealStatus(0);
|
|
|
tradeBillsMapper.updateToFailByPrimaryKey(preSuccess);
|
|
|
logger.info("{}, rollback lock success", logTag);
|
|
|
if (e instanceof ServiceException) {
|
|
|
throw e;
|
|
|
} else {
|
|
|
ServiceExceptions.throwServiceException(500, "转账失败id=" + tradeBillsId);
|
|
|
}
|
|
|
} finally {
|
|
|
logger.info("{}, complete", logTag);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void transferWithAlipayTransfer(String logTag, TradeBills tradeBills, long orderCode, AuthorizeResultRespVO account, BigDecimal amount, int now, 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.setDealRelateId(tradeBills.getId());
|
|
|
tradeBills.setId(null);
|
|
|
tradeBills.setTradeStatus(100);
|
|
|
tradeBills.setCreateTime(now);
|
|
|
addTradeBills(tradeBills);
|
|
|
// ?
|
|
|
OrdersPayTransfer transferSuccess = new OrdersPayTransfer();
|
|
|
transferSuccess.setId(transfer.getId());
|
|
|
transferSuccess.setAlipayTradeId(transferResult.getTradeNo());
|
|
|
transferSuccess.setStatus(1);
|
|
|
transferSuccess.setUpdateTime(now);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transferSuccess);
|
|
|
}else {
|
|
|
logger.warn("{}, transfer fail {}", logTag,transferResult);
|
|
|
throw new ServiceException(transferResult.getCode(), transferResult.getMsg());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void transferWithAlipayExceedMillionTransfer(TradeBills tradeBills, long orderCode, String logTag, AuthorizeResultRespVO account, BigDecimal amount, OrdersPayTransfer transfer) {
|
|
|
String businessId = transfer.getId() + "_" + tradeBills.getId();
|
|
|
TransferResult transferResult = alipayService.newAlipayExceedMillionTransfer()
|
|
|
.transferOrderCode( Long.toString(orderCode))
|
|
|
.alipayUid(account.getAlipayId())
|
|
|
.alipayAccount(account.getAlipayAccount())
|
|
|
.transferAmount(amount)
|
|
|
.businessId(businessId)
|
|
|
.userName(account.getCertName())
|
|
|
.transfer();
|
|
|
// success to wait
|
|
|
if (transferResult.getCode() == 200) {
|
|
|
logger.info("{}, transfer success and out trade no is {}", logTag, businessId);
|
|
|
// 更新流水状态为转账中
|
|
|
tradeBills.setDealStatus(0);
|
|
|
tradeBills.setTradeStatus(TradeBills.Status.TRANSFER_WAITING.getCode());
|
|
|
tradeBillsMapper.updateSelectiveByPrimaryKeyNoCondition(tradeBills);
|
|
|
logger.info("{}, the trade bill has updated to transfer waiting", logTag);
|
|
|
} else {
|
|
|
logger.warn("{}, transfer fail {}", logTag,transferResult);
|
|
|
throw new ServiceException(transferResult.getCode(), transferResult.getMsg());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
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("transferWhenExceedMillion 转账阿里接口(总金额超过100万)返回 {}, orderCode is {}, transferId is {}, tradeBillsId is {}, alibillId is {}", mapResult, orderCode, transferId, tradeBills.getId(), transferId + "_" + tradeBills.getId());
|
|
|
// success to wait
|
|
|
if (StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
tradeBills.setDealStatus(0);
|
|
|
tradeBills.setTradeStatus(TradeBills.Status.TRANSFER_WAITING.getCode());
|
|
|
// wait
|
|
|
tradeBillsMapper.updateSelectiveByPrimaryKeyNoCondition(tradeBills);
|
|
|
if (StringUtils.equals("T", mapResult.get("is_success"))) {
|
|
|
tradeBills.setDealStatus(0);
|
|
|
tradeBills.setTradeStatus(TradeBills.Status.TRANSFER_WAITING.getCode());
|
|
|
// wait
|
|
|
tradeBillsMapper.updateSelectiveByPrimaryKeyNoCondition(tradeBills);
|
|
|
logger.info("transferWhenExceedMillion 转账阿里接口(总金额超过100万)更新之后的tradeBill: {}", tradeBillsMapper.selectByPrimaryKey(tradeBills.getId()));
|
|
|
}
|
|
|
return mapResult;
|
|
|
}
|
|
|
return mapResult;
|
|
|
}
|
|
|
|
|
|
private boolean exceedMillion(JSONObject jsonObject) {
|
|
|
// {"msg":"Business Failed","code":"40004","sub_msg":"单日最多可转100万元","sub_code":"EXCEED_LIMIT_DM_MAX_AMOUNT"}
|
...
|
...
|
|