文章有不当之处,欢迎指正,若是喜欢微信阅读,你也能够关注个人微信公众号:
好好学java
,获取优质学习资源。javascript
下载地址:https://docs.open.alipay.com/270/106291/html
readme.txt请好好看一下。java
只有一个Java配置类,其他都是JSP。mysql
(1).注册蚂蚁金服开发者帐号(免费,不像苹果会收取费用)jquery
注册地址:https://open.alipay.com ,用你的支付宝帐号扫码登陆,完善我的信息,选择服务类型(我选的是自研)。git
(2).设置app_id和gatewayUrlgithub
其中密钥须要本身生成,appID和支付宝网关是已经给好的,网关有dev字样,代表是用于开发测试。spring
(3).设置密钥sql
点击“生成方法”,打开界面以下:数据库
下周密钥生成工具,解压打开后,选择2048位生成密钥:
若是没有设置过,此时显示文本是“设置应用公钥”,我这里是已经设置过得。
设置方法,“打开密钥文件路径”:
复制应用公钥2048.txt中的内容到点击“设置应用公钥”的弹出框中,保存:
商户私钥(merchant_private_key)
复制 应用私钥2048.txt 中的内容到merchant_private_key中。
支付宝公钥(alipay_public_key)
点击如上图连接,复制弹出框里面的内容到alipay_public_key。
若是这个设置不对,结果是:支付成功,可是验签失败。
若是是正式环境,须要上传到对应的应用中:
(4).服务器异步通知页面路径(notify_url)
若是没有更名,修改IP和端口号就能够了,我本身的以下:
http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp
复制代码
(5).页面跳转同步通知页面路径(return_url)
http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp
复制代码
测试用的支付宝买家帐户能够在“沙箱帐号”这个页面能够找到:
支付成功后,验签结果:
因为咱们使用的是沙箱测试环境,测试环境和正式上线的环境的网关是不同的,若是配置错误,会出现,appid错误的问题。配置以下:
https://github.com/OUYANGSIHAI/sihai-maven-ssm-alipay
整合到ssm同样,咱们须要像沙箱测试环境同样,须要修改支付的配置信息
主要包括如下的数据库表:
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='产品表 ';
复制代码
这里就不介绍了,这个只包括简单的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);
}
复制代码
**注意:**源代码最后给出。
同上,最后在项目源代码里可见。
以订单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);
}
复制代码
支付流程图
首先,启动项目后,输入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();
//商品描述,可空
String body = "用户订购商品个数:" + order.getBuyCounts();
// 该笔订单容许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的状况下,不管交易什么时候建立,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
String timeout_express = "1c";
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"timeout_express\":\""+ timeout_express +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}
复制代码
这段代码均可以在阿里支付的demo里面找到的,只须要复制过来,而后改改,整合到ssm环境便可。
上面就是将阿里支付宝支付整合到ssm的全过程了,若是还有什么疑问,能够留言或者私信我!
https://github.com/OUYANGSIHAI/sihai-maven-ssm-alipay