一、登录支付宝开放平台

这里要记住这里生成的秘钥等下写代码还要用的,如果忘了也没关系点这里就有了


到这里我们所需要的基本配值就配好了
二.开始整合springboot
2.1.创建一个boot的项目

<dependencies> <dependency> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-thymeleaf
artifactId>
dependency> <dependency> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-web
artifactId>
dependency> <dependency> <groupId>com.alipay.sdk
groupId> <artifactId>alipay-sdk-java
artifactId> <version>4.22.49.ALL
version>
dependency> <dependency> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-test
artifactId> <scope>test
scope>
dependency>
dependencies>
2.2.配置yml配置文件
server: port: 8800 spring: thymeleaf: cache: false suffix: .html
prefix: classpath:/templates/ alipay: url: https://openapi.alipaydev.com/gateway.do #此处请填写你当面付的APPID appid: 74366 #此处请填写你的应用私钥且转PKCS8格式 privateKey: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEnWye3jI2equTliM79t5lvT5lW3Lci8byiXS9r0O9lGHXYA/jyxr639Ui/Ub2iBvXBvBoEluPnFC1dPMCvIAhf2dJCtGTEXPWOkl1rd9plr57Zmo2w7QyQPyZle0wLmQdGlGXu4AQB8iyEvJTm9RmF55evFLWVQVNaV9I0p7uRKsUET8SMT7wV0f1TpXfjiSZ9AjX1JAD7rV+ue8Z6wOiEi4vjT8DBS6Z7bLeskEWQosL15cXwemHbA5uPkKw8ggYcKWbzP+4upJZXlnEEZubWTlxRWx7ByzHcyywog+6KXlLECJBu2ZDdPm+dYufP9AccSpPhQZ1iASJ3MdC+2PbAgMBAAECggEAXz+uh4Cx8f0zhMJeNjB+1fYRfb0sB4oBb6wYhDEA54ZOq+GcKBF2JGBl9kaSFHn3/P5H54ff6YrtURdZf0qigOy/4294zTfIykJ9Bp/GilEyKPGLMiBoKXwRscJEZMm9MEX5+pA6T8k3decz06dl65nlLsf2e2j/cPVQ8b4NVrZBKak/L3BQgAA6oB89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FAAOCAQ8AMIIBCgKCAQEAlaXJhOzn/mLe1OzK+p3R52r1/f7aNAGdyPpDBgb5tq9BeKCSpOpPK9GwGngyrwFxEJIX/77MlHn6Kis36gJBvzSxo6fejLyiwSMZFNRl+wGp/HnhDRbfUcrAxCppkpKp9KwP8lbwabGYIIJLK26l9s3Rw9FXf++/h94g2HgvfE1DaSgCNcG9T7bVZXD76pqvD8gxvtXe5MvYFbiTcP6WAUP/W5hNB+roZ9ZmgyK/8HBLljpnB9WVrjg1d/6TSWgCuu6k6aDlkZLQGibxUNb9RyXIYVXQYkagMYkzd8TUvBwaBq04y3yV2orpb2Y7HVSEjSsvid9JTpswzAuWl4rU1wIDAQAB #异步通知url(注意拦截器是否拦截) notifyUrl: http://localhost:8800/notify #支付成功后返回的页面地址 returnUrl: http://localhost:8800/return
2.3.开始写代码了
package com.mumuwei.alipay.controller; import com.mumuwei.alipay.util.AlipayUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class AlipayController {
private AlipayUtil alipayUtil; @Autowired public void setAlipayUtil(AlipayUtil alipayUtil) {
this.alipayUtil = alipayUtil; } @GetMapping("/") public String index(){
return "index"; } @PostMapping("/create") public String create(String id, String price, String title, Model model){
String pay = alipayUtil.pay(id, price, title); model.addAttribute("form", pay); return "pay"; } @GetMapping("/return") public String returnNotice(String out_trade_no, Model model){
String query = alipayUtil.query(out_trade_no); model.addAttribute("query", query); return "query"; } @PostMapping("/notify") public void notifyUrl(String trade_no, String total_amount, String trade_status){
System.err.println("支付宝订单编号:" + trade_no + ", 订单金额: " + total_amount + ",订单状态:" + trade_status); } }
package com.mumuwei.alipay.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.alipay.api.request.AlipayTradeQueryRequest; import com.alipay.api.response.AlipayTradePagePayResponse; import com.alipay.api.response.AlipayTradeQueryResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AlipayUtil {
@Value("${alipay.appid}") private String appid; @Value("${alipay.url}") private String url; @Value("${alipay.privateKey}") private String privateKey; @Value("${alipay.publicKey}") private String publicKey; @Value("${alipay.notifyUrl}") private String notifyUrl; @Value("${alipay.returnUrl}") private String returnUrl; public String pay(String id, String price, String title ){
AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2"); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); request.setNotifyUrl(notifyUrl); request.setReturnUrl(returnUrl); JSONObject bizContent = new JSONObject(); bizContent.put("out_trade_no", id); bizContent.put("total_amount", price); bizContent.put("subject", title); bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); request.setBizContent(bizContent.toString()); AlipayTradePagePayResponse response = null; String form = null; try {
response = alipayClient.pageExecute(request); form = response.getBody(); } catch (AlipayApiException e) {
e.printStackTrace(); } if(response.isSuccess()){
System.out.println("调用成功"); } else {
System.out.println("调用失败"); } return form; } public String query(String id){
AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2"); AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); JSONObject bizContent = new JSONObject(); bizContent.put("out_trade_no", id); request.setBizContent(bizContent.toString()); AlipayTradeQueryResponse response = null; String body = null; try {
response = alipayClient.execute(request); body = response.getBody(); } catch (AlipayApiException e) {
e.printStackTrace(); } if(response.isSuccess()){
System.out.println("调用成功"); } else {
System.out.println("调用失败"); } return body; } }
package com.mumuwei.alipay; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AlipayApplication {
public static void main(String[] args) {
SpringApplication.run(AlipayApplication.class, args); } }
2.4.html代码
index.html
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title
title>
head> <body> <h1>支付宝沙箱支付
h1> <form action="create" method="post"> 订单编号: <input type="text" name="id"> <br> 订单金额:<input type="text" name="price"> <br> 订单标题:<input type="text" name="title"> <br> <input type="submit" value="创建订单">
form>
body>
html>
notify.html
DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title
title>
head> <body> <h1>支付宝沙箱支付异步通知
h1>
body>
html>
pay.html
DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title
title>
head> <body> <div th:utext="${form}">
div>
body>
html>
query.html
DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title
title>
head> <body> <div th:text="${query}">
div>
body>
html>
三.运行代码

到这里我们boot整合沙盒支付就完成了,如果有小伙伴对上面的参数该兴趣的话看这里
支付宝开放平台
这里官网有详细的解释,还有demo实例
https://opendocs.alipay.com/open/270/106291

我已经把代码上到gitee上了
记得关注我后面我会出一个整合沙箱支付的项目
gitee的下载地址
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/228493.html原文链接:https://javaforall.net
