Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

This tutorial describes how to use the framework to achieve ssm Alipay function. This article is divided into two parts, namely "Alipay test environment to test the code" and "Alipay will be integrated into ssm framework" detailed code and graphic interpretation of their own practice when we must carefully read the documentation, did not talk much Let's start.

Alipay test environment to test the code

Source

https://github.com/OUYANGSIHAI/sihai-maven-ssm-alipay

1. Download the PC demo official website:

Download: https: //docs.open.alipay.com/270/106291/

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

2. Download and unzip into eclipse

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

readme.txt Please take a look.

Only a Java class configuration, the rest are JSP.

3. Configure AlipayConfig

(1) gold suit ant registered developer account (for free, unlike Apple will be charged)

Registered Address: https: //open.alipay.com, use your PayPal account to log scan code, improve personal information, select the type of service (I chose the self-study).

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

 

(2) and provided app_id gatewayUrl

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

Wherein the key generation needs its own, appID and payment gateway is a treasure to have a good, the gateway has dev words, show that for development and testing.

(3) Set the key

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 Click the "Generation", open interface as follows:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

Next week the key generation tools, unzip opens, select 2048 to generate the key:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 If you do not set too, the text is displayed as "public settings," I have set up here is nice.

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 Setting method, "the key to open the file path":

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 Copy the contents of the application in public 2048.txt to click on the "public settings" pop-up box, save:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

  • Merchant private key (merchant_private_key)
  • Copy the contents of the application in private 2048.txt to merchant_private_key in.
  • Alipay public key (alipay_public_key)

 

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

Click on the link above chart, copy the contents inside the pop-up box to alipay_public_key.

If this setting is wrong, the result is: successful payment, but failed to sign inspection.

If it is a formal environment, you need to be uploaded to the corresponding application:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

(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

4.测试运行

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

测试用的支付宝买家账户可以在"沙箱账"这个页面可以找到:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

支付成功后,验签结果:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

问题解决

由于我们使用的是沙箱测试环境,测试环境和正式上线的环境的网关是不一样的,如果配置错误,会出现,appid错误的问题。配置如下:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

源代码下载

链接: https://pan.baidu.com/s/1n6GbEJiMzoGWJrSw0bb2Cg 密码: zd9e

将支付宝支付整合到ssm框架

1、项目架构

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code
  • 项目架构:spring+springmvc+mybatis
  • 数据库:mysql
  • 部署环境:tomcat9.0
  • 开发环境:jdk9、idea
  • 支付:支付宝、微信

整合到ssm一样,我们需要像沙箱测试环境一样,需要修改支付的配置信息

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 2、数据库代码

主要包括以下的数据库表:

  • user:用户表
  • order:支付产生的订单
  • flow:流水账
  • product:商品表:用于模拟购买商品。
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 '支付方式 1:支付宝 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 '订单状态 10:待付款 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(支付流程)

支付流程图

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

首先,启动项目后,输入http://localhost:8080/,会进入到商品页面,如下:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

下面是页面代码

商品页面(products.jsp)

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

代码实现:

<%@ 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>

点击上面的购买,进入到订单页面

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 填写个数,然后点击生成订单,调用如下代码

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 根据SID(生成id的工具)等信息生成订单,保存到数据库。

进入到选择支付页面

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 调用了如下代码:

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

然后,我们选择支付宝支付,进入到了我们支付的页面了,大功告成!

Spring MVC + Spring + Mybatis achieve Alipay payment functions (Detailed graphics + Code

 

 

调用了如下代码:

/**
*
* @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 code can be found inside the Ali payment, just copied, and then change to change, the environment can be integrated into ssm.

Above is the Ali Alipay ssm integrated into the whole process.

Guess you like

Origin www.cnblogs.com/CQqf2019/p/10953785.html