支付寶app支付java後臺流程demo
使用ssm框架實現支付寶支付功能。
支付寶測試環境程式碼測試
原始碼
https://github.com/OUYANGSIHAI/sihai-maven-ssm-alipay
1.下載電腦網站的官方demo:
下載:https://docs.open.alipay.com/270/106291/
2.下載解壓匯入eclipse
readme.txt請好好看一下。
只有一個Java配置類,其餘都是JSP。
3.配置AlipayConfig
(1) 註冊螞蟻金服開發者賬號(免費,不像蘋果會收取費用)
註冊地址:https://open.alipay.com ,用你的支付寶賬號掃碼登入
(2) 設定app_id和gatewayUrl
其中金鑰需要自己生成,appID和支付寶閘道器是已經給好的,閘道器有dev字樣,表明是用於開發測試。點那個設定 可以下載生成公鑰
注意部分:
點選上圖的生成方法 下載 跟你係統對用的 程式,然後生成 相應的 密匙
如果沒有設定過,此時顯示文字是"設定應用公鑰",我這裡是已經設定過得。
下一步:檢視你的公鑰把 公鑰的字串 複製過去ide AlipayConfig.java 裡面
以上的步驟基本上是 完成搭建 測試用的DEMO 僅此而已;
如果是正式環境,需要上傳到對應的應用中:
基本開發文件全部可以在這裡看自己可以梳理流程
SSM+alipay
原始碼下載
連結: 連結: https://pan.baidu.com/s/1pZodm4L_TKvGOQuBel2Quw 提取碼: ksbq
將支付寶支付整合到ssm框架
1、專案架構
-
專案架構:spring+springmvc+mybatis
-
資料庫:mysql
-
部署環境:tomcat7.0
-
開發環境:jdk8、idea
-
支付:支付寶、微信
整合到ssm一樣,我們需要像沙箱測試環境一樣,需要修改支付的配置資訊
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 '支付方式\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(支付流程)
<%@ 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>
首先,啟動專案後,輸入http://localhost:8080/,會進入到商品頁面,購買測試
點選上面的購買按鈕後跳轉,進入到訂單頁面
controller:
根據SID
(生成id的工具)等資訊生成訂單,儲存到資料庫。
支付寶支付 controller程式碼:
然後,我們選擇支付寶支付,進入到了我們支付的頁面了,大功告成!
/**
*
* @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;
}