修改下单

This commit is contained in:
wuchunlei
2024-12-04 14:36:51 +08:00
parent 76730d890d
commit e6293ffbbd
2 changed files with 8 additions and 58 deletions

View File

@@ -49,6 +49,9 @@ public class OrderController {
BookEntity bookEntity = bookService.getById(buyOrder.getAbroadBookId());
BigDecimal totalPrice = buyOrder.getOrderMoney();
//校验价格
if (totalPrice.compareTo(new BigDecimal(0))<=0){
return R.error("订单价格不能为0");
}
if (totalPrice.compareTo(bookEntity.getAbroadPrice())!=0){
return R.error("订单价格异常");
}
@@ -77,8 +80,7 @@ public class OrderController {
}
}
Map<String, Object> result = new HashMap<>();
result.put("orderSn", buyOrder.getOrderSn());
result.put("money", totalPrice);
result.put("orderId", buyOrder.getOrderId());
return R.ok(result);
}

View File

@@ -3,31 +3,20 @@ package com.peanut.modules.pay.paypal.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.promeg.pinyinhelper.Pinyin;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.book.service.UserEbookBuyService;
import com.peanut.modules.common.entity.BookEntity;
import com.peanut.modules.common.entity.BuyOrder;
import com.peanut.modules.common.entity.PayPaypalOrder;
import com.peanut.modules.common.service.BookService;
import com.peanut.modules.pay.paypal.config.PaypalConfig;
import com.peanut.modules.pay.paypal.service.PayPaypalOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
/**
@@ -38,8 +27,6 @@ import java.util.*;
@Slf4j
public class PaypalController {
@Autowired
private BookService bookService;
@Autowired
private PaypalConfig paypalConfig;
@Autowired
@@ -47,43 +34,17 @@ public class PaypalController {
@Autowired
private PayPaypalOrderService payPaypalOrderService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private UserEbookBuyService userEbookBuyService;
@RequestMapping("/createOrder")
@Transactional
public R createOrder(@RequestBody Map<String,Object> params){
BuyOrder buyOrder = new BuyOrder();
buyOrder.setUserId(ShiroUtils.getUId());
buyOrder.setCome(3);//3 海外读书
buyOrder.setPaymentMethod("5"); //5 paypal
int bookId = Integer.parseInt(params.get("abroadBookId").toString());
BookEntity bookEntity = bookService.getById(bookId);
BigDecimal orderMoney = new BigDecimal(params.get("orderMoney").toString());
//校验价格
if (orderMoney.compareTo(bookEntity.getAbroadPrice())!=0){
return R.error("订单价格异常");
BuyOrder buyOrder = buyOrderService.getById(params.get("orderId").toString());
if ("3".equals(buyOrder.getOrderStatus())){
return R.error("订单已完成");
}
buyOrder.setOrderMoney(orderMoney);
buyOrder.setRealMoney(orderMoney);
buyOrder.setRemark("Purchase the e-book '"+ Pinyin.toPinyin(bookEntity.getName(), " ").toLowerCase()+"'");
buyOrder.setOrderStatus("0");
buyOrder.setOrderType("abroadBook");
buyOrder.setAbroadBookId(bookId);
String timeId = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(timeId);
JSONObject res = paypalConfig.createOrder(buyOrder);
if (res.containsKey("links")&&res.containsKey("status")){
if ("CREATED".equals(res.get("status").toString())){
buyOrderService.save(buyOrder);
String url = "";
List<JSONObject> links = (List)res.get("links");
for (JSONObject o:links) {
if ("approve".equals(o.get("rel").toString())){
url = o.get("href").toString();
}
}
PayPaypalOrder payPaypalOrder = new PayPaypalOrder();
payPaypalOrder.setOrderId(buyOrder.getOrderId());
payPaypalOrder.setOrderSn(buyOrder.getOrderSn());
@@ -91,24 +52,11 @@ public class PaypalController {
payPaypalOrder.setPaypalOrderId(res.get("id").toString());
payPaypalOrder.setDescription(buyOrder.getRemark());
payPaypalOrderService.save(payPaypalOrder);
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
return R.ok().put("url",url).put("id",res.get("id").toString());
return R.ok().put("id",res.get("id").toString());
}
}
return R.error(res.get("error").toString());
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
return message;
};
}
@RequestMapping("/orderInfo")
public R orderInfo(@RequestBody Map<String,Object> params) throws Exception{