...
|
...
|
@@ -44,7 +44,7 @@ import com.yohoufo.order.model.OrderInfo; |
|
|
import com.yohoufo.order.model.PayQueryBo;
|
|
|
import com.yohoufo.order.model.PayRefundBo;
|
|
|
import com.yohoufo.order.model.PaymentData;
|
|
|
import com.yohoufo.order.model.request.ManualTransferMoneyRequest;
|
|
|
import com.yohoufo.order.model.request.ManualDealRequest;
|
|
|
import com.yohoufo.order.model.request.PaymentRequest;
|
|
|
import com.yohoufo.order.model.request.TransferMoneyRequest;
|
|
|
import com.yohoufo.order.model.response.PaymentConfirmRsp;
|
...
|
...
|
@@ -528,6 +528,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
logger.warn("transferMonErr transferMon计算费用结果为 null, 不合法的金额");
|
|
|
record.setTradeStatus(202);
|
|
|
addTradeBills(record);
|
|
|
// TODO alarm
|
|
|
throw new ServiceException(400, "计算金额错误!:");
|
|
|
}
|
|
|
transfer.setAmount(transferAmount);
|
...
|
...
|
@@ -538,6 +539,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
logger.warn("transferMonErr transferMon计算费用结果为 {}, 小于0.1", transferAmount);
|
|
|
record.setTradeStatus(202);
|
|
|
addTradeBills(record);
|
|
|
// TODO alarm
|
|
|
throw new ServiceException(400, "不合法的金额:" + transferAmount);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -548,7 +550,7 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
if (jsonObject == null) {
|
|
|
logger.warn("transferMonErr 转账失败 , orderCode is {}", buyerOrderCode);
|
|
|
transfer.setStatus(3);
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
throw new ServiceException(500, "转账失败:阿里接口返回null");
|
|
|
}
|
|
|
transfer.setAlipayTradeResult(jsonObject.toJSONString());
|
|
|
// 成功
|
...
|
...
|
@@ -563,12 +565,17 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
transfer.setStatus(1);
|
|
|
} else {
|
|
|
logger.warn("transferMonErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
throw new ServiceException(500, "转账失败:code或者order_id不是成功状态");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
transfer.setStatus(3);
|
|
|
logger.warn("transferMonErr 转账失败 , orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
transfer.setStatus(3);
|
|
|
// TODO alarm
|
|
|
if(e instanceof ServiceException) {
|
|
|
throw new ServiceException(((ServiceException) e).getCode(), e.getMessage());
|
|
|
} else {
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
}
|
|
|
} finally {
|
|
|
logger.info("transferMon最后更新状态 status= {}", transfer.getStatus());
|
|
|
transfer.setUpdateTime(now);
|
...
|
...
|
@@ -581,55 +588,91 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public synchronized void manualTransfer(ManualTransferMoneyRequest req) {
|
|
|
// 转账
|
|
|
logger.info("manualTransfer开始调用阿里接口参数req={}", req);
|
|
|
long orderCode = req.getBuyerOrderCode();
|
|
|
if (orderCode == 0) {
|
|
|
orderCode = req.getSellerOrderCode();
|
|
|
@Database(ForceMaster = true)
|
|
|
public void manualDeal(ManualDealRequest req) {
|
|
|
logger.info("manualDeal人工打款开始参数req={}", req);
|
|
|
Integer tradeBillsId = req.getTradeBillsId();
|
|
|
if (tradeBillsId == null || tradeBillsId < 1) {
|
|
|
throw new ServiceException(400, "manualDeal:流水Id不合法");
|
|
|
}
|
|
|
if (orderCode == 0) {
|
|
|
orderCode = System.nanoTime();
|
|
|
if (req.getOperateUid() == null || req.getOperateUid() < 1) {
|
|
|
throw new ServiceException(400, "manualDeal:客服uid不合法");
|
|
|
}
|
|
|
String alipayAccount = req.getAlipayAccount();
|
|
|
if (StringUtils.isBlank(alipayAccount)) {
|
|
|
throw new ServiceException(400, "manualTransfer alipayAccount is null");
|
|
|
throw new ServiceException(400, "manualDeal:支付宝账号不合法");
|
|
|
}
|
|
|
TradeBills tradeBills = tradeBillsMapper.selectByPrimaryKey(tradeBillsId);
|
|
|
if (tradeBills == null) {
|
|
|
throw new ServiceException(400, "manualDeal:流水不存在");
|
|
|
}
|
|
|
if (tradeBills.getTradeStatus() == 100) {
|
|
|
throw new ServiceException(400, "manualDeal:该流水不是失败的");
|
|
|
}
|
|
|
// 查询处理过的
|
|
|
if (tradeBillsMapper.selectByDealRelateId(tradeBillsId) != null) {
|
|
|
throw new ServiceException(400, "manualDeal:该流水已经处理过");
|
|
|
}
|
|
|
long orderCode = tradeBills.getOrderCode();
|
|
|
|
|
|
BigDecimal amount = null;
|
|
|
try {
|
|
|
amount = new BigDecimal(req.getAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
|
|
|
} catch (Exception e) {
|
|
|
throw new ServiceException(400, "manualTransfer amount is invalid");
|
|
|
throw new ServiceException(400, "manualDeal:金额不合法");
|
|
|
}
|
|
|
if (amount.compareTo(new BigDecimal("0.1")) < 0) {
|
|
|
throw new ServiceException(400, "manualTransfer amount less than 0.1");
|
|
|
throw new ServiceException(400, "manualDeal:金额小于0.1");
|
|
|
}
|
|
|
if (tradeBills.getAmount().compareTo(amount) != 0) {
|
|
|
throw new ServiceException(400, "manualDeal:金额有误");
|
|
|
}
|
|
|
|
|
|
int now = (int) (System.currentTimeMillis() / 1000);
|
|
|
|
|
|
ManualTransfer transfer = new ManualTransfer();
|
|
|
transfer.setAlipayAccount(alipayAccount);
|
|
|
transfer.setBuyerOrderCode(req.getBuyerOrderCode());
|
|
|
transfer.setSellerOrderCode(req.getSellerOrderCode());
|
|
|
if (req.getBuyerOrderCode() == 0 && req.getSellerOrderCode() == 0) {
|
|
|
transfer.setBuyerOrderCode(orderCode);
|
|
|
int beforeStatus = tradeBills.getTradeStatus();
|
|
|
// 预标记成功
|
|
|
TradeBills preSuccess = new TradeBills();
|
|
|
preSuccess.setId(tradeBills.getId());
|
|
|
preSuccess.setTradeStatus(100);
|
|
|
preSuccess.setDealTime(now);
|
|
|
preSuccess.setDealStatus(1);
|
|
|
preSuccess.setDealUid(req.getOperateUid());
|
|
|
if (tradeBillsMapper.updateToOkByPrimaryKey(preSuccess) == 0) {
|
|
|
throw new ServiceException(400, "manualDeal:流水已经处理过id=" + tradeBills.getId());
|
|
|
}
|
|
|
|
|
|
// '1:买家uid; 2:卖家uid','1:保证金;2:货款;3:补偿款', '1:用户收入; 2:用户支出'
|
|
|
try {
|
|
|
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;
|
|
|
}
|
|
|
} 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);
|
|
|
}
|
|
|
transfer.setAmount(amount);
|
|
|
transfer.setUid(req.getUid());
|
|
|
transfer.setCreateTime(now);
|
|
|
transfer.setStatus(0);
|
|
|
int transferType = (((req.getSceneId() << 2) | req.getMoneyTo()) << 2) | req.getMoneyType();
|
|
|
transfer.setTransferType(transferType);
|
|
|
manualTransferMapper.insert(transfer);
|
|
|
try {
|
|
|
|
|
|
JSONObject jsonObject = alipayService.transferMoney(Long.toString(orderCode), alipayAccount, amount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("manualTransfer 转账失败 , req is {}", req);
|
|
|
transfer.setStatus(3);
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
logger.warn("manualDeal 转账失败 , req is {}", req);
|
|
|
throw new ServiceException(500, "转账失败:阿里接口返回null");
|
|
|
}
|
|
|
transfer.setAlipayTradeResult(jsonObject.toJSONString());
|
|
|
logger.info("manualDeal 转账阿里接口返回 {}", jsonObject.toJSONString());
|
|
|
// 成功
|
|
|
Integer code = null;
|
|
|
String orderId = null;
|
...
|
...
|
@@ -637,30 +680,43 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
&& (code = jsonObject.getInteger("code")) == 10000
|
|
|
&& jsonObject.containsKey("order_id")
|
|
|
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
|
|
|
logger.info("manualTransfer转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
|
|
|
transfer.setAlipayTradeId(orderId);
|
|
|
transfer.setStatus(1);
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
logger.warn("manualTransferErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
// 上报
|
|
|
throw new ServiceException(500, "转账失败:返回code="+code+",order_id="+orderId);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
transfer.setStatus(3);
|
|
|
logger.warn("manualTransferErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
|
|
|
throw new ServiceException(500, "转账失败id=" + transfer.getId());
|
|
|
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);
|
|
|
} finally {
|
|
|
logger.info("manualTransfer最后更新状态 status= {}", transfer.getStatus());
|
|
|
transfer.setUpdateTime(now);
|
|
|
manualTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
logger.info("manualTransfer最后更新状态完成,转账结束, buyerOrderCode is {}!", orderCode);
|
|
|
logger.info("manualDeal转账结束, tradeBillsId is {}!", tradeBillsId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void addTradeBills(TradeBills record) {
|
|
|
try {
|
|
|
tradeBillsMapper.insert(record);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("PayRecordErr记录交易到数据库出错 err={}, rec = {}", e.getMessage(), record);
|
|
|
// TODO alarm
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|