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.ItemDao; import com.llisoft.pay.dao.OrderDao; import com.llisoft.pay.entity.Item; import com.llisoft.pay.entity.Order; import com.llisoft.pay.util.CodeUtil; @Service public class PayService { // 支付状态 未支付 public static final byte STATUS_WAIT = 1; // 支付状态 已支付 public static final byte STATUS_PAYED = 2; // 支付类型 支付宝 public static final byte TYPE_ALI = 1; // 支付类型 微信 public static final byte TYPE_WX = 2; // 支付标记 - 异步通知 public static final byte FLAG_NOTIFY = 1; // 支付标记 - 同步回调 public static final byte FLAG_RETURN = 2; // 支付标记 - 主动查询 public static final byte FLAG_QUERY = 3; // 支付标记 - 对账 public static final byte FLAG_CHECK = 4; private Logger logger = LoggerFactory.getLogger(PayService.class); @Autowired private ItemDao payDao; @Autowired private OrderDao orderDao; @Autowired private AliPayService aliPayService; @Autowired private WxPayService wxPayService; @Autowired private OrderService orderService; /** * 获取 * @param payNum * @return * @throws Exception */ public Item get(String payNum) throws Exception{ Item pay = payDao.selectByPayNum(payNum); if (Objects.isNull(pay)) { throw new Exception("支付记录不存在: " + payNum); } return pay; } /** * 添加支付 * @param payType 支付类型 * @param payMoney 支付金额 * @param orderId 订单号 * @return 返回支付跳转地址 */ public String add(byte payType, int payMoney, int orderId){ String payNum = CodeUtil.uuid(); Item pay = new Item(); pay.setPayNum(payNum); pay.setPayType(payType); pay.setPayMoney(payMoney); pay.setPayStatus(STATUS_WAIT); pay.setOrderId(orderId); payDao.insert(pay); logger.debug("添加支付成功: {}", payNum); return payNum; } /** * 发起支付 * @param payNum 支付号 * @param isMobile 是否移动端页面 * @return * @throws Exception */ public String pay(String payNum, boolean isMobile) throws Exception{ Item item = this.get(payNum); if (Objects.isNull(item)) { throw new Exception("支付记录不存在: " + payNum); } Order order = orderDao.select(item.getOrderId()); if (item.getPayType() == TYPE_ALI) { // 支付宝 return isMobile ? aliPayService.paym(payNum, order.getOrderMoney(), order.getOrderTitle()) : aliPayService.pay(payNum, order.getOrderMoney(), order.getOrderTitle()); }else if (item.getPayType() == TYPE_WX) { // 微信 return wxPayService.pay(payNum, order.getOrderMoney(), order.getOrderTitle()); } return null; } /** * 完成支付 * 验证订单信息 + 更新支付状态 + 更新订单状态(包括支付方式) * @param payNum * @param tradeNum 第三方交易号(由第三方通知返回) * @param money 实际支付金额(分) * @param flag 支付成功标记(1异步通知/2同步通知/3主动查询/4对账) * @return * @throws Exception */ @Transactional public boolean finish(String payNum, String tradeNum, int money, byte flag) throws Exception{ Item item = this.get(payNum); if (item.getPayMoney() != money) { // 核对金额 throw new Exception("支付金额异常: " + money); } // 更新支付状态 payDao.updatePay(item.getPayId(), tradeNum, STATUS_PAYED, flag); logger.debug("支付处理成功, 状态更新为已支付: {}", payNum); return orderService.finish(item.getOrderId()); } /** * 验证支付状态 * @param payNum * @return * @throws Exception */ public boolean check(int orderId) throws Exception { Item pay = payDao.selectByOrderIdLast(orderId); if(Objects.isNull(pay)) { return false; } // 未支付状态,去第三方验证 if (pay.getPayStatus() == STATUS_WAIT) { if(pay.getPayType() == TYPE_ALI) { return aliPayService.query(pay.getPayNum()); }else if (pay.getPayType() == TYPE_WX) { return wxPayService.query(pay.getPayNum()); } } return false; } }