| | |
| | | import com.yeshi.buwan.exception.order.OrderException; |
| | | import com.yeshi.buwan.exception.order.PayException; |
| | | import com.yeshi.buwan.exception.vip.VIPException; |
| | | import com.yeshi.buwan.exception.vip.VideoBuyRecordException; |
| | | import com.yeshi.buwan.pptv.PPTVVipManager; |
| | | import com.yeshi.buwan.service.inter.order.OrderService; |
| | | import com.yeshi.buwan.service.manager.GoldCornManager; |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public OrderRecord createOrder(OrderRecord record) throws OrderException { |
| | | if (record.getMoney() == null || record.getType() == null || record.getOrderType() == null || record.getUid() == null) { |
| | | if (record.getMoney() == null || (record.getType() == null && record.getVideoCid() == null) || record.getOrderType() == null || record.getUid() == null) { |
| | | throw new OrderException(1, "参数不完整"); |
| | | } |
| | | |
| | |
| | | if (record.getCreateTime() == null) { |
| | | record.setCreateTime(new Date()); |
| | | } |
| | | |
| | | if (record.getPayWay() == OrderRecord.PAY_WAY_WX) { |
| | | if (record.getGoldCorn() != null && record.getGoldCorn() > 0) { |
| | | if (record.getMoney().compareTo(new BigDecimal("0")) == 0) { |
| | | record.setPayWay(OrderRecord.PAY_WAY_GOLDCORN); |
| | | } else { |
| | | record.setPayWay(OrderRecord.PAY_WAY_WX_GOLDCORN); |
| | | } |
| | | } |
| | | } else if (record.getPayWay() == OrderRecord.PAY_WAY_ALIPAY) { |
| | | if (record.getGoldCorn() != null && record.getGoldCorn() > 0) { |
| | | if (record.getMoney().compareTo(new BigDecimal("0")) == 0) { |
| | | record.setPayWay(OrderRecord.PAY_WAY_GOLDCORN); |
| | | } else { |
| | | record.setPayWay(OrderRecord.PAY_WAY_ALIPAY_GOLDCORN); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | record.setMoneyPay(false); |
| | | record.setGoldCornPay(false); |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | // @GlobalTransactional(timeoutMills = 30000, name = "buwan-order", rollbackFor = Exception.class) |
| | | @Override |
| | | public PayWayInfoDTO payOrder(OrderRecord record) throws OrderException, GoldCornException, PayException { |
| | | //TODO 影视豆支付 |
| | | public PayWayInfoDTO payOrder(OrderRecord record) throws OrderException, GoldCornException, PayException, VIPException, PPTVException, VideoBuyRecordException { |
| | | String orderNo = VIPOrderUtil.getOutOrderNo(record.getOrderType(), record.getId()); |
| | | switch (record.getPayWay()) { |
| | | case OrderRecord |
| | |
| | | } catch (Exception e) { |
| | | throw new GoldCornException(1, "影视豆扣除出错"); |
| | | } |
| | | paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date()); |
| | | |
| | | orderLogger.info("订单影视豆支付成功:id-{}", record.getId()); |
| | | case OrderRecord |
| | | .PAY_WAY_ALIPAY: { |
| | | if (record.getMoney() != null && record.getMoney().compareTo(new BigDecimal(0)) == 0) { |
| | | return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId())); |
| | | } |
| | | //生成支付宝支付订单 |
| | | String form = VipUtil.getVipChargeAlipayForm(record.getId(), orderNo, record.getMoney()); |
| | | String form = VipUtil.getVipChargeAlipayForm(record.getId(), record.getOrderType(), orderNo, record.getMoney()); |
| | | //暂存2分钟 |
| | | String id = StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis()); |
| | | redisManager.cacheCommonString(id, form, 120); |
| | |
| | | .PAY_WAY_WX_GOLDCORN: |
| | | try { |
| | | goldCornManager.consumeGoldCorn(record, record.getGoldCorn()); |
| | | |
| | | } catch (Exception e) { |
| | | throw new GoldCornException(1, "影视豆扣除出错"); |
| | | } |
| | | paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date()); |
| | | orderLogger.info("订单影视豆支付成功:id-{} 类型-{}", record.getId(), record.getOrderType().name()); |
| | | case OrderRecord |
| | | .PAY_WAY_WX: { |
| | | if (record.getMoney() != null && record.getMoney().compareTo(new BigDecimal(0)) == 0) { |
| | | return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId())); |
| | | } |
| | | //生成微信支付订单 |
| | | try { |
| | | String title = ""; |
| | |
| | | case OrderRecord.PAY_WAY_GOLDCORN: { |
| | | try { |
| | | goldCornManager.consumeGoldCorn(record, record.getGoldCorn()); |
| | | orderLogger.info("订单影视豆支付成功:id-{}", record.getId()); |
| | | } catch (Exception e) { |
| | | throw new GoldCornException(1, "影视豆扣除出错"); |
| | | } |
| | | orderLogger.info("订单影视豆支付成功:id-{}", record.getId()); |
| | | paySuccess(record.getId(), OrderRecord.PAY_WAY_GOLDCORN, null, new Date()); |
| | | return new PayWayInfoDTO(0, VipUtil.getPaySuccessUrl(record.getId())); |
| | | } |
| | | break; |
| | | } |
| | | throw new OrderException(1, "支付方式不存在"); |
| | | } |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public OrderRecord paySuccess(String id, int payWay, BigDecimal payMoney, Date payTime) throws |
| | | VIPException, PPTVException { |
| | | VIPException, PPTVException, VideoBuyRecordException, OrderException { |
| | | orderLogger.info("订单支付成功目的执行:id-{},支付方式-{},支付金额-{},支付时间-{}", id, payWay, payMoney, payTime.getTime()); |
| | | |
| | | Session session = vipOrderRecordDao.getSession(); |
| | |
| | | //修改记录 |
| | | Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(OrderRecord.class).setCacheable(false).setParameter(0, id); |
| | | List<OrderRecord> list = query.list(); |
| | | if (list == null || list.size() == 0) |
| | | throw new OrderException(10, "订单不存在"); |
| | | |
| | | if (list != null && list.size() > 0) { |
| | | OrderRecord record = list.get(0); |
| | | if (record.getState() != OrderRecord.STATE_NOT_PAY) |
| | | throw new VIPException(1, "订单未处于待支付状态"); |
| | | OrderRecord record = list.get(0); |
| | | if (record.getState() != OrderRecord.STATE_NOT_PAY) |
| | | throw new OrderException(1, "订单未处于待支付状态"); |
| | | |
| | | |
| | | if (record.getPayWay() != OrderRecord.PAY_WAY_GOLDCORN) { |
| | | record.setPayMoney(payMoney); |
| | | record.setMoneyPay(true); |
| | | } else { |
| | | record.setGoldCornPay(true); |
| | | } |
| | | |
| | | record.setPayTime(payTime); |
| | | record.setUpdateTime(new Date()); |
| | | record.setState(getPayState(record)); |
| | | |
| | | //已经支付成功 |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | session.createSQLQuery("insert into wk_vip_order_pay_success(id,create_time) value(?,now())").setParameter(0, id).executeUpdate(); |
| | | |
| | | } |
| | | |
| | | if (record.getOrderType() == OrderType.vip) { |
| | | Date[] expireDate = addExpireTime(session, record.getUid(), payTime, record.getType()); |
| | | record.setVipStartTime(expireDate[0]); |
| | | record.setVipEndTime(expireDate[1]); |
| | | if (expireDate == null) { |
| | | throw new VIPException(2, "添加用户会员时间出错"); |
| | | } |
| | | session.update(record); |
| | | |
| | | |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | orderLogger.info("订单全部支付成功:id-{}", id); |
| | | //购买VIP |
| | | pptvVipManager.buyVIP(record); |
| | | orderLogger.info("购买VIP成功:id-{}", id); |
| | | } else { |
| | | orderLogger.info("订单部分支付成功:id-{}", id); |
| | | } |
| | | |
| | | } else if (record.getOrderType() == OrderType.video) { |
| | | record.setVipStartTime(payTime); |
| | | //7天之内有效 |
| | | record.setVipEndTime(new Date(payTime.getTime() + 1000 * 60 * 60L * 24 * 7)); |
| | | session.update(record); |
| | | //单片购买成功(7天有效期) |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | orderLogger.info("订单全部支付成功:id-{}", id); |
| | | //购买单片 |
| | | pptvVipManager.buyVideo(record); |
| | | orderLogger.info("购买单片成功:id-{}", id); |
| | | } else { |
| | | orderLogger.info("订单部分支付成功:id-{}", id); |
| | | } |
| | | } |
| | | |
| | | return record; |
| | | if (payWay != OrderRecord.PAY_WAY_GOLDCORN) { |
| | | record.setPayMoney(payMoney); |
| | | record.setMoneyPay(true); |
| | | } else { |
| | | throw new VIPException(10, "订单不存在"); |
| | | record.setGoldCornPay(true); |
| | | } |
| | | |
| | | record.setPayTime(payTime); |
| | | record.setUpdateTime(new Date()); |
| | | record.setState(getPayState(record)); |
| | | |
| | | //已经支付成功 |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | session.createSQLQuery("insert into wk_vip_order_pay_success(id,create_time) value(?,now())").setParameter(0, id).executeUpdate(); |
| | | } |
| | | |
| | | if (record.getOrderType() == OrderType.vip) { |
| | | Date[] expireDate = addExpireTime(session, record.getUid(), payTime, record.getType()); |
| | | record.setVipStartTime(expireDate[0]); |
| | | record.setVipEndTime(expireDate[1]); |
| | | if (expireDate == null) { |
| | | throw new VIPException(2, "添加用户会员时间出错"); |
| | | } |
| | | session.update(record); |
| | | |
| | | |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | orderLogger.info("订单全部支付成功:id-{}", id); |
| | | //购买VIP |
| | | pptvVipManager.buyVIP(record); |
| | | orderLogger.info("购买VIP成功:id-{}", id); |
| | | } else { |
| | | orderLogger.info("订单部分支付成功:id-{}", id); |
| | | } |
| | | |
| | | } else if (record.getOrderType() == OrderType.video) { |
| | | record.setVipStartTime(payTime); |
| | | //7天之内有效 |
| | | record.setVipEndTime(new Date(payTime.getTime() + 1000 * 60 * 60L * 24 * 7)); |
| | | session.update(record); |
| | | //单片购买成功(7天有效期) |
| | | if (record.getState() == OrderRecord.STATE_PAY) { |
| | | orderLogger.info("订单全部支付成功:id-{}", id); |
| | | //购买单片 |
| | | pptvVipManager.buyVideo(record); |
| | | orderLogger.info("购买单片成功:id-{}", id); |
| | | } else { |
| | | orderLogger.info("订单部分支付成功:id-{}", id); |
| | | } |
| | | } |
| | | |
| | | return record; |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | try { |
| | | goldCornManager.drawbackGoldCorn(record); |
| | | } catch (Exception e) { |
| | | throw new GoldCornException(1, "影视豆退款成功"); |
| | | throw new GoldCornException(1, "影视豆退款失败"); |
| | | } |
| | | } |
| | | |
| | |
| | | record.setState(OrderRecord.STATE_CANCEL); |
| | | record.setUpdateTime(new Date()); |
| | | record.setRemarks(reason); |
| | | vipOrderRecordDao.update(record); |
| | | session.update(record); |
| | | orderLogger.info("取消订单成功:id-{}", id); |
| | | } |
| | | |
| | | @Override |
| | | public List<OrderRecord> getCanCancelOrderList(int page, int pageSize) { |
| | | |
| | | OrderRecordDao.DaoQuery daoQuery = new OrderRecordDao.DaoQuery(); |
| | | daoQuery.state = OrderRecord.STATE_NOT_PAY; |
| | | //30分钟未支付取消订单 |
| | | daoQuery.maxCreateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 30); |
| | | daoQuery.minCreateTime = new Date(daoQuery.maxCreateTime.getTime() - 1000 * 60 * 60L * 24); |
| | | |
| | | return vipOrderRecordDao.list(daoQuery); |
| | | } |
| | | |
| | | |
| | |
| | | return record; |
| | | |
| | | switch (record.getPayWay()) { |
| | | case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN: |
| | | case OrderRecord.PAY_WAY_ALIPAY: { |
| | | //支付宝 |
| | | AlipayTradeQueryResponse res = null; |
| | |
| | | } |
| | | |
| | | break; |
| | | case OrderRecord.PAY_WAY_WX_GOLDCORN: |
| | | case OrderRecord.PAY_WAY_WX: { |
| | | //微信 |
| | | try { |
| | |
| | | UserVIPInfo vip = new UserVIPInfo(); |
| | | vip.setUid(uid); |
| | | vip.setCreateTime(new Date()); |
| | | vip.setExpireDate(getExpireTime(payTime, null, type)); |
| | | |
| | | Date[] expireDate = getExpireTime(payTime, null, type); |
| | | vip.setExpireDate(expireDate[1]); |
| | | session.save(vip); |
| | | return new Date[]{payTime, vip.getExpireDate()}; |
| | | return expireDate; |
| | | } else { |
| | | //修改 |
| | | UserVIPInfo vipInfo = (UserVIPInfo) list.get(0); |
| | | Date expireDate = getExpireTime(payTime, vipInfo.getExpireDate(), type); |
| | | session.createQuery("update UserVIPInfo i set i.expireDate=? ,i.updateTime=? where i.uid=?").setParameter(0, expireDate).setParameter(1, new Date()).setParameter(2, uid).executeUpdate(); |
| | | Date[] expireDate = getExpireTime(payTime, vipInfo.getExpireDate(), type); |
| | | session.createQuery("update UserVIPInfo i set i.expireDate=? ,i.updateTime=? where i.uid=?").setParameter(0, expireDate[1]).setParameter(1, new Date()).setParameter(2, uid).executeUpdate(); |
| | | |
| | | return new Date[]{vipInfo.getExpireDate() == null ? new Date() : vipInfo.getExpireDate(), expireDate}; |
| | | return expireDate; |
| | | } |
| | | } |
| | | |
| | |
| | | * @param type |
| | | * @return |
| | | */ |
| | | private Date getExpireTime(Date payTime, Date expireTime, VIPPriceType type) { |
| | | private Date[] getExpireTime(Date payTime, Date expireTime, VIPPriceType type) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | if (expireTime != null) { |
| | | calendar.setTimeInMillis(expireTime.getTime()); |
| | | //之前续期时间是否大于付款时间 |
| | | if (payTime.getTime() < expireTime.getTime()) |
| | | calendar.setTimeInMillis(expireTime.getTime()); |
| | | else |
| | | calendar.setTimeInMillis(payTime.getTime()); |
| | | } else { |
| | | calendar.setTimeInMillis(payTime.getTime()); |
| | | } |
| | | long startTime = calendar.getTimeInMillis(); |
| | | if (type == VIPPriceType.day) { |
| | | calendar.add(Calendar.DAY_OF_WEEK, 1); |
| | | } else if (type == VIPPriceType.week) { |
| | |
| | | } else if (type == VIPPriceType.year) { |
| | | calendar.add(Calendar.YEAR, 1); |
| | | } |
| | | return new Date(calendar.getTimeInMillis()); |
| | | return new Date[]{new Date(startTime), new Date(calendar.getTimeInMillis())}; |
| | | } |
| | | |
| | | |