use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.
the class OrderServiceImpl method wxPayNotify.
@Override
public void wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
String xmlResult = null;
try {
xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
} catch (IOException e) {
log.error(WxPayNotifyResponse.fail(e.getMessage()), e);
}
WxPayOrderNotifyResult result = null;
try {
result = wxPayService.parseOrderNotifyResult(xmlResult);
if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
log.error(xmlResult);
}
} catch (WxPayException e) {
log.error(e.getMessage(), e);
}
log.info("处理腾讯支付平台的订单支付, {}", result.getReturnMsg());
String orderSn = result.getOutTradeNo();
String payId = result.getTransactionId();
// 分转化成元
String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
if (order == null) {
log.error("微信支付回调:订单不存在,orderSn:{}", orderSn);
return;
}
// 检查这个订单是否已经处理过
if (OrderUtil.hasPayed(order)) {
log.error("微信支付回调:订单已经处理过了,orderSn:{}", orderSn);
return;
}
// 检查支付订单金额
if (!totalFee.equals(order.getActualPrice().toString())) {
log.error("微信支付回调: 支付金额不符合,orderSn:{},totalFee:{}", order.getOrderSn(), totalFee);
return;
}
order.setPayId(payId);
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
order.setUpdateTime(new Date());
if (!updateById(order)) {
log.error("微信支付回调: 更新订单状态失败,order:{}", JSON.toJSONString(order.getOrderSn()));
return;
}
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
}
// 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId());
// 取消订单超时未支付任务
taskService.removeTask(new OrderUnpaidTask(order.getId()));
}
use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.
the class OrderServiceImpl method h5pay.
@Override
@Transactional(rollbackFor = Exception.class)
public R h5pay(String orderSn, Integer payType, HttpServletRequest request) {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
Long userId = order.getUserId();
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
// 保存支付方式
boolean update = lambdaUpdate().set(Order::getPayType, payType).eq(Order::getOrderSn, orderSn).update();
if (!update) {
return R.error(ReturnCodeEnum.ORDER_SET_PAY_ERROR);
}
switch(Objects.requireNonNull(PayTypeEnum.of(payType))) {
case WX:
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setTradeType(WxPayConstants.TradeType.MWEB);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
case ALI:
// 初始化
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGateway(), alipayConfig.getAppId(), alipayConfig.getRsaPrivateKey(), alipayConfig.getFormat(), alipayConfig.getCharset(), alipayConfig.getAlipayPublicKey(), alipayConfig.getSigntype());
// 创建API对应的request,使用手机网站支付request
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
// 在公共参数中设置回跳和通知地址
String url = WaynConfig.getMobileUrl() + request.getContextPath();
alipayRequest.setReturnUrl(url + "/returnOrders/" + orderSn + "/" + userId);
alipayRequest.setNotifyUrl(url + "/paySuccess?payType=1&orderSn=" + orderSn);
// 填充业务参数
// 必填
// 商户订单号,需保证在商户端不重复
String out_trade_no = orderSn + new Random().nextInt(9999);
// 销售产品码,与支付宝签约的产品码名称。目前仅支持FAST_INSTANT_TRADE_PAY
String product_code = "FAST_INSTANT_TRADE_PAY";
// 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]。
BigDecimal actualPrice = order.getActualPrice();
String total_amount = actualPrice.toString();
// 订单标题
String subject = "支付宝测试";
// 选填
// 商品描述,可空
String body = "商品描述";
alipayRequest.setBizContent("{" + "\"out_trade_no\":\"" + out_trade_no + "\"," + "\"product_code\":\"" + product_code + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"}");
// 请求
String form;
try {
// 需要自行申请支付宝的沙箱账号、申请appID,并在配置文件中依次配置AppID、密钥、公钥,否则这里会报错。
// 调用SDK生成表单
form = alipayClient.pageExecute(alipayRequest).getBody();
return R.success().add("form", form);
} catch (AlipayApiException e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
}
case ALI_TEST:
// 支付宝test,直接更新支付状态为已支付
order.setPayId("xxxxx0987654321-ali");
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
order.setUpdateTime(new Date());
if (!updateById(order)) {
return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
}
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
}
// 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId());
// 取消订单超时未支付任务
taskService.removeTask(new OrderUnpaidTask(order.getId()));
return R.success();
default:
return R.error(ReturnCodeEnum.ORDER_NOT_SUPPORT_PAYWAY_ERROR);
}
}
use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.
the class OrderServiceImpl method prepay.
@Override
@Transactional(rollbackFor = Exception.class)
public R prepay(String orderSn, Integer payType, HttpServletRequest request) {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!returnCodeEnum.equals(ReturnCodeEnum.SUCCESS)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
// 设置支付方式
order.setPayType(payType);
Member member = iMemberService.getById(MobileSecurityUtils.getUserId());
String openid = member.getWeixinOpenid();
if (openid == null) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setOpenid(openid);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
}
use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.
the class OrderServiceImpl method cancel.
@Override
@Transactional(rollbackFor = Exception.class)
public R cancel(Long orderId) {
Order order = getById(orderId);
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isCancel()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_CANCAL_ERROR);
}
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_CANCEL);
order.setOrderEndTime(LocalDateTime.now());
order.setUpdateTime(new Date());
if (!updateById(order)) {
throw new BusinessException("更新数据已失效");
}
// 商品货品数量增加
List<OrderGoods> goodsList = iOrderGoodsService.list(new QueryWrapper<OrderGoods>().eq("order_id", orderId));
for (OrderGoods orderGoods : goodsList) {
Long productId = orderGoods.getProductId();
Integer number = orderGoods.getNumber();
if (!iGoodsProductService.addStock(productId, number)) {
throw new BusinessException("商品货品库存增加失败");
}
}
// releaseCoupon(orderId);
return R.success();
}
use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.
the class OrderServiceImpl method delete.
@Override
@Transactional(rollbackFor = Exception.class)
public R delete(Long orderId) {
Order order = getById(orderId);
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isDelete()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_DELETE_ERROR);
}
// 删除订单
removeById(orderId);
// 删除订单商品
iOrderGoodsService.remove(new QueryWrapper<OrderGoods>().eq("order_id", orderId));
return R.success();
}
Aggregations