package com.ks.vip.service.remote;
|
|
import com.alipay.api.AlipayApiException;
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
import com.ks.vip.exception.OrderPayException;
|
import com.ks.vip.exception.VipOrderException;
|
import com.ks.vip.mapper.VipOrderMapper;
|
import com.ks.vip.pojo.DO.VipCombo;
|
import com.ks.vip.pojo.DO.VipOrder;
|
import com.ks.vip.pojo.DTO.PayWayInfoDTO;
|
import com.ks.vip.pojo.DTO.VipOrderDTO;
|
import com.ks.vip.pojo.Enums.OrderTypeEnum;
|
import com.ks.vip.pojo.Enums.PayWayEnum;
|
import com.ks.vip.service.VipCenterService;
|
import com.ks.vip.service.VipComboService;
|
import com.ks.vip.service.VipOrederPayService;
|
import com.ks.vip.service.manager.RedisManager;
|
import com.ks.vip.util.Constant;
|
import com.ks.vip.util.PayUtil;
|
import org.apache.dubbo.config.annotation.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.alipay.AlipayH5PayUtil;
|
import org.yeshi.utils.entity.wx.WXPayOrderInfoV3;
|
import org.yeshi.utils.wx.WXPayV3Util;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.UUID;
|
|
@Service(version = "1.0")
|
public class VipOrderPayServiceImpl implements VipOrederPayService {
|
|
@Resource
|
private VipOrderMapper vipOrderMapper;
|
|
@Resource
|
private VipComboService vipComboService;
|
|
@Resource
|
private VipCenterService vipCenterService;
|
|
@Resource
|
private RedisManager redisManager;
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public PayWayInfoDTO addOrderWithPay(VipOrderDTO t, String notifyUrl, String returnUrl) throws VipOrderException {
|
if (t.getComboId() == null || t.getComboId() <= 0) {
|
throw new VipOrderException(1, "套餐ID不能为空");
|
}
|
if (StringUtil.isNullOrEmpty(t.getUid())) {
|
throw new VipOrderException(1, "用户ID不能为空");
|
}
|
if (t.getPayWay() == null) {
|
throw new VipOrderException(1, "付款方式不能为空");
|
}
|
if (StringUtil.isNullOrEmpty(t.getThreeOrderId())) {
|
throw new VipOrderException(1, "付款订单号不能为空");
|
}
|
|
// 检查是否已记录
|
VipOrder order = vipOrderMapper.getByThreeOrderId(t.getThreeOrderId());
|
if (order != null) {
|
throw new VipOrderException(1, "该付款订单号已存在");
|
}
|
|
// 判断订单类型: 首单、续费
|
OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
|
long count = vipOrderMapper.countByTypeAndUid(t.getUid(), orderType);
|
if (count > 0) {
|
orderType = OrderTypeEnum.buyRenew;
|
}
|
|
// 插入订单信息
|
VipOrder newOrder = new VipOrder();
|
newOrder.setComboId(t.getComboId());
|
newOrder.setUid(t.getUid());
|
newOrder.setThreeOrderId(t.getThreeOrderId());
|
newOrder.setPayWay(t.getPayWay().name());
|
newOrder.setType(orderType);
|
newOrder.setCreateTime(new Date());
|
vipOrderMapper.insertSelective(newOrder);
|
|
// 支付方式
|
try {
|
return payOrder(newOrder, notifyUrl, returnUrl);
|
} catch (OrderPayException e) {
|
e.printStackTrace();
|
throw new VipOrderException(VipOrderException.CODE_CREATE_PAY_ORDER_FAIL, e.getMsg());
|
}
|
}
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public PayWayInfoDTO payOrder(VipOrder order, String notifyUrl, String returnUrl) throws OrderPayException {
|
if (order.getState() == VipOrder.STATE_PAY) {
|
throw new OrderPayException(OrderPayException.CODE_ALREADY_PAY, "已经完成支付");
|
}
|
String goodsTitle = Constant.ORDER_PAY_TITLE;
|
String orderId = order.getId() + "";
|
switch (order.getPayWay()) {
|
//生成支付宝支付订单
|
case "alipay":
|
String form = PayUtil.getVipChargeAlipayForm(orderId, goodsTitle, orderId, order.getPayMoney(), notifyUrl, returnUrl);
|
if (StringUtil.isNullOrEmpty(form)) {
|
throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, "创建支付宝支付订单失败");
|
}
|
//暂存2分钟
|
String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis());
|
redisManager.cacheCommonString(id, form, 120);
|
return new PayWayInfoDTO(PayWayEnum.alipay.getWay(), null, id);
|
case "weChat":
|
try {
|
String payUrl = PayUtil.createWXOrder(null, orderId, order.getPayMoney(), goodsTitle, notifyUrl, returnUrl);
|
return new PayWayInfoDTO(PayWayEnum.weChat.getWay(), payUrl, null);
|
} catch (Exception e) {
|
throw new OrderPayException(OrderPayException.CODE_CREATE_PAY_ORDER_FAIL, e.getMessage());
|
}
|
}
|
return null;
|
}
|
|
@Override
|
public String getAlipayForm(String formId) {
|
return redisManager.getCommonString(formId);
|
}
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public VipOrder checkOrderPayState(Long id) {
|
VipOrder order = vipOrderMapper.selectByPrimaryKey(id);
|
if (order == null || order.getState() == VipOrder.STATE_PAY) {
|
return order;
|
}
|
switch (order.getPayWay()) {
|
//支付宝
|
case "alipay":
|
AlipayTradeQueryResponse res = null;
|
try {
|
res = AlipayH5PayUtil.queryOrder(PayUtil.getAlipayApp(), id + "", null);
|
//支付成功
|
if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) {
|
try {
|
return paySuccess(order);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
} catch (AlipayApiException e) {
|
e.printStackTrace();
|
}
|
//微信
|
case "weChat":
|
try {
|
WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(id + "", PayUtil.getWXAPP());
|
if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) {
|
paySuccess(order);
|
return order;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 支付成功更新处理
|
*
|
* @param order
|
* @return
|
*/
|
@Transactional(rollbackFor = Exception.class)
|
public VipOrder paySuccess(VipOrder order) throws VipOrderException {
|
|
VipCombo vipCombo = vipComboService.selectByPrimaryKey(order.getComboId());
|
if (vipCombo == null) {
|
throw new VipOrderException(1, "该套餐不存在");
|
}
|
// 有效天数
|
Integer validDays = vipCombo.getValidDays();
|
|
// 判断订单类型
|
OrderTypeEnum orderType = OrderTypeEnum.buyNewly;
|
long count = vipOrderMapper.countByTypeAndUid(order.getUid(), orderType);
|
if (count > 0) {
|
orderType = OrderTypeEnum.buyRenew;
|
}
|
|
// order.setThreeOrderId(); TODO
|
order.setState(VipOrder.STATE_PAY);
|
order.setPayTime(new Date());
|
order.setUpdateTime(new Date());
|
vipOrderMapper.updateByPrimaryKeySelective(order);
|
|
// 更新等级
|
vipCenterService.saveVipCenter(order.getUid(), vipCombo.getGradeId(), validDays * 24, order.getId());
|
|
return order;
|
}
|
|
}
|