订d
This commit is contained in:
@@ -159,29 +159,58 @@ public class OrderInfoServiceImpl implements IOrderInfoService
|
|||||||
@Transactional
|
@Transactional
|
||||||
public AjaxResult payOrder(PaymentRequest request) {
|
public AjaxResult payOrder(PaymentRequest request) {
|
||||||
try {
|
try {
|
||||||
// 参数校验
|
// 获取当前用户
|
||||||
if (StringUtils.isEmpty(request.getOrderId())) {
|
|
||||||
return AjaxResult.error("订单ID不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询订单
|
|
||||||
OrderInfo orderInfo = selectOrderByOrderId(request.getOrderId());
|
|
||||||
if (orderInfo == null) {
|
|
||||||
return AjaxResult.error("订单不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查订单状态
|
|
||||||
if (orderInfo.getPayStatus() != PayStatusEnum.CREATE.getStatus()) {
|
|
||||||
return AjaxResult.error("订单状态不正确");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查用户权限
|
|
||||||
LoginUser loginUser = SecurityUtils.getLoginUser();
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||||||
if (loginUser == null || !loginUser.getUserId().equals(orderInfo.getUserId())) {
|
if (loginUser == null) {
|
||||||
return AjaxResult.error("无权限操作此订单");
|
return AjaxResult.error(401, "用户未登录");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用第三方支付接口 TODO:// 待修改
|
OrderInfo orderInfo = null;
|
||||||
|
|
||||||
|
// 如果是 applePay 且订单ID为空,允许创建新订单
|
||||||
|
if (StringUtils.isEmpty(request.getOrderId()) && "applePay".equals(request.getPayType())) {
|
||||||
|
// 为 applePay 创建临时订单
|
||||||
|
orderInfo = new OrderInfo();
|
||||||
|
orderInfo.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||||
|
orderInfo.setOrderId(System.currentTimeMillis()); // 使用时间戳作为订单号
|
||||||
|
orderInfo.setUserId(loginUser.getUserId());
|
||||||
|
orderInfo.setPayType("applePay");
|
||||||
|
orderInfo.setPayStatus(PayStatusEnum.CREATE.getStatus()); // 1-创建订单
|
||||||
|
orderInfo.setIdDel(0L);
|
||||||
|
orderInfo.setVersion(1L);
|
||||||
|
orderInfo.setCreateTime(DateUtils.getNowDate());
|
||||||
|
orderInfo.setDeviceType(request.getDeviceType());
|
||||||
|
orderInfo.setClientIp(request.getAttach()); // 可能将IP放在attach中
|
||||||
|
|
||||||
|
// 保存临时订单
|
||||||
|
int result = insertOrderInfo(orderInfo);
|
||||||
|
if (result <= 0) {
|
||||||
|
return AjaxResult.error("创建临时订单失败");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 参数校验
|
||||||
|
if (StringUtils.isEmpty(request.getOrderId())) {
|
||||||
|
return AjaxResult.error("订单ID不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询订单
|
||||||
|
orderInfo = selectOrderByOrderId(request.getOrderId());
|
||||||
|
if (orderInfo == null) {
|
||||||
|
return AjaxResult.error("订单不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查订单状态
|
||||||
|
if (orderInfo.getPayStatus() != PayStatusEnum.CREATE.getStatus()) {
|
||||||
|
return AjaxResult.error("订单状态不正确");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查用户权限
|
||||||
|
if (!loginUser.getUserId().equals(orderInfo.getUserId())) {
|
||||||
|
return AjaxResult.error("无权限操作此订单");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用第三方支付接口
|
||||||
ShopUser shopUser = processPayment(orderInfo, request);
|
ShopUser shopUser = processPayment(orderInfo, request);
|
||||||
if (shopUser != null){
|
if (shopUser != null){
|
||||||
return AjaxResult.success(shopUser);
|
return AjaxResult.success(shopUser);
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import com.ruoyi.common.utils.StringUtils;
|
|||||||
import com.ruoyi.system.mapper.OrderInfoMapper;
|
import com.ruoyi.system.mapper.OrderInfoMapper;
|
||||||
import com.ruoyi.system.service.IOrderInfoService;
|
import com.ruoyi.system.service.IOrderInfoService;
|
||||||
import com.ruoyi.system.service.ShopUserService;
|
import com.ruoyi.system.service.ShopUserService;
|
||||||
import org.checkerframework.checker.units.qual.A;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -33,6 +32,7 @@ import java.security.cert.X509Certificate;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 描述:
|
* 描述:
|
||||||
@@ -252,25 +252,46 @@ public class AppleyPay {
|
|||||||
// 计算服务开始和结束时间
|
// 计算服务开始和结束时间
|
||||||
Date startTime = new Date();
|
Date startTime = new Date();
|
||||||
|
|
||||||
|
// 创建或更新订单记录
|
||||||
|
OrderInfo orderInfo = null;
|
||||||
|
boolean isNewOrder = false; // 标记是否为新订单
|
||||||
|
|
||||||
|
// 如果 orderParam 为 null 或 orderId 为空,需要创建新订单
|
||||||
|
if (orderParam == null || orderParam.getOrderId() == null) {
|
||||||
|
// 创建新订单
|
||||||
|
orderInfo = new OrderInfo();
|
||||||
|
orderInfo.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||||
|
orderInfo.setOrderId(System.currentTimeMillis()); // 使用时间戳作为订单号
|
||||||
|
orderInfo.setUserId(userId);
|
||||||
|
orderInfo.setPayType("applePay");
|
||||||
|
orderInfo.setIdDel(0L);
|
||||||
|
orderInfo.setVersion(1L);
|
||||||
|
orderInfo.setCreateTime(DateUtils.getNowDate());
|
||||||
|
isNewOrder = true;
|
||||||
|
} else {
|
||||||
|
// 查询现有订单
|
||||||
|
orderInfo = orderInfoService.selectOrderByOrderId(orderParam.getOrderId().toString());
|
||||||
|
if (orderInfo == null) {
|
||||||
|
// 如果订单不存在,创建新订单
|
||||||
|
orderInfo = new OrderInfo();
|
||||||
|
orderInfo.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||||
|
orderInfo.setOrderId(orderParam.getOrderId());
|
||||||
|
orderInfo.setUserId(userId);
|
||||||
|
orderInfo.setPayType("applePay");
|
||||||
|
orderInfo.setIdDel(0L);
|
||||||
|
orderInfo.setVersion(1L);
|
||||||
|
orderInfo.setCreateTime(DateUtils.getNowDate());
|
||||||
|
isNewOrder = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 创建订单记录
|
// 设置订单信息
|
||||||
OrderInfo orderInfo = new OrderInfo();
|
|
||||||
orderInfo.setOrderId(orderParam.getOrderId());
|
|
||||||
|
|
||||||
orderInfo.setUserId(userId);
|
|
||||||
orderInfo.setPayType("applePay");
|
|
||||||
orderInfo.setPayStatus(2L); // 2-待出货
|
orderInfo.setPayStatus(2L); // 2-待出货
|
||||||
orderInfo.setPayTime(DateUtils.getNowDate());
|
orderInfo.setPayTime(DateUtils.getNowDate());
|
||||||
orderInfo.setStartTime(startTime);
|
orderInfo.setStartTime(startTime);
|
||||||
|
|
||||||
orderInfo.setTradeNo(transactionId);
|
orderInfo.setTradeNo(transactionId);
|
||||||
orderInfo.setCallbackContent(sb.toString()); // 保存完整的回调内容
|
orderInfo.setCallbackContent(transactionId); // 保存交易ID
|
||||||
orderInfo.setCallTime(DateUtils.getNowDate());
|
orderInfo.setCallTime(DateUtils.getNowDate());
|
||||||
orderInfo.setCallbackContent(transactionId);
|
|
||||||
|
|
||||||
orderInfo.setIdDel(0L);
|
|
||||||
orderInfo.setVersion(1L);
|
|
||||||
orderInfo.setCreateTime(DateUtils.getNowDate());
|
|
||||||
orderInfo.setUpdateTime(DateUtils.getNowDate());
|
orderInfo.setUpdateTime(DateUtils.getNowDate());
|
||||||
|
|
||||||
if ("com.mingyue.product.month".equals(productId)) {
|
if ("com.mingyue.product.month".equals(productId)) {
|
||||||
@@ -284,8 +305,6 @@ public class AppleyPay {
|
|||||||
Date endTime = calculateEndTime(startTime, packageType);
|
Date endTime = calculateEndTime(startTime, packageType);
|
||||||
orderInfo.setEndTime(endTime);
|
orderInfo.setEndTime(endTime);
|
||||||
orderInfo.setPackageType(packageType);
|
orderInfo.setPackageType(packageType);
|
||||||
orderInfoService.updateOrderInfo(orderInfo);
|
|
||||||
return shopUserService.updateShopUserVipTime(userId, "1");
|
|
||||||
} else if ("com.mingyue.product.quarterly".equals(productId)) {
|
} else if ("com.mingyue.product.quarterly".equals(productId)) {
|
||||||
// 季付
|
// 季付
|
||||||
packageType = "2";
|
packageType = "2";
|
||||||
@@ -297,8 +316,6 @@ public class AppleyPay {
|
|||||||
orderInfo.setOrderName(orderName);
|
orderInfo.setOrderName(orderName);
|
||||||
orderInfo.setEndTime(endTime);
|
orderInfo.setEndTime(endTime);
|
||||||
orderInfo.setPackageType(packageType);
|
orderInfo.setPackageType(packageType);
|
||||||
orderInfoService.updateOrderInfo(orderInfo);
|
|
||||||
return shopUserService.updateShopUserVipTime(userId, "2");
|
|
||||||
} else if ("com.mingyue.product.semiAnnual".equals(productId)) {
|
} else if ("com.mingyue.product.semiAnnual".equals(productId)) {
|
||||||
// 半年付
|
// 半年付
|
||||||
packageType = "3";
|
packageType = "3";
|
||||||
@@ -310,8 +327,6 @@ public class AppleyPay {
|
|||||||
orderInfo.setOrderName(orderName);
|
orderInfo.setOrderName(orderName);
|
||||||
orderInfo.setEndTime(endTime);
|
orderInfo.setEndTime(endTime);
|
||||||
orderInfo.setPackageType(packageType);
|
orderInfo.setPackageType(packageType);
|
||||||
orderInfoService.updateOrderInfo(orderInfo);
|
|
||||||
return shopUserService.updateShopUserVipTime(userId, "3");
|
|
||||||
} else if ("com.mingyue.product.annual".equals(productId)) {
|
} else if ("com.mingyue.product.annual".equals(productId)) {
|
||||||
// 年付
|
// 年付
|
||||||
packageType = "4";
|
packageType = "4";
|
||||||
@@ -323,12 +338,20 @@ public class AppleyPay {
|
|||||||
Date endTime = calculateEndTime(startTime, packageType);
|
Date endTime = calculateEndTime(startTime, packageType);
|
||||||
orderInfo.setEndTime(endTime);
|
orderInfo.setEndTime(endTime);
|
||||||
orderInfo.setPackageType(packageType);
|
orderInfo.setPackageType(packageType);
|
||||||
orderInfoService.updateOrderInfo(orderInfo);
|
|
||||||
return shopUserService.updateShopUserVipTime(userId, "4");
|
|
||||||
} else {
|
} else {
|
||||||
log.error("用户ID:{}, 未知的产品ID: {}", userId, productId);
|
log.error("用户ID:{}, 未知的产品ID: {}", userId, productId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据是否为新订单选择插入或更新
|
||||||
|
if (isNewOrder) {
|
||||||
|
orderInfoService.insertOrderInfo(orderInfo);
|
||||||
|
} else {
|
||||||
|
orderInfoService.updateOrderInfo(orderInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新用户VIP时间
|
||||||
|
return shopUserService.updateShopUserVipTime(userId, packageType);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("向苹果发起验证支付请求是否有效出现异常:{}", e.getMessage(), e);
|
log.error("向苹果发起验证支付请求是否有效出现异常:{}", e.getMessage(), e);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user