项目中使用支付宝沙箱教程初级版
1.在项目中添加依赖
可以使用pom.xml,也可以直接添加jar包
<dependencies>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.34.47.ALL</version>
</dependency>
2.在项目中创建一个util包
并在util包下创建AlipayTools工具类
package com.qfedu.util;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qfedu.pojo.MyOrder;
public class AlipayTools {
public static String pay(MyOrder orders){
//准备接入的配置
String appId = "";//APPID
String serverUrl = "https://openapi.alipaydev.com/gateway.do";
String rsa2 = "RSA2";
String alipayPublicKey = "";//支付宝公钥
String charset = "UTF-8";
String privateKey = "";//应用私钥
String json = "json";
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, json, charset, alipayPublicKey, rsa2);
//request对象
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//商家的post请求接口
request.setNotifyUrl("这里填写自己的IP地址,也可以用内网穿透/PaySuccess");//回调--post请求
//商家的get请求接口
request.setReturnUrl("这里填写自己的IP地址,也可以用内网穿透/PaySuccess");//回调--get请求
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", "trade_no1114");
bizContent.put("total_amount", "888");
bizContent.put("subject", "测试商品");
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(bizContent.toJSONString());
String form= "" ;
try {
form = alipayClient.pageExecute(request).getBody(); //调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
return form;
}
}
具体使用教程可参考原文链接:
3.并在controller创建PayServlet类
package com.qfedu.controller;
import com.qfedu.pojo.MyOrder;
import com.qfedu.util.AlipayTools;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet(urlPatterns = "/PayServlet")
public class PayServlet extends BaseServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
MyOrder myOrder1 = new MyOrder(); //Myorder是订单列表的实体类对象
List<MyOrder> myorders = (List<MyOrder>) session.getAttribute("myorders");
for (MyOrder myorder : myorders) {
myOrder1.setUid(myorder.getUid());
myOrder1.setTotalpice(myorder.getTotalpice()); //总价
myOrder1.setShopname(myorder.getShopname()); //商品名称
myOrder1.setBack01(myorder.getBack01());//商品图片
}
String pay = AlipayTools.pay(myOrder1);
resp.getWriter().write(pay);
}
}
再创建PaySuccessServlet类
package com.qfedu.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/PaySuccess")
public class PaySuccessServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("success.jsp");
}
}
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>支付成功</h1>
</body>
</html>
在订单页面中
<div style="width: 121px">
<a href="/PayServlet"><button style=" width: 120px;height: 30px;background-color: #ff6700;border: none">立即付款</button></a>
<button style="width: 120px;height: 30px;border: 1px solid lightgrey">订单详情</button>
<button style="width: 120px;height: 30px;border: 1px solid lightgrey">联系客服</button>
</div>