123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package com.llisoft.pay.service;
- import java.util.Objects;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.llisoft.pay.dao.OrderDao;
- import com.llisoft.pay.entity.App;
- import com.llisoft.pay.entity.Order;
- import com.llisoft.pay.util.CodeUtil;
- import com.llisoft.pay.util.JsonUtil;
- @Service
- public class OrderService {
-
- // 支付状态 未支付
- public static final byte STATUS_WAIT = 1;
- // 支付状态 已支付
- public static final byte STATUS_PAYED = 2;
-
- private Logger logger = LoggerFactory.getLogger(OrderService.class);
-
- @Autowired
- private OrderDao orderDao;
- @Autowired
- private PayService payService;
- @Autowired
- private AppService appService;
-
-
- /**
- * 添加
- * @param appKey 支付key(由支付服务为业务单独分配)
- * @param orderMoney 订单金额(分)
- * @param orderTitle 标题(如果客户端没有传会使用app的name)
- * @return
- * @throws Exception
- */
- public String add(String appKey, int orderMoney, String orderTitle) throws Exception{
- // 验证appKey合法性
- App app = appService.get(appKey);
- if (Objects.isNull(app)) {
- logger.error("appKey不存在: {}", appKey);
- return null;
- }
- // 生成订单号, 当前时间+5位随机数
- String orderNum = CodeUtil.time();
- if(Objects.isNull(orderTitle) || orderTitle.trim().isEmpty()) {
- orderTitle = app.getAppName(); // 没有传title时使用业务名称
- }
- // 创建订单
- Order order = new Order();
- order.setOrderNum(orderNum);
- if(app.getDebug()) {
- orderMoney = 1; // 一分钱测试
- }
- order.setOrderMoney(orderMoney);
- order.setOrderTitle(orderTitle); // 订单描述
- order.setOrderStatus(STATUS_WAIT);
- order.setAppId(app.getAppId());
- orderDao.insert(order);
- logger.debug("添加订单成功: {}", orderNum);
- return orderNum;
- }
-
- /**
- * 订单支付
- * @param orderNum
- * @param payType
- * @return
- * @throws Exception
- */
- public String pay(String orderNum, byte payType, boolean isMobile) throws Exception{
- Order order = orderDao.selectByOrderNum(orderNum);
- if(order.getOrderStatus() == STATUS_PAYED){
- logger.error("订单已经支付过: {}", orderNum);
- return null;
- }
- // 添加支付记录
- String payNum = payService.add(payType, order.getOrderMoney(), order.getOrderId());
- return payService.pay(payNum, isMobile);
- }
-
- /**
- * 订单核对
- * @param orderNum
- * @return
- * @throws Exception
- */
- public boolean check(String orderNum) throws Exception {
- Order order = orderDao.selectByOrderNum(orderNum);
- if(order.getOrderStatus() == STATUS_PAYED) {
- logger.debug("当前订单状态为已支付,直接返回: {}", orderNum);
- return true;
- }
- logger.debug("当前订单状态为未支付,调用接口验证: {}", orderNum);
- return payService.check(order.getOrderId());
- }
-
- /**
- * 完成订单
- * 更新订单状态 + 通知业务服务
- * @param orderId
- * @return 返回同步回调地址
- * @throws Exception
- */
- @Transactional
- public boolean finish(int orderId) throws Exception{
- // 更新订单状态
- orderDao.updatePay(orderId, STATUS_PAYED);
- logger.debug("订单处理成功, 状态更新为已支付: {}", orderId);
- // 发送支付成功异步通知
- Order order = orderDao.select(orderId);
- appService.notify(order.getAppId(), order.getOrderNum(), JsonUtil.toJson(order));
- return true;
- }
-
- }
|