Spring MVC+Spring+Mybatis 实现支付宝支付功能
drop?table?if?exists?user;
/*==============================================================*/
/*?Table:?user??????????????????????????????????????????????????*/
/*==============================================================*/
create?table?user
(
id???????????????????varchar(20)?not?null,
username?????????????varchar(128),
sex??????????????????varchar(20),
primary?key?(id)
);
alter?table?user?comment?'用户表';
CREATE?TABLE?`flow`?(
`id`?varchar(20)?NOT?NULL,
`flow_num`?varchar(20)?DEFAULT?NULL?COMMENT?'流水号',
`order_num`?varchar(20)?DEFAULT?NULL?COMMENT?'订单号',
`product_id`?varchar(20)?DEFAULT?NULL?COMMENT?'产品主键 ID',
`paid_amount`?varchar(11)?DEFAULT?NULL?COMMENT?'支付金额',
`paid_method`?int(11)?DEFAULT?NULL?COMMENT?'支付方式\r\n????????????1:支付宝\r\n????????????2:微信',
`buy_counts`?int(11)?DEFAULT?NULL?COMMENT?'购买个数',
`create_time`?datetime?DEFAULT?NULL?COMMENT?'创建时间',
PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='流水表';
CREATE?TABLE?`orders`?(
`id`?varchar(20)?NOT?NULL,
`order_num`?varchar(20)?DEFAULT?NULL?COMMENT?'订单号',
`order_status`?varchar(20)?DEFAULT?NULL?COMMENT?'订单状态\r\n????????????10:待付款\r\n????????????20:已付款',
`order_amount`?varchar(11)?DEFAULT?NULL?COMMENT?'订单金额',
`paid_amount`?varchar(11)?DEFAULT?NULL?COMMENT?'实际支付金额',
`product_id`?varchar(20)?DEFAULT?NULL?COMMENT?'产品表外键 ID',
`buy_counts`?int(11)?DEFAULT?NULL?COMMENT?'产品购买的个数',
`create_time`?datetime?DEFAULT?NULL?COMMENT?'订单创建时间',
`paid_time`?datetime?DEFAULT?NULL?COMMENT?'支付时间',
PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='订单表';
CREATE?TABLE?`product`?(
`id`?varchar(20)?NOT?NULL,
`name`?varchar(20)?DEFAULT?NULL?COMMENT?'产品名称',
`price`?varchar(11)?DEFAULT?NULL?COMMENT?'价格',
PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4?COMMENT='产品表?';
3、dao 数据接口层
这里就不介绍了,这个只包括简单的 curd,可以使用`通用 mapper`,或者`逆向工程`就行。以订单 order 为例给出:
public?interface?OrdersMapper?{
int?countByExample(OrdersExample?example);
int?deleteByExample(OrdersExample?example);
int?deleteByPrimaryKey(String?id);
int?insert(Orders?record);
int?insertSelective(Orders?record);
List<Orders>?selectByExample(OrdersExample?example);
Orders?selectByPrimaryKey(String?id);
int?updateByExampleSelective(@Param("record")?Orders?record,?@Param("example")?OrdersExample?example);
int?updateByExample(@Param("record")?Orders?record,?@Param("example")?OrdersExample?example);
int?updateByPrimaryKeySelective(Orders?record);
int?updateByPrimaryKey(Orders?record);
}
注意:源代码最后给出
4、service 层
同上,最后在项目源代码里可见。 以订单 order 为例给出:
/**
*?订单操作?service
*?@author?ibm
*
*/
public?interface?OrdersService {
/**
*?新增订单
*?@param?order
*/
public?void?saveOrder(Orders?order);
/**
*?
*?@Title:?OrdersService.java
*?@Package?com.sihai.service
*?@Description:?修改叮当状态,改为?支付成功,已付款;?同时新增支付流水
*?Copyright:?Copyright?(c)?2017
*?Company:FURUIBOKE.SCIENCE.AND.TECHNOLOGY
*?
*?@author?sihai
*?@date?2017 年 8 月 23 日?下
午 9:04:35
*?@version?V1.0
*/
public?void?updateOrderStatus(String?orderId,?String?alpayFlowNum,?String?paidAmount);
/**
*?获取订单
*?@param?orderId
*?@return
*/
public?Orders?getOrderById(String?orderId);
}
4、支付宝支付 controller(支付流程)
支付流程图
首先,启动项目后,输入 http://localhost:8080/,会进入到商品页面,如下:
下面是页面代码
商品页面(products.jsp)
代码实现:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"?pageEncoding="UTF-8"%>
<%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>
<%@?taglib?uri="http://java.sun.com/jsp/jstl/functions"?prefix="fn"?%>??
<script?src="<%=request.getContextPath()?%>/static/js/jquery.min.js"?type="text/javascript"></script>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>
产品编号
</td>
<td>
产品名称
</td>
<td>
产品价格
</td>
<td>
操作
</td>
</tr>
<c:forEach?items="${pList?}"?var="p">
<tr>
<td>
${p.id?}
</td>
<td>
${p.name?}
</td>
<td>
${p.price?}
</td>
<td>
<a?href="<%=request.getContextPath()?%>/alipay/goConfirm.action?productId=${p.id?}">购买</a>
</td>
</tr>
</c:forEach>
</table>
<input?type="hidden"?id="hdnContextPath"?name="hdnContextPath"?value="<%=request.getContextPath()?%>"/>
</body>
</html>
<script?type="text/javascript">
$(document).ready(function() {
var?hdnContextPath?=?$("#hdnContextPath").val();
});
</script>
点击上面的购买,进入到订单页面
填写个数,然后点击生成订单,调用如下代码
根据SID
(生成 id 的工具)等信息生成订单,保存到数据库。
进入到选择支付页面
调用了如下代码:
然后,我们选择支付宝支付,进入到了我们支付的页面了,大功告成!
调用了如下代码:
/**
*
*?@Title:?AlipayController.java
*?@Package?com.sihai.controller
*?@Description:?前往支付宝第三方网关进行支付
*?Copyright:?Copyright?(c)?2017
*?Company:FURUIBOKE.SCIENCE.AND.TECHNOLOGY
*
*?@author?sihai
*?@date?2017 年 8 月 23 日?下午 8:50:43
*?@version?V1.0
*/
@RequestMapping(value?=?"/goAlipay",?produces?=?"text/html;?charset=UTF-8")
@ResponseBody
public?String?goAlipay(String?orderId,?HttpServletRequest?request,?HttpServletRequest?response)?throws?Exception {
Orders?order?=?orderService.getOrderById(orderId);
Product?product?=?productService.getProductById(order.getProductId());
//获得初始化的 AlipayClient
AlipayClient?alipayClient?=?new?DefaultAlipayClient(AlipayConfig.gatewayUrl,?AlipayConfig.app_id,?AlipayConfig.merchant_private_key,?"json",?AlipayConfig.charset,?AlipayConfig.alipay_public_key,?AlipayConfig.sign_type);
//设置请求参数
AlipayTradePagePayRequest?alipayRequest?=?new?AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商户订单号,商户网站订单系统中唯一订单号,必填
String?out_trade_no?=?orderId;
//付款金额,必填
String?total_amount?=?order.getOrderAmount();
//订单名称,必填
String?subject?=?product.getName();
评论