杨杰 před 6 roky
rodič
revize
e5fbe30d0e

+ 1 - 1
src/main/java/com/llisoft/service/pay/config/SwaggerConfig.java

@@ -24,7 +24,7 @@ public class SwaggerConfig {
 	@Bean  
 	public Docket docket() {  
 		return new Docket(DocumentationType.SWAGGER_2)
-			.apiInfo(new ApiInfoBuilder().title("文件服务API").version("v1.0.0").build())
+			.apiInfo(new ApiInfoBuilder().title("支付服务API").version("v1.0.0").build())
 			.directModelSubstitute(byte.class, int.class) //修正byte转string的Bug
 			.select()
 			// 文档中需要屏蔽的接口

+ 26 - 86
src/main/java/com/llisoft/service/pay/controller/OrderController.java

@@ -1,35 +1,30 @@
 package com.llisoft.service.pay.controller;
 
-import java.util.Objects;
-
-import javax.servlet.http.HttpServletRequest;
+import javax.validation.Valid;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
 
 import com.llisoft.common.exception.MtaException;
 import com.llisoft.common.util.JsonUtil;
-import com.llisoft.service.pay.entity.Order;
-import com.llisoft.service.pay.service.AppService;
+import com.llisoft.common.vo.ResponseVo;
 import com.llisoft.service.pay.service.OrderService;
-import com.llisoft.service.pay.service.PayService;
-import com.llisoft.service.pay.vo.OrderRequestVo;
-import com.llisoft.service.pay.vo.OrderResponseVo;
-
+import com.llisoft.service.pay.vo.OrderAddRequestVo;
+import com.llisoft.service.pay.vo.OrderAddResponseVo;
+import com.llisoft.service.pay.vo.OrderInfoRequestVo;
+import com.llisoft.service.pay.vo.OrderInfoResponseVo;
+import com.llisoft.service.pay.vo.OrderPayRequestVo;
+import com.llisoft.service.pay.vo.OrderPayResponseVo;
 
 /**
  * 订单
  */
-@Controller
+@RestController
 @RequestMapping("/order")
 public class OrderController{
 	
@@ -37,88 +32,33 @@ public class OrderController{
 	
 	@Autowired
 	private OrderService orderService;
-	@Autowired
-	private PayService payService;
-	@Autowired
-	private AppService appService;
 	
 	
 	/**
-	 * 添加订单
-	 * 暂时不加认证签名
-	 * @param request
-	 */
-	@PostMapping
-	public @ResponseBody OrderResponseVo add(OrderRequestVo vo) throws MtaException{
-		logger.info("添加订单请求: {}", JsonUtil.toJson(vo));
-		if(Objects.isNull(vo.getAppkey()) || vo.getAppkey().trim().isEmpty() || vo.getMoney()<=0) {
-			throw new MtaException("添加订单: 非法入参: " + JsonUtil.toJson(vo));
-		}
-		String ordernum = orderService.add(vo.getAppkey(), vo.getMoney(), vo.getOrdernum(), vo.getTitle());
-		return this.get(ordernum);
-	}
-	
-	/**
-	 * 支付
-	 * @param orderId
-	 * @param type
-	 * @return
+	 * 订单添加
 	 */
-	@GetMapping("/{orderId}/pay")
-	public String pay(@PathVariable int orderId, // 默认支付宝
-			@RequestParam(required=false,defaultValue="1") byte type) throws MtaException{
-		if(orderId <= 0) {
-			throw new MtaException("订单号非法: " + orderId);
-		}
-		Order order = orderService.get(orderId);
-		if(order.getOrderStatus() == OrderService.STATUS_PAYED){
-			logger.info("订单支付: 订单已经支付过: {}", orderId);
-			return "redirect:/order/"+orderId+"/ok"; // 已经支付过
-		}
-		logger.info("订单支付: {}, 支付类型: {}", orderId, type==1 ? "支付宝" : "微信");
-		String paynum = payService.add(orderId, type);
-		return "redirect:/pay/" + paynum;
+	@PostMapping("/add")
+	public ResponseVo<OrderAddResponseVo> add(@Valid @RequestBody OrderAddRequestVo vo) throws MtaException{
+		logger.info("收到订单添加请求: {}", JsonUtil.toJson(vo));
+		return ResponseVo.success(orderService.add(vo.getAppKey(), vo.getMoney(), vo.getTitle()));
 	}
 	
 	/**
-	 * 支付成功
-	 * @return
-	 * @throws MtaException 
+	 * 订单支付
 	 */
-	@GetMapping(value="/{orderId}/ok")
-	public String ok(@PathVariable int orderId, HttpServletRequest request) throws MtaException{
-		Order order = orderService.get(orderId);
-		if (order.getOrderStatus() == OrderService.STATUS_PAYED) {
-			// 重定向到业务回调地址
-			String returnUrl = appService.getReturnUrl(order.getAppId(), order.getOrderNum());
-			if (Objects.nonNull(returnUrl)) {
-				return "redirect:" + returnUrl;
-			} 
-			// 未配置成功地址的返回默认
-			request.setAttribute("order", order);
-			return "/payok.jsp";
-		}
-		return null;
+	@PostMapping("/pay")
+	public ResponseVo<OrderPayResponseVo> pay(@Valid @RequestBody OrderPayRequestVo vo) throws MtaException{
+		logger.info("收到订单支付请求: {}", JsonUtil.toJson(vo));
+		return ResponseVo.success(orderService.pay(vo.getOrderNum(), vo.getPayType(), vo.isMobile()));
 	}
 	
 	/**
-	 * 获取订单
-	 * @param paynum
-	 * @return
-	 * @throws MtaException 
+	 * 订单信息
 	 */
-	@GetMapping("/{orderNum}")
-	public @ResponseBody OrderResponseVo get(@PathVariable String orderNum) throws MtaException{
-		OrderResponseVo responseBean = null;
-		if(Objects.nonNull(orderNum) && !orderNum.trim().isEmpty()){
-			Order order = orderService.getByOrderNum(orderNum);
-			responseBean = new OrderResponseVo();
-			BeanUtils.copyProperties(order, responseBean);
-			responseBean.setPayed(order.getOrderStatus()==OrderService.STATUS_PAYED);
-			responseBean.setPayUrl("/order/"+orderNum+"/pay");
-		}
-		logger.info("获取订单请求: {}, 返回结果: {}", orderNum, JsonUtil.toJson(responseBean));
-		return responseBean;
+	@PostMapping("/info")
+	public ResponseVo<OrderInfoResponseVo> get(@Valid @RequestBody OrderInfoRequestVo vo) throws MtaException{
+		logger.info("收到订单详情请求: {}", JsonUtil.toJson(vo));
+		return ResponseVo.success(orderService.info(vo.getOrderNum()));
 	}
 	
 }

+ 0 - 69
src/main/java/com/llisoft/service/pay/controller/PayController.java

@@ -1,69 +0,0 @@
-package com.llisoft.service.pay.controller;
-
-import java.io.IOException;
-import java.util.Objects;
-
-import javax.servlet.http.HttpServletResponse;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import com.llisoft.common.exception.MtaException;
-import com.llisoft.service.pay.entity.Item;
-import com.llisoft.service.pay.service.PayService;
-
-
-/**
- * 支付
- */
-@Controller
-@RequestMapping("/pay")
-public class PayController{
-	
-	private Logger logger = LoggerFactory.getLogger(PayController.class);
-	
-	@Autowired
-	private PayService payService;
-	
-
-	/**
-	 * 发起支付
-	 * @param paynum
-	 * @param type 支付方式(1微信/2支付宝)
-	 * @param device
-	 * @return
-	 * @throws IOException 
-	 * @throws MtaException 
-	 */
-	@GetMapping("/{paynum}")
-	public void pay(@PathVariable String paynum, boolean isMobile, HttpServletResponse response) throws IOException, MtaException{
-		logger.info("发起支付请求: {}", paynum);
-		if(Objects.isNull(paynum) || paynum.trim().isEmpty()) {
-			logger.error("发起支付: 非法支付号: {}", paynum);
-			return;
-		}
-		String html = payService.pay(paynum, isMobile);
-		response.setContentType("text/html; charset=utf-8"); 
-		response.getWriter().write(html);
-		response.getWriter().flush();
-		response.getWriter().close();
-	}
-	
-	/**
-	 * 支付成功
-	 * @return
-	 * @throws MtaException 
-	 */
-	@GetMapping("/{paynum}/ok")
-	public String payok(@PathVariable String paynum) throws MtaException{
-		Item pay = payService.get(paynum);
-		return Objects.nonNull(pay) && pay.getPayStatus()==PayService.STATUS_PAYED ? 
-				"redirect:/order/"+pay.getOrderId()+"/ok" : null;
-	}
-
-}

+ 10 - 2
src/main/java/com/llisoft/service/pay/service/AppService.java

@@ -1,8 +1,11 @@
 package com.llisoft.service.pay.service;
 
+import java.util.Objects;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import com.llisoft.common.exception.MtaException;
 import com.llisoft.service.pay.dao.AppDao;
 import com.llisoft.service.pay.entity.App;
 
@@ -17,9 +20,14 @@ public class AppService {
 	 * 获取
 	 * @param appKey
 	 * @return
+	 * @throws MtaException 
 	 */
-	public App get(String appKey){
-		return appDao.selectByAppKey(appKey);
+	public App get(String appKey) throws MtaException{
+		App app = appDao.selectByAppKey(appKey);
+		if (Objects.isNull(app)) {
+			throw new MtaException("APP不存在: " + appKey);
+		}
+		return app;
 	}
 	
 	/**

+ 76 - 41
src/main/java/com/llisoft/service/pay/service/OrderService.java

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.llisoft.common.exception.MtaException;
+import com.llisoft.common.util.BeanUtil;
 import com.llisoft.common.util.DateUtil;
 import com.llisoft.common.util.HttpUtil;
 import com.llisoft.common.util.JsonUtil;
@@ -16,6 +17,9 @@ import com.llisoft.common.util.RandomUtil;
 import com.llisoft.service.pay.dao.OrderDao;
 import com.llisoft.service.pay.entity.App;
 import com.llisoft.service.pay.entity.Order;
+import com.llisoft.service.pay.vo.OrderAddResponseVo;
+import com.llisoft.service.pay.vo.OrderInfoResponseVo;
+import com.llisoft.service.pay.vo.OrderPayResponseVo;
 
 
 @Service
@@ -31,10 +35,81 @@ public class OrderService {
 	@Autowired
 	private OrderDao orderDao;
 	@Autowired
+	private PayService payService;
+	@Autowired
 	private AppService appService;
 	
 	
 	/**
+	 * 添加
+	 * @param appKey 支付key(由支付服务为业务单独分配)
+	 * @param orderMoney 订单金额(分)
+	 * @param orderTitle 标题(如果客户端没有传会使用app的name)
+	 * @return
+	 * @throws MtaException 
+	 */
+	public OrderAddResponseVo add(String appKey, int orderMoney, String orderTitle) throws MtaException{
+		// 验证appKey合法性
+		App app = appService.get(appKey);
+		if (Objects.isNull(app)) {
+			throw new MtaException("appKey不存在: " + appKey);
+		}
+		// 生成订单号, 当前时间+5位随机数
+		String orderNum = DateUtil.formatMillisecond(new Date()) + RandomUtil.getRandomInt(5);
+		if(Objects.isNull(orderTitle) || orderTitle.trim().isEmpty()) {
+			orderTitle = app.getAppName(); // 没有传title时使用业务名称
+		}
+		// 创建订单
+		Order order = new Order();
+		order.setOrderNum(orderNum);
+		order.setOrderMoney(orderMoney);
+		order.setOrderTitle(orderTitle); // 订单描述
+		order.setOrderStatus(STATUS_WAIT);
+		order.setAppId(app.getAppId());
+		orderDao.insert(order);
+		logger.info("添加订单成功: {}", orderNum);
+		// 返回结果
+		OrderAddResponseVo vo = new OrderAddResponseVo();
+		vo.setOrderNum(orderNum);
+		return vo;
+	}
+	
+	/**
+	 * 订单支付
+	 * @param orderNum
+	 * @param payType
+	 * @return
+	 * @throws MtaException
+	 */
+	public OrderPayResponseVo pay(String orderNum, byte payType, boolean isMobile) throws MtaException{
+		Order order = this.getByOrderNum(orderNum);
+		if(order.getOrderStatus() == STATUS_PAYED){
+			throw new MtaException("订单已经支付过: " + orderNum);
+		}
+		// 添加支付记录
+		String payNum = payService.add(order.getOrderId(), payType);
+		String html = payService.pay(payNum, isMobile);
+		// 返回结果
+		OrderPayResponseVo vo = new OrderPayResponseVo();
+		vo.setHtml(html);
+		return vo;
+	}
+	
+	/**
+	 * 订单详情
+	 * @param orderNum
+	 * @return
+	 * @throws MtaException 
+	 */
+	public OrderInfoResponseVo info(String orderNum) throws MtaException {
+		Order order = this.getByOrderNum(orderNum);
+		OrderInfoResponseVo vo = BeanUtil.transformBean(order, OrderInfoResponseVo.class);
+		vo.setPayed(order.getOrderStatus() == STATUS_PAYED);
+		return vo;
+	}
+	
+	
+	/**
 	 * 获取
 	 * @param orderId
 	 * @return
@@ -63,47 +138,6 @@ public class OrderService {
 	}
 	
 	/**
-	 * 添加
-	 * @param appKey 支付key(由支付服务为业务单独分配)
-	 * @param orderMoney 订单金额(分)
-	 * @param orderNum 订单号(如果客户端没有传会自动生成)
-	 * @param orderTitle 标题(如果客户端没有传会使用app的name)
-	 * @return
-	 * @throws MtaException 
-	 */
-	public String add(String appKey, int orderMoney, String orderNum, String orderTitle) throws MtaException{
-		// 验证入参合法性
-		if (Objects.isNull(appKey) || appKey.trim().isEmpty() || orderMoney<=0) {
-			throw new MtaException("非法入参: appKey="+appKey+", orderMoney="+orderMoney);
-		}
-		// 验证appKey合法性
-		App app = appService.get(appKey);
-		if (Objects.isNull(app)) {
-			throw new MtaException("appKey不存在: "+appKey);
-		}
-		// 验证订单号
-		if(Objects.nonNull(orderNum) && !orderNum.trim().isEmpty()) {
-			throw new MtaException("订单号重复: "+orderNum);
-		}
-		// 创建订单
-		if(Objects.isNull(orderNum) || orderNum.trim().isEmpty()) {
-			orderNum = DateUtil.formatMillisecond(new Date()) + RandomUtil.getRandomInt(5); // 当前时间+5位随机数
-		}
-		if(Objects.isNull(orderTitle) || orderTitle.trim().isEmpty()) {
-			orderTitle = app.getAppName(); // 没有传title时使用业务名称
-		}
-		Order order = new Order();
-		order.setOrderNum(orderNum);
-		order.setOrderMoney(orderMoney);
-		order.setOrderTitle(orderTitle); // 订单描述
-		order.setOrderStatus(STATUS_WAIT);
-		order.setAppId(app.getAppId());
-		orderDao.insert(order);
-		logger.info("添加订单成功: {}", orderNum);
-		return orderNum;
-	}
-	
-	/**
 	 * 完成
 	 * @param orderNum
 	 * @return
@@ -126,5 +160,6 @@ public class OrderService {
 		}
 		return true;
 	}
+
 	
 }

+ 48 - 0
src/main/java/com/llisoft/service/pay/vo/OrderAddRequestVo.java

@@ -0,0 +1,48 @@
+package com.llisoft.service.pay.vo;
+
+import javax.validation.constraints.NotBlank;
+
+import org.hibernate.validator.constraints.Range;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel("订单添加请求实体")
+public class OrderAddRequestVo {
+
+	@ApiModelProperty("appKey")
+	@NotBlank(message="appKey不能为空")
+    private String appKey;
+	
+	@ApiModelProperty("订单金额, 单位:分")
+	@Range(min=1, message="金额非法")
+	private int money;
+	
+	@ApiModelProperty("商品标题, 第三方支付页面显示, 不传时显示app.name")
+    private String title;
+
+	public String getAppKey() {
+		return appKey;
+	}
+
+	public void setAppKey(String appKey) {
+		this.appKey = appKey;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public int getMoney() {
+		return money;
+	}
+
+	public void setMoney(int money) {
+		this.money = money;
+	}
+  
+}

+ 21 - 0
src/main/java/com/llisoft/service/pay/vo/OrderAddResponseVo.java

@@ -0,0 +1,21 @@
+package com.llisoft.service.pay.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel("订单添加返回实体")
+public class OrderAddResponseVo {
+	
+	@ApiModelProperty("订单号")
+    private String orderNum;
+
+	public String getOrderNum() {
+		return orderNum;
+	}
+
+	public void setOrderNum(String orderNum) {
+		this.orderNum = orderNum;
+	}
+
+ 
+}

+ 23 - 0
src/main/java/com/llisoft/service/pay/vo/OrderInfoRequestVo.java

@@ -0,0 +1,23 @@
+package com.llisoft.service.pay.vo;
+
+import javax.validation.constraints.NotBlank;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel("订单详情请求实体")
+public class OrderInfoRequestVo {
+
+	@ApiModelProperty("订单号")
+	@NotBlank(message="订单号不能为空")
+    private String orderNum;
+
+	public String getOrderNum() {
+		return orderNum;
+	}
+
+	public void setOrderNum(String orderNum) {
+		this.orderNum = orderNum;
+	}
+  
+}

+ 14 - 37
src/main/java/com/llisoft/service/pay/vo/OrderResponseVo.java → src/main/java/com/llisoft/service/pay/vo/OrderInfoResponseVo.java

@@ -2,43 +2,27 @@ package com.llisoft.service.pay.vo;
 
 import java.util.Date;
 
-/**
- * 订单返回实体
- */
-public class OrderResponseVo {
-	
-    private String appKey;
-    
-    private String orderNum;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 
-    private int orderMoney;
+@ApiModel("订单详情返回实体")
+public class OrderInfoResponseVo {
 
-    private String orderTitle;
+	@ApiModelProperty("订单号")
+    private String orderNum;
 
-    private boolean payed;
+	@ApiModelProperty("订单金额")
+    private int orderMoney;
 
+	@ApiModelProperty("下单时间")
     private Date createTime;
 
+	@ApiModelProperty("支付时间")
     private Date payTime;
-    
-    private String payUrl;
-    
-
-    public String getPayUrl() {
-		return payUrl;
-	}
-
-	public void setPayUrl(String payUrl) {
-		this.payUrl = payUrl;
-	}
-
-	public String getAppKey() {
-		return appKey;
-	}
 
-	public void setAppKey(String appKey) {
-		this.appKey = appKey;
-	}
+	@ApiModelProperty("是否支付")
+    private boolean payed;
+    
 
 	public String getOrderNum() {
 		return orderNum;
@@ -56,14 +40,6 @@ public class OrderResponseVo {
 		this.orderMoney = orderMoney;
 	}
 
-	public String getOrderTitle() {
-		return orderTitle;
-	}
-
-	public void setOrderTitle(String orderTitle) {
-		this.orderTitle = orderTitle;
-	}
-
 	public boolean isPayed() {
 		return payed;
 	}
@@ -87,4 +63,5 @@ public class OrderResponseVo {
     public void setPayTime(Date payTime) {
         this.payTime = payTime;
     }
+    
 }

+ 49 - 0
src/main/java/com/llisoft/service/pay/vo/OrderPayRequestVo.java

@@ -0,0 +1,49 @@
+package com.llisoft.service.pay.vo;
+
+import javax.validation.constraints.NotBlank;
+
+import org.hibernate.validator.constraints.Range;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel("订单支付请求实体")
+public class OrderPayRequestVo {
+
+	@ApiModelProperty("订单号")
+	@NotBlank(message="订单号不能为空")
+    private String orderNum;
+	
+	@ApiModelProperty("支付方式 (1支付宝/2微信) 默认支付宝")
+	@Range(min=1, message="支付方式非法")
+    private byte payType = 1;
+	
+	@ApiModelProperty("是否移动端")
+	private boolean mobile;
+
+	
+	public String getOrderNum() {
+		return orderNum;
+	}
+
+	public void setOrderNum(String orderNum) {
+		this.orderNum = orderNum;
+	}
+
+	public byte getPayType() {
+		return payType;
+	}
+
+	public void setPayType(byte payType) {
+		this.payType = payType;
+	}
+
+	public boolean isMobile() {
+		return mobile;
+	}
+
+	public void setMobile(boolean mobile) {
+		this.mobile = mobile;
+	}
+
+}

+ 20 - 0
src/main/java/com/llisoft/service/pay/vo/OrderPayResponseVo.java

@@ -0,0 +1,20 @@
+package com.llisoft.service.pay.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel("订单支付返回实体")
+public class OrderPayResponseVo {
+	
+	@ApiModelProperty("HTML文本")
+    private String html;
+
+	public String getHtml() {
+		return html;
+	}
+
+	public void setHtml(String html) {
+		this.html = html;
+	}
+ 
+}

+ 0 - 46
src/main/java/com/llisoft/service/pay/vo/OrderRequestVo.java

@@ -1,46 +0,0 @@
-package com.llisoft.service.pay.vo;
-
-/**
- * 订单请求实体
- */
-public class OrderRequestVo {
-
-    private String appkey;
-    private String ordernum; // 订单号 不传自动生成
-    private String title; // 商品描述 不传使用app的name 
-    private int money; // 单位: 分
-
-
-    public String getAppkey() {
-        return appkey;
-    }
-
-    public void setAppkey(String appkey) {
-        this.appkey = appkey == null ? null : appkey.trim();
-    }
-
-	public String getOrdernum() {
-		return ordernum;
-	}
-
-	public void setOrdernum(String ordernum) {
-        this.ordernum = ordernum == null ? null : ordernum.trim();
-	}
-
-	public String getTitle() {
-		return title;
-	}
-
-	public void setTitle(String title) {
-		this.title = title;
-	}
-
-	public int getMoney() {
-		return money;
-	}
-
-	public void setMoney(int money) {
-		this.money = money;
-	}
-  
-}