Authored by Lixiaodi

增加人工打款

... ... @@ -47,12 +47,12 @@
</select>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yohoufo.dal.order.model.TradeBills" useGeneratedKeys="true">
insert into trade_bills (uid, order_code, user_type,pay_type,trade_type,
insert into trade_bills (id, uid, order_code, user_type,pay_type,trade_type,
income_outcome,amount,system_amount,trade_status,create_time
,deal_uid,deal_status,deal_time,deal_relate_id)
values (#{uid},#{orderCode},#{userType},#{payType},#{tradeType},
values (#{id},#{uid},#{orderCode},#{userType},#{payType},#{tradeType},
#{incomeOutcome},#{amount},#{systemAmount},
#{tradeStatus},#{createTime},#{dealUid},#{dealStatus}#{dealTime},#{dealRelateId})
#{tradeStatus},#{createTime},#{dealUid},#{dealStatus},#{dealTime},#{dealRelateId})
</insert>
<select id="selectGoodsIncomeOrCompensateIncomeTradeBillsWithPageByUid" resultMap="BaseResultMap">
... ... @@ -119,12 +119,12 @@
deal_relate_id = #{dealRelateId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER} and trade_status != 100
where id = #{id,jdbcType=INTEGER} and deal_status != 1
</update>
<update id="updateToFailByPrimaryKey" parameterType="com.yohoufo.dal.order.model.TradeBills" >
update trade_bills set trade_status = #{tradeStatus,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER} and trade_status = 100
update trade_bills set deal_status = #{dealStatus,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
... ...
... ... @@ -3,6 +3,7 @@ package com.yohoufo.order.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -633,7 +634,6 @@ public class PaymentServiceImpl implements IPaymentService {
// 预标记成功
TradeBills preSuccess = new TradeBills();
preSuccess.setId(tradeBills.getId());
preSuccess.setTradeStatus(100);
preSuccess.setDealTime(now);
preSuccess.setDealStatus(1);
preSuccess.setDealUid(req.getOperateUid());
... ... @@ -646,27 +646,36 @@ public class PaymentServiceImpl implements IPaymentService {
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;
PayRefundBo refundBo = manualRefund(orderCode, amount);
if (refundBo.getRefundStatus() == RefundContant.PAYMENT_REFUND_RESULTCODE_SUCCESS) {
// 加新流水
tradeBills.setDealRelateId(tradeBills.getId());
tradeBills.setId(null);
tradeBills.setTradeStatus(100);
tradeBills.setCreateTime(now);
addTradeBills(tradeBills);
return;
} else {
throw new ServiceException(500, "退款失败:" + refundBo.getRefundMsg());
}
}
} 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);
if (e instanceof ServiceException) {
throw new ServiceException(((ServiceException) e).getCode(), "退款失败id=" + tradeBillsId + "," + e.getMessage());
} else {
throw new ServiceException(500, "退款失败id=" + tradeBillsId);
}
}
try {
OrdersPayTransfer transfer = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
if (transfer == null || transfer.getStatus() == 1) {
throw new ServiceException(400, "转账记录不存在或者已成功转账,取消转账,流水id=" + tradeBillsId);
}
JSONObject jsonObject = alipayService.transferMoney(Long.toString(orderCode), alipayAccount, amount);
if (jsonObject == null) {
logger.warn("manualDeal 转账失败 , req is {}", req);
... ... @@ -681,19 +690,12 @@ public class PaymentServiceImpl implements IPaymentService {
&& jsonObject.containsKey("order_id")
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
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);
}
// 加新流水
tradeBills.setDealRelateId(tradeBills.getId());
tradeBills.setId(null);
tradeBills.setTradeStatus(100);
tradeBills.setCreateTime(now);
addTradeBills(tradeBills);
} else {
logger.warn("manualDealErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
// 上报
... ... @@ -702,15 +704,63 @@ public class PaymentServiceImpl implements IPaymentService {
} catch (Exception e) {
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);
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);
}
}
private PayRefundBo manualRefund(long orderCode, BigDecimal amount){
// 根据订单号,反解 卖家订单号 or 买家订单号
CodeMeta codeMeta = orderCodeGenerator.expId(orderCode);
// 确认状态是已支付, 存在支付状态
AbstractOrderPaymentService orderPaymentService = getOrderPaymentService(codeMeta, orderCode);
OrderInfo orderInfo = orderPaymentService.getOrderInfo(orderCode);
// 获取支付成功记录
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderInfo.getOrderCode(), orderInfo.getUid());
if(ordersPay == null || ordersPay.getAmount() == null){
logger.warn("refund paid record not exist.orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
}
if(!CollectionUtils.isEmpty(ordersPayRefundMapper.selectByOrderCode(orderCode))) {
logger.warn("refund has success before .orderCode is {}", orderCode);
throw new ServiceException(400, "订单已经退款成功过!");
}
// 支付金额小于退款金额
if (ordersPay.getAmount().compareTo(amount) < 0) {
logger.warn("refund refundAmount {} > amount {}, orderCode is {}",
ordersPay.getAmount(), amount, orderCode);
throw new ServiceException(ServiceError.ORDER_REFUND_OVER_TOTAL_AMOUNT);
}
// 退款
AbstractPayService payService = getPayService(orderInfo.getPayment());
PayRefundBo payRefundBo = new PayRefundBo();
payRefundBo.setPayOrderCode(ordersPay.getOrderCode());
payRefundBo.setAmount(amount.doubleValue());
payRefundBo.setOrderTotalFee(ordersPay.getAmount().doubleValue());
payRefundBo.setRefundOrderCode(String.valueOf(ordersPay.getOrderCode()) + ordersPay.getId());
PayRefundBo refundBo = payService.refundOpenApi(payRefundBo);
//退款申请成功的记录添加到orders_pay_refund表
if (refundBo.getRefundStatus() == RefundContant.PAYMENT_REFUND_RESULTCODE_SUCCESS) {
recordPayRefund(refundBo);
}
return refundBo;
}
private void addTradeBills(TradeBills record) {
try {
tradeBillsMapper.insert(record);
... ...