|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.tools.common.beans.ApiResponse;
|
|
|
import com.yohoufo.common.caller.UfoServiceCaller;
|
|
|
import com.yohoufo.common.utils.TimeUtils;
|
|
|
import com.yohoufo.dal.order.*;
|
|
|
import com.yohoufo.dal.order.model.*;
|
|
|
import com.yohoufo.order.common.*;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
|
|
import com.yohoufo.order.constants.RefundContant;
|
|
|
import com.yohoufo.order.model.*;
|
|
|
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMetaMapper;
|
|
|
import com.yohoufo.dal.order.OrdersPayMapper;
|
|
|
import com.yohoufo.dal.order.OrdersPayRefundMapper;
|
|
|
import com.yohoufo.dal.order.OrdersPayTransferMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderMetaMapper;
|
|
|
import com.yohoufo.dal.order.TradeBillsMapper;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.dal.order.model.OrdersPay;
|
|
|
import com.yohoufo.order.common.*;
|
|
|
import com.yohoufo.dal.order.model.OrdersPayRefund;
|
|
|
import com.yohoufo.dal.order.model.OrdersPayTransfer;
|
|
|
import com.yohoufo.dal.order.model.SellerOrder;
|
|
|
import com.yohoufo.dal.order.model.SellerOrderMeta;
|
|
|
import com.yohoufo.dal.order.model.TradeBills;
|
|
|
import com.yohoufo.order.common.OrderCodeType;
|
|
|
import com.yohoufo.order.common.Payment;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
|
|
import com.yohoufo.order.constants.RefundContant;
|
|
|
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.CheckupRequest;
|
|
|
import com.yohoufo.order.model.request.PaymentRequest;
|
|
|
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.AbstractOrderPaymentService;
|
...
|
...
|
@@ -27,18 +50,10 @@ import com.yohoufo.order.service.IPaymentService; |
|
|
import com.yohoufo.order.service.SellerOrderPaymentService;
|
|
|
import com.yohoufo.order.service.pay.AbstractPayService;
|
|
|
import com.yohoufo.order.service.pay.alipay.AlipayOuyinService;
|
|
|
import com.yohoufo.order.service.pay.weixin.WeixinPayAppService;
|
|
|
import com.yohoufo.order.service.pay.weixin.WeixinPayBlkAppService;
|
|
|
import com.yohoufo.order.service.pay.weixin.WeixinPayUFOAppService;
|
|
|
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
|
|
|
import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import com.yohoufo.user.responseVO.AuthorizeResultRespVO;
|
|
|
|
|
|
|
|
|
@Service
|
...
|
...
|
@@ -78,6 +93,21 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
|
|
|
@Autowired
|
|
|
SellerOrderMapper sellerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderMetaMapper sellerOrderMetaMapper;
|
|
|
|
|
|
@Autowired
|
|
|
OrdersPayTransferMapper ordersPayTransferMapper;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
UfoServiceCaller ufoServiceCaller;
|
|
|
|
|
|
@Autowired
|
|
|
TradeBillsMapper tradeBillsMapper;
|
|
|
|
|
|
/**
|
|
|
* 获取主场的订单service
|
...
|
...
|
@@ -355,89 +385,240 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void transferMon(CheckupRequest request){
|
|
|
|
|
|
// 参数检查
|
|
|
if (request.getOrderCode() < 0
|
|
|
|| request.getCheckupResult() <0){
|
|
|
logger.warn("transferMon request empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 未更新前,买家订单号的状态: 鉴定中
|
|
|
// 买家订单号信息
|
|
|
OrderInfo orderInfo = buyerOrderPaymentService.getOrderInfo(request.getOrderCode());
|
|
|
|
|
|
if (orderInfo == null) {
|
|
|
logger.warn("getOrderInfo order not exist, orderCode is {}", request.getOrderCode());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// TODO 安全性校验:比如对转账的金额限额
|
|
|
// 保证金抽成 货款抽成 都在货款抽成
|
|
|
// 运费买家下单的时候 确定的
|
|
|
|
|
|
// 鉴定通过: 1、保证金退款给卖家(参见退款接口) 2、打货款给卖家
|
|
|
if (request.getCheckupResult() == 1){
|
|
|
|
|
|
// 根据买家订单号,查支付成功记录
|
|
|
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderInfo.getOrderCode(), orderInfo.getUid());
|
|
|
if (ordersPay == null){
|
|
|
|
|
|
}
|
|
|
|
|
|
// 实施转账
|
|
|
|
|
|
}
|
|
|
// 鉴定不通过: 1、保证金赔款给买家
|
|
|
else if(request.getCheckupResult() == 2){
|
|
|
// 检查 保证金是否已经支付
|
|
|
// 1.根据买家订单号 获取 skup
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(orderInfo.getUid(), orderInfo.getOrderCode());
|
|
|
if (buyerOrderGoods == null){
|
|
|
|
|
|
}
|
|
|
|
|
|
// 2.根据skup获取 卖家订单号
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(buyerOrderGoods.getSkup());
|
|
|
if (sellerOrder == null){
|
|
|
|
|
|
}
|
|
|
|
|
|
// 3.根据卖家订单号,获取支付成功结果
|
|
|
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(sellerOrder.getOrderCode(), sellerOrder.getUid());
|
|
|
if (ordersPay == null){
|
|
|
|
|
|
}
|
|
|
|
|
|
// 根据用户uid,获取卖家支付宝账号
|
|
|
// 从meta中获取,取费用系数
|
|
|
String transferOrderCode = String.valueOf(request.getOrderCode()) + ordersPay.getId();
|
|
|
String alipayAccount = "";
|
|
|
// 算费
|
|
|
double transferAmount = 0;
|
|
|
|
|
|
// TODO 下面转账的代码放入到提现接口
|
|
|
// 转账订单号必须唯一,所以一笔订单的一笔支付,只能转账一次
|
|
|
JSONObject jsonObject = alipayService.transferMoney(transferOrderCode, alipayAccount, transferAmount);
|
|
|
if (jsonObject.getInteger("code") == 10000){
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* <pre>
|
|
|
* 1:鉴定通过(货款-->卖家);
|
|
|
* 2:鉴定不通过(保证金 --> 买家);
|
|
|
* 3:有买家时卖家不卖了(保证金 --> 买家)
|
|
|
* </pre>
|
|
|
*
|
|
|
* @param request 请求
|
|
|
*/
|
|
|
public void transferMon(TransferMoneyRequest request){
|
|
|
logger.info("transferMon转账开始,request = {}", request);
|
|
|
|
|
|
long buyerOrderCode = request.getBuyerOrderCode();
|
|
|
long sellerOrderCode = request.getBuyerOrderCode();
|
|
|
int transferType = request.getType();
|
|
|
|
|
|
logger.info("transferMon参数检查");
|
|
|
// 订单号check
|
|
|
if (buyerOrderCode < 1 || sellerOrderCode < 1) {
|
|
|
logger.warn("transferMon orderCode empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
// 类型检查
|
|
|
if (transferType < 1 || transferType > 3) {
|
|
|
logger.warn("transferType invalid");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 买家订单检查
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(buyerOrderCode);
|
|
|
if (buyerOrder == null) {
|
|
|
logger.warn("getOrderInfo buyer order not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 卖家订单检查
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(sellerOrderCode);
|
|
|
if (sellerOrder == null){
|
|
|
logger.warn("getOrderInfo seller order not exist, orderCode is {}", sellerOrderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 记录转账记录到 orders_pay_transfer
|
|
|
|
|
|
|
|
|
// 更新订单状态
|
|
|
|
|
|
|
|
|
|
|
|
// 查看是否已经有转账记录
|
|
|
OrdersPayTransfer exist = ordersPayTransferMapper.selectByBuyerOrderCode(buyerOrderCode);
|
|
|
if (exist != null) {
|
|
|
logger.warn("getOrderInfo order not exist, orderCode is {}", buyerOrderCode);
|
|
|
throw new ServiceException(400, "订单已经处理过");
|
|
|
}
|
|
|
|
|
|
Integer targetUid = transferType == 1 ? sellerOrder.getUid() : buyerOrder.getUid();
|
|
|
|
|
|
if (targetUid == null || targetUid < 1) {
|
|
|
logger.warn("uid {} 不合法", targetUid);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]不合法");
|
|
|
}
|
|
|
String alipayAccount = getAlipayAccount(targetUid);
|
|
|
if (StringUtils.isBlank(alipayAccount)) {
|
|
|
logger.warn("uid {} 没有获取到有效的支付宝账号", targetUid);
|
|
|
throw new ServiceException(400, "uid[" + targetUid + "]没有获取到有效的支付宝账号");
|
|
|
}
|
|
|
logger.info("transferMon参数检查成功!");
|
|
|
|
|
|
int now = (int) (System.currentTimeMillis()/1000);
|
|
|
|
|
|
OrdersPayTransfer transfer = new OrdersPayTransfer();
|
|
|
transfer.setBuyerOrderCode(buyerOrderCode);
|
|
|
transfer.setSellerOrderCode(sellerOrderCode);
|
|
|
transfer.setAlipayTradeId("");
|
|
|
transfer.setAlipayTradeResult("");
|
|
|
transfer.setTransferType(transferType);
|
|
|
transfer.setUid(targetUid);
|
|
|
transfer.setAlipayAccount(alipayAccount);
|
|
|
transfer.setAmount(BigDecimal.ZERO);
|
|
|
transfer.setCreateTime(now);
|
|
|
transfer.setStatus(0);
|
|
|
|
|
|
try {
|
|
|
logger.info("transferMon插入初始化转账信息buyerOrderCode is {}", buyerOrderCode);
|
|
|
ordersPayTransferMapper.insert(transfer);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("insert ordersPayTransfer failed, orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
|
|
|
throw new ServiceException(400, "交易记录创建失败");
|
|
|
}
|
|
|
|
|
|
logger.info("transferMon插入初始化转账信息成功,接下来计算费用");
|
|
|
// 算费
|
|
|
BigDecimal transferAmount = calcTransferAmount(sellerOrder.getUid(), sellerOrder.getSkup(), transferType);
|
|
|
logger.info("transferMon计算费用结果为 {}", transferAmount);
|
|
|
if (transferAmount == null || transferAmount.compareTo(new BigDecimal("0.1")) < 0) {
|
|
|
logger.warn("transferMon计算费用结果为 {}, 不合法的金额", transferAmount);
|
|
|
throw new ServiceException(400, "不合法的金额");
|
|
|
}
|
|
|
transfer.setAmount(transferAmount);
|
|
|
|
|
|
// 转账
|
|
|
try {
|
|
|
logger.info("transferMon开始调用阿里接口参数buyerOrderCode={}, alipayAccount={}, transferAmount={}", buyerOrderCode, alipayAccount, transferAmount);
|
|
|
JSONObject jsonObject = alipayService.transferMoney(Long.toString(buyerOrderCode), alipayAccount, transferAmount);
|
|
|
if (jsonObject == null) {
|
|
|
logger.warn("转账失败 , orderCode is {}", buyerOrderCode);
|
|
|
transfer.setStatus(3);
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
}
|
|
|
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 {
|
|
|
logger.warn("返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
|
|
|
transfer.setStatus(3);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
transfer.setStatus(3);
|
|
|
logger.warn("转账失败 , orderCode is {}, msg is {}", buyerOrderCode, e.getMessage());
|
|
|
throw new ServiceException(500, "转账失败");
|
|
|
} finally {
|
|
|
logger.info("transferMon最后更新状态 status= {}", transfer.getStatus());
|
|
|
transfer.setUpdateTime(now);
|
|
|
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
|
|
|
logger.info("transferMon最后更新状态完成,转账结束, buyerOrderCode is {}!", buyerOrderCode);
|
|
|
}
|
|
|
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
|
|
try {
|
|
|
record.setUid(targetUid);
|
|
|
record.setOrderCode(transferType == 1 ? sellerOrderCode : buyerOrderCode);
|
|
|
record.setUserType(transferType == 1 ? 2 : 1);
|
|
|
record.setPayType(1);
|
|
|
record.setTradeType(transferType == 1 ? 2 : 3);
|
|
|
record.setIncomeOutcome(1);
|
|
|
record.setAmount(transferAmount);
|
|
|
// 因为转账支付宝扣费累计计算,使用转账额
|
|
|
record.setSystemAmount(transferAmount.multiply(new BigDecimal("-1")));
|
|
|
record.setTradeStatus(1);
|
|
|
record.setCreateTime(now);
|
|
|
tradeBillsMapper.insert(record);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("记录交易到数据库出错 err={}, rec = {}", e.getMessage(), record);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 支付保证金流水记录
|
|
|
public void addPayEnsureRecord(Integer uid, Integer skup, Long orderCode, Integer payType, BigDecimal amount) {
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
|
|
try {
|
|
|
record.setUid(uid);
|
|
|
record.setOrderCode(orderCode);
|
|
|
record.setUserType(2);// 1:买家uid; 2:卖家uid
|
|
|
record.setPayType(payType);// 1:支付宝; 2:微信
|
|
|
record.setTradeType(1);//1:保证金;2:货款;3:补偿款
|
|
|
record.setIncomeOutcome(2);// 1:用户收入; 2:用户支出
|
|
|
record.setAmount(amount);
|
|
|
record.setSystemAmount(amount.multiply(new BigDecimal("-1")));// 有货收入
|
|
|
record.setTradeStatus(0);//0:订单未完结;1:订单完结
|
|
|
record.setCreateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
tradeBillsMapper.insert(record);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("记录交易到数据库出错 err={}, rec = {}", e.getMessage(), record);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 支付货款流水记录
|
|
|
public void addPayBuyRecord(Integer uid, Long orderCode, Integer payType, BigDecimal amount) {
|
|
|
// 增加流水记录
|
|
|
TradeBills record = new TradeBills();
|
|
|
try {
|
|
|
record.setUid(uid);
|
|
|
record.setOrderCode(orderCode);
|
|
|
record.setUserType(1);// 1:买家uid; 2:卖家uid
|
|
|
record.setPayType(payType);// 1:支付宝; 2:微信
|
|
|
record.setTradeType(2);//1:保证金;2:货款;3:补偿款
|
|
|
record.setIncomeOutcome(2);// 1:用户收入; 2:用户支出
|
|
|
record.setAmount(amount);
|
|
|
record.setSystemAmount(amount.multiply(new BigDecimal("-1")));// 有货收入
|
|
|
record.setTradeStatus(0);//0:订单未完结;1:订单完结
|
|
|
record.setCreateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
tradeBillsMapper.insert(record);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("记录交易到数据库出错 err={}, rec = {}", e.getMessage(), record);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
private BigDecimal calcTransferAmount(Integer sellerUid, Integer skup, int transferType) {
|
|
|
SellerOrderMeta meta = sellerOrderMetaMapper.selectByMetaKey(sellerUid, skup, "fee");
|
|
|
if (meta == null) {
|
|
|
logger.warn("查询SellerOrderMeta无数据, sellerUid is {}, skup is {}", sellerUid, skup);
|
|
|
throw new ServiceException(500, "计费信息不完整");
|
|
|
}
|
|
|
if (StringUtils.isBlank(meta.getMetaValue())) {
|
|
|
logger.warn("查询SellerOrderMeta的MetaValue为空, sellerUid is {}, skup is {}", sellerUid, skup);
|
|
|
throw new ServiceException(500, "计费信息不完整");
|
|
|
}
|
|
|
try {
|
|
|
JSONObject metavalue = JSON.parseObject(meta.getMetaValue());
|
|
|
// 交易成功的情况
|
|
|
if (transferType == 1) {
|
|
|
return metavalue.getBigDecimal("income");
|
|
|
}
|
|
|
// (1-Y)*A
|
|
|
// 保证金
|
|
|
BigDecimal ensure = metavalue.getBigDecimal("earnestMoney");
|
|
|
// 保证金抽成
|
|
|
BigDecimal ensureYohoRage = metavalue.getJSONObject("serviceFeeRate").getBigDecimal("earnestMoney");
|
|
|
return ensure.multiply(BigDecimal.ONE.subtract(ensureYohoRage));
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("计费信息不完整, sellerUid is {}, skup is {}, err is {}", sellerUid, skup, e.getMessage());
|
|
|
throw new ServiceException(500, "计费信息不完整");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private String getAlipayAccount(int targetUid) {
|
|
|
ApiResponse<AuthorizeResultRespVO> resp = ufoServiceCaller.call("ufo.user.aliPayAccountQuery", ApiResponse.class, targetUid);
|
|
|
if (resp != null) {
|
|
|
AuthorizeResultRespVO data = (AuthorizeResultRespVO) resp.getData();
|
|
|
if (data != null && data.getAlipayAccount() != null) {
|
|
|
return data.getAlipayAccount();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 支付
|
|
|
* @param request
|
|
|
* @return
|
...
|
...
|
|