1.上蚂蚁金服官网注册开发者账号(https://open.alipay.com/platform/home.htm)
2.生成RSA公钥,密钥(这里使用官方推荐工具生成 https://docs.open.alipay.com/291/105971)
在官方demo的基础上修改代码或自行编写
demo下载地址 https://docs.open.alipay.com/270/106291/
1.复制demo里的AlipayConfig.java到项目,并将配置信息写进AlipayConfig.java(准备阶段里的APPID,公钥,密钥以及同步、异步回调地址都在这里配置)
AlipayConfig.java
package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "这里是你的APPID号";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "这里是你应用公钥所对应的私钥";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key = "这里是支付宝公钥";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = “这里是异步回调的地址(必须是能被外网访问的地址),用于业务逻辑的处理”;
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "这里是同步回调的地址,用于将结果回显给用户";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
// 支付宝网关
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 支付宝网关
public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.复制demo里的alipay.trade.page.pay.jsp到项目,并设置支付订单参数,如:商户订单号,金额,商品名等(具体有哪些参数可在开发文档查看)。
alipay.trade.page.pay.jsp
<%@page import="org.springframework.beans.factory.annotation.Autowired"%>
<%@page import="javax.annotation.Resource"%>
付款
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="com.alipay.config.*"%>
<%@ page import="com.alipay.api.*"%>
<%@ page import="com.alipay.api.request.*"%>
<%@ page import="com.shop.service.tempService" %>
<%@ page import="com.shop.service.impl.tempServiceImpl" %>
<%@ page import="com.shop.pojo.temp" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%
//获得初始化的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 =new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额,必填
String total_amount =new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
//订单名称,必填
String subject =new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
//商品描述,可空
String body =new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
//自定义参数(存入数据库,不发送给Ali)
String user_id = new String(request.getParameter("WIDuser_id").getBytes("ISO-8859-1"),"UTF-8");
String article_id=new String(request.getParameter("WIDarticle_id").getBytes("ISO-8859-1"),"UTF-8");
String num =new String(request.getParameter("WIDnum").getBytes("ISO-8859-1"),"UTF-8");
String address=new String(request.getParameter("WIDaddress").getBytes("ISO-8859-1"),"UTF-8");
/*
发送的参数,必须符合命名规范,详情见API帮助文档
out_trade_no:商户订单id
total_amout:付款金额
subject:订单名
body:商品名
quantity:数量
*/
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
//生成预订单
temp x = new temp();
x.setId(out_trade_no);
x.setUser_id(Integer.parseInt(user_id));
x.setArticle_id(Integer.parseInt(article_id));
x.setNum(Integer.parseInt(num));
x.setPay_money(Float.valueOf(total_amount));
x.setAddress(address);
x.setTime(new Date());
//获取tempServiced接口
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
tempService tempservice = (tempService) wac.getBean("tempService");
if(tempservice.getTemp(out_trade_no)==null)
tempservice.addTemp(x);
//输出
out.println(result);
%>
3.请求同步回调,复制demo里的return_url.jsp到项目并修改
同步回调
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
电脑网站支付return_url
<%@ page import="java.util.*"%>
<%@ page import="java.util.Map"%>
<%@ page import="com.alipay.config.*"%>
<%@ page import="com.alipay.api.*"%>
<%@ page import="com.alipay.api.internal.util.*"%>
<%
/* *
* 功能:支付宝服务器同步通知页面
* 日期:2017-03-30
* 说明:
* 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
* 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
*************************页面功能说明*************************
* 该页面仅做页面展示,业务逻辑处理请勿在该页面执行
*/
//获取支付宝GET过来反馈信息
Map params = new HashMap();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
//——请在这里编写您的程序(以下代码仅作参考)——
if(signVerified) {
//商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
out.println("trade_no:"+trade_no+"
out_trade_no:"+out_trade_no+"
total_amount:"+total_amount);
}else {
out.println("验签失败");
}
//——请在这里编写您的程序(以上代码仅作参考)——
%>
4.请求异步回调,根据之前回调地址编写对应的请求响应代码做业务逻辑处理
异步回调
由于Controller只是简单的调用service函数而已,所以这里直接放service函数
@Override
public void notify_url(HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
//获取支付宝POST过来反馈信息
Map params = new HashMap();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
//签名认证
boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
if(signVerified) {//验证成功
//获取认证状态
String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
//成功支付后在数据库添加记录
if (trade_status.equals("TRADE_SUCCESS")){
//业务逻辑处理
String order_id = params.get("out_trade_no");
//获取预订单数据
temp perorder = tempmapper.getTemp(order_id);
//生成真正的订单
orders x=new orders();
x.setId(perorder.getId()); //订单id
x.setUser_id(perorder.getUser_id());
x.setArticle_id(perorder.getArticle_id());
x.setNum(perorder.getNum());
x.setPay_money(perorder.getPay_money());
x.setAddress(perorder.getAddress());
x.setTime(perorder.getTime());
ordersmapper.insertOrder(x);
//删除预订单缓存
tempmapper.deleteTemp(order_id);
//修改库存,销量
articles article = articlesmapper.getArticle(x.getArticle_id());
article.setHot(article.getHot()+x.getNum());
article.setNum(article.getNum()-x.getNum());
articlesmapper.updateArticle(article);
}
}
}