...
|
...
|
@@ -13,14 +13,17 @@ import com.yohoufo.dal.order.SellerOrderMapper; |
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrderMeta;
|
|
|
import com.yohoufo.dal.order.model.SellerOrder;
|
|
|
import com.yohoufo.order.common.OrderCodeType;
|
|
|
import com.yohoufo.order.common.OrderStatus;
|
|
|
import com.yohoufo.order.common.Payment;
|
|
|
import com.yohoufo.order.common.*;
|
|
|
import com.yohoufo.order.constants.MetaKey;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
|
|
import com.yohoufo.order.model.OrderInfo;
|
|
|
import com.yohoufo.order.request.PaymentRequest;
|
|
|
import com.yohoufo.order.response.PaymentConfirmRsp;
|
|
|
import com.yohoufo.order.response.PrepayResponse;
|
|
|
import com.yohoufo.order.service.AbstractOrderPaymentService;
|
|
|
import com.yohoufo.order.service.BuyerOrderPaymentService;
|
|
|
import com.yohoufo.order.service.IPaymentService;
|
|
|
import com.yohoufo.order.service.SellerOrderPaymentService;
|
|
|
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
|
|
|
import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
...
|
...
|
@@ -39,52 +42,97 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
BuyerOrderMetaMapper buyerOrderMetaMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderMapper sellerOrderMapper;
|
|
|
BuyerOrderPaymentService buyerOrderPaymentService;
|
|
|
|
|
|
@Value("${unionpay.pay.url}")
|
|
|
private String unionPayUrl;
|
|
|
@Autowired
|
|
|
SellerOrderPaymentService sellerOrderPaymentService;
|
|
|
|
|
|
@Value("${unionpay.pay.callbackUrl}")
|
|
|
private String callbackUrl;
|
|
|
|
|
|
@Value("${unionpay.pay.msgSrc}")
|
|
|
private String msgSrc;
|
|
|
@Autowired
|
|
|
OrderCodeGenerator orderCodeGenerator;
|
|
|
|
|
|
@Value("${unionpay.pay.mid}")
|
|
|
private String mid;
|
|
|
|
|
|
@Value("${unionpay.pay.tid}")
|
|
|
private String tid;
|
|
|
/**
|
|
|
* 获取主场的订单service
|
|
|
* @param codeMeta
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
private AbstractOrderPaymentService getOrderPaymentService(CodeMeta codeMeta, PaymentRequest request){
|
|
|
|
|
|
@Value("${unionPay.pay.signKey}")
|
|
|
private String signKey;
|
|
|
if (!OrderCodeType.isExistOrderCodeType(codeMeta.getType())) {
|
|
|
logger.warn("payment orderCode invalidate {}, uid is {}, codeMeta is {}",
|
|
|
request.getOrderCode(), request.getUid(), codeMeta.getType());
|
|
|
throw new ServiceException(ServiceError.ORDER_ORDER_CODE_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
@Autowired
|
|
|
HttpClient httpClient;
|
|
|
AbstractOrderPaymentService paymentService = null;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMetaMapper buyerOrderMetaMapper;
|
|
|
// 买家订单
|
|
|
if (codeMeta.getType() == OrderCodeType.BUYER_TYPE.getType()){
|
|
|
paymentService = this.buyerOrderPaymentService;
|
|
|
|
|
|
/**
|
|
|
* 机构商户号
|
|
|
*/
|
|
|
public static final String INST_MID = "APPDEFAULT";
|
|
|
}else{
|
|
|
paymentService = this.sellerOrderPaymentService;
|
|
|
|
|
|
@Autowired
|
|
|
OrderCodeGenerator orderCodeGenerator;
|
|
|
}
|
|
|
|
|
|
return paymentService;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PaymentConfirmRsp payConfirm(PaymentRequest request){
|
|
|
|
|
|
// 入口数据检查
|
|
|
if (request.getUid() < 0
|
|
|
|| request.getOrderCode() < 0){
|
|
|
logger.warn("payConfirm request empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
// 反解订单号
|
|
|
// 根据订单号 反解:买家订单号 or 卖家订单号
|
|
|
CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode());
|
|
|
|
|
|
// 获取主场的service
|
|
|
AbstractOrderPaymentService paymentService = getOrderPaymentService(codeMeta, request);
|
|
|
|
|
|
// 获取订单信息
|
|
|
OrderInfo orderInfo = paymentService.getOrderInfo(request);
|
|
|
|
|
|
// 已支付的状态,直接返回
|
|
|
|
|
|
// payment!=null && 待付款,其他直接返回
|
|
|
PaymentConfirmRsp paymentConfirmRsp = PaymentConfirmRsp.builder().orderCode(request.getOrderCode()).build();
|
|
|
if (orderInfo.getPayment() == null || !paymentService.isOrderWaitingPay(orderInfo)){
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// 未支付订单
|
|
|
Payment payment = Payment.getPayment(orderInfo.getPayment());
|
|
|
JSONObject result = paymentService.accessUnionPay(paymentService.buildQueryOrderData(request, payment));
|
|
|
|
|
|
if (result.containsKey("errCode") && "SUCCESS".equals(result.getString("errCode"))){
|
|
|
String stauts = result.getString("status");
|
|
|
if ("TRADE_SUCCESS".equals(stauts)){
|
|
|
|
|
|
public void payConfirm(PaymentRequest request){
|
|
|
// 支付成功 TODO
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//
|
|
|
return paymentConfirmRsp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 订单退款
|
|
|
* 使用场景: [卖家发布商品]页面的[不卖了],
|
...
|
...
|
@@ -113,207 +161,84 @@ public class PaymentServiceImpl implements IPaymentService { |
|
|
*/
|
|
|
public PrepayResponse payment(PaymentRequest request){
|
|
|
|
|
|
// 入口数据检查
|
|
|
if (request.getUid() < 0
|
|
|
|| request.getOrderCode() < 0){
|
|
|
logger.warn("payment not exist");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
Payment payment = checkPaymentRequest(request);
|
|
|
|
|
|
// 不存在的支付方式
|
|
|
Payment payment = Payment.getPayment(request.getPayment());
|
|
|
if (payment == null){
|
|
|
logger.warn("payment payment not exist {}, uid is {}, orderCode is {}" ,request.getPayment(),
|
|
|
request.getUid(), request.getOrderCode());
|
|
|
throw new ServiceException(ServiceError.ORDER_PAYMENT_IS_EMPTY);
|
|
|
}
|
|
|
// 根据订单号 反解:买家订单号 or 卖家订单号
|
|
|
CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode());
|
|
|
|
|
|
// 获取主场的service
|
|
|
AbstractOrderPaymentService paymentService = getOrderPaymentService(codeMeta, request);
|
|
|
|
|
|
// check 订单是否存在
|
|
|
OrderInfo orderInfo = paymentService.getOrderInfo(request);
|
|
|
|
|
|
// 卖家or买家订单,支付方式更新,返回实付金额
|
|
|
BigDecimal amount = checkUpdOrderCode(request);
|
|
|
BigDecimal amount = paymentService.checkUpdOrderCodePayment(orderInfo, request);
|
|
|
|
|
|
// 向银联提下过单的,直接返回结果
|
|
|
PrepayResponse oldAppPayRequest = getAppPayRequest(request);
|
|
|
PrepayResponse oldAppPayRequest = paymentService.getAppPayRequest(request);
|
|
|
if (oldAppPayRequest != null){
|
|
|
logger.info("payment oldAppPayRequest orderCode is {}, uid is {}, oldAppPayRequest is {}",
|
|
|
request.getOrderCode(), request.getUid(), oldAppPayRequest);
|
|
|
logger.info("payment oldAppPayRequest orderCode is {}, uid is {}, oldAppPayRequest is {}",request.getOrderCode(), request.getUid(), oldAppPayRequest);
|
|
|
return oldAppPayRequest;
|
|
|
}
|
|
|
|
|
|
// 生成预支付数据
|
|
|
JSONObject prepayData = buildPrepayData(request, payment, amount);
|
|
|
JSONObject prepayData = paymentService.buildPrepayData(request, payment, amount);
|
|
|
|
|
|
// 返回处理结果
|
|
|
PrepayResponse response = new PrepayResponse();
|
|
|
|
|
|
try{
|
|
|
String result = httpClient.post(unionPayUrl, JSON.toJSONString(prepayData));
|
|
|
JSONObject resultJSON = JSONObject.parseObject(result);
|
|
|
if (resultJSON.containsKey("errCode") && "SUCCESS".equals(resultJSON.getString("errCode"))){
|
|
|
response.setPrepayResult(PrepayResponse.SUCCESS);
|
|
|
response.setJsonObj(resultJSON.getJSONObject("appPayRequest"));
|
|
|
|
|
|
// 保存支付成功结果
|
|
|
saveAppPayRequest(request, response);
|
|
|
|
|
|
logger.info("payment success orderCode is {}", request.getOrderCode() );
|
|
|
return response;
|
|
|
}else {
|
|
|
response.setPrepayResult(PrepayResponse.FAILED);
|
|
|
response.setJsonObj(resultJSON);
|
|
|
logger.warn("payment fail, orderCode is {}, result is {}", request.getOrderCode(), result);
|
|
|
return response;
|
|
|
}
|
|
|
}catch (Exception e) {
|
|
|
logger.warn("payment error orderCode is {}, e is {}", request.getOrderCode());
|
|
|
}
|
|
|
response.setPrepayResult(PrepayResponse.FAILED);
|
|
|
return response;
|
|
|
JSONObject resultJSON = paymentService.accessUnionPay(prepayData);
|
|
|
|
|
|
}
|
|
|
if (resultJSON.containsKey("errCode") && "SUCCESS".equals(resultJSON.getString("errCode"))){
|
|
|
response.setPrepayResult(PrepayResponse.SUCCESS);
|
|
|
response.setJsonObj(resultJSON.getJSONObject("appPayRequest"));
|
|
|
|
|
|
/**
|
|
|
* 获取前回的访问结果
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
private PrepayResponse getAppPayRequest(PaymentRequest request) {
|
|
|
BuyerOrderMeta buyerOrderMeta = buyerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getOrderCode(), getMetaKey(request.getPayment()));
|
|
|
// 保存支付成功结果
|
|
|
paymentService.saveAppPayRequest(orderInfo, response);
|
|
|
|
|
|
if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())){
|
|
|
return null;
|
|
|
}
|
|
|
try{
|
|
|
PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class);
|
|
|
logger.info("payment success orderCode is {}", request.getOrderCode() );
|
|
|
return response;
|
|
|
}catch (Exception e){
|
|
|
}else{
|
|
|
|
|
|
response.setPrepayResult(PrepayResponse.FAILED);
|
|
|
response.setJsonObj(resultJSON);
|
|
|
return response;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private void saveAppPayRequest(PaymentRequest request, PrepayResponse response) {
|
|
|
BuyerOrderMeta buyerOrderMeta = new BuyerOrderMeta();
|
|
|
buyerOrderMeta.setUid(request.getUid());
|
|
|
buyerOrderMeta.setOrderCode(request.getOrderCode());
|
|
|
buyerOrderMeta.setMetaKey(getMetaKey(request.getPayment()));
|
|
|
buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response));
|
|
|
|
|
|
// 记录appPayRequest
|
|
|
buyerOrderMetaMapper.insert(buyerOrderMeta);
|
|
|
}
|
|
|
|
|
|
public String getMetaKey(int payment){
|
|
|
return MetaKey.APP_PAY_REQUEST + OrderConstant.SPLIT_STR +payment;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成预支付数据
|
|
|
* 入口数据检查
|
|
|
* @param request
|
|
|
* @param paymentVal
|
|
|
* @return
|
|
|
*/
|
|
|
private JSONObject buildPrepayData(PaymentRequest request, Payment payment, BigDecimal amount) {
|
|
|
JSONObject prepayData = new JSONObject();
|
|
|
prepayData.put("msgSrc", msgSrc);
|
|
|
prepayData.put("msgType", payment.getValue());
|
|
|
prepayData.put("requestTimestamp", DateUtil.date2String(new Date(), OrderConstant.DATE_FORMAT));
|
|
|
prepayData.put("merOrderId", getPayOrderCode(request.getOrderCode(), payment));
|
|
|
prepayData.put("mid", mid);
|
|
|
prepayData.put("tid", tid);
|
|
|
prepayData.put("instMid", INST_MID);
|
|
|
prepayData.put("notifyUrl", callbackUrl);
|
|
|
// 单位分 存在点,则签名失败,不支持1.0这种
|
|
|
prepayData.put("totalAmount", amount.multiply(new BigDecimal(100)).setScale( 0, BigDecimal.ROUND_DOWN ).longValue());
|
|
|
|
|
|
prepayData.put("sign", SignUtils.getSign(prepayData, signKey));
|
|
|
return prepayData;
|
|
|
}
|
|
|
|
|
|
|
|
|
private String getPayOrderCode(long orderCode, Payment payment){
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
sb.append(OrderConstant.ORDER_CODE_PRE);
|
|
|
sb.append(orderCode);
|
|
|
sb.append(OrderConstant.SPLIT_STR);
|
|
|
sb.append(payment.getCode());
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
private BigDecimal checkUpdOrderCode(PaymentRequest request) {
|
|
|
// 根据订单号 反解:买家订单号 or 卖家订单号
|
|
|
CodeMeta codeMeta = orderCodeGenerator.expId(request.getOrderCode());
|
|
|
if (!OrderCodeType.isExistOrderCodeType(codeMeta.getType())) {
|
|
|
logger.warn("payment orderCode invalidate {}, uid is {}, codeMeta is {}", request.getOrderCode(), request.getUid(), codeMeta.getType());
|
|
|
throw new ServiceException(ServiceError.ORDER_ORDER_CODE_IS_EMPTY);
|
|
|
private Payment checkPaymentRequest(PaymentRequest request) {
|
|
|
// 入口数据检查
|
|
|
if (request.getUid() < 0
|
|
|
|| request.getOrderCode() < 0){
|
|
|
logger.warn("payment request empty");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
logger.info("payment orderCode is {} ,uid is {}, type is {}",request.getOrderCode(), request.getUid(), codeMeta.getType());
|
|
|
|
|
|
// 买家订单号
|
|
|
if (codeMeta.getType() == OrderCodeType.BUYER_TYPE.getType()){
|
|
|
return checkUpdBuyerOrderCode(request);
|
|
|
}else{
|
|
|
return checkUpdSellerOrderCode(request);
|
|
|
// 不存在的支付方式
|
|
|
Payment payment = Payment.getPayment(request.getPayment());
|
|
|
if (payment == null){
|
|
|
logger.warn("payment payment not exist {}, uid is {}, orderCode is {}" ,request.getPayment(),
|
|
|
request.getUid(), request.getOrderCode());
|
|
|
throw new ServiceException(ServiceError.ORDER_PAYMENT_IS_EMPTY);
|
|
|
}
|
|
|
return payment;
|
|
|
}
|
|
|
|
|
|
|
|
|
private BigDecimal checkUpdSellerOrderCode(PaymentRequest request) {
|
|
|
// 订单状态
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid());
|
|
|
if (sellerOrder == null){
|
|
|
logger.warn("payment orderCode not exist, orderCode is {}", request.getOrderCode(), request.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 订单不是未付款的状态
|
|
|
if (sellerOrder.getStatus() == null || sellerOrder.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){
|
|
|
logger.warn("payment status not allow {}, orderCode is {}, uid is {}", sellerOrder.getStatus(), request.getOrderCode(), request.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW);
|
|
|
}
|
|
|
|
|
|
// 检查实付金额
|
|
|
if (sellerOrder.getEarnestMoney() == null || sellerOrder.getEarnestMoney().compareTo(new BigDecimal(0)) <=0 ){
|
|
|
logger.warn("payment amount null, orderCode is {}", request.getOrderCode());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 更新预支付方式
|
|
|
sellerOrder.setPayment(request.getPayment());
|
|
|
sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
sellerOrderMapper.updateByPrimaryKey(sellerOrder);
|
|
|
|
|
|
return sellerOrder.getEarnestMoney();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查买家订单状态,更新支付方式
|
|
|
* @param request
|
|
|
*/
|
|
|
private BigDecimal checkUpdBuyerOrderCode(PaymentRequest request) {
|
|
|
// 订单状态
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(request.getOrderCode(), request.getUid());
|
|
|
if (buyerOrder == null){
|
|
|
logger.warn("payment orderCode not exist, orderCode is {}", request.getOrderCode(), request.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 订单不是未付款的状态
|
|
|
if (buyerOrder.getStatus() == null || buyerOrder.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){
|
|
|
logger.warn("payment status not allow {}, orderCode is {}, uid is {}", buyerOrder.getStatus(), request.getOrderCode(), request.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW);
|
|
|
}
|
|
|
|
|
|
// 检查实付金额
|
|
|
if (buyerOrder.getAmount() == null || buyerOrder.getAmount().compareTo(new BigDecimal(0)) <= 0){
|
|
|
logger.warn("payment amount null, orderCode is {}", request.getOrderCode());
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
// 更新预支付方式
|
|
|
buyerOrder.setPayment((byte)request.getPayment());
|
|
|
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
buyerOrderMapper.updateByPrimaryKey(buyerOrder);
|
|
|
|
|
|
return buyerOrder.getAmount();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|