密钥生产
![密钥](https://i-blog.csdnimg.cn/blog_migrate/4af6c4ac9dd382e0d448bc2227e799e1.png)
配置密钥
![配置](https://i-blog.csdnimg.cn/blog_migrate/9139ef5fea2ef55c315e4e8da3de06a4.png)
pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>alipay</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alipay</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<fastjson_version>1.2.28</fastjson_version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alipay</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.5.0.ALL</version>
</dependency>
<dependency>
<groupId>com.alipay</groupId>
<artifactId>alipay-trade-sdk</artifactId>
<version>20161215</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.110.ALL</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson_version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
index页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<H1>支付宝demo测试</H1>
<hr>
<form action="order/alipay" method="post">
订单 :<br>
<input type="text" name="out_trade_no"><br>
名称 :<br>
<input type="text" name="subject"><br>
金额 :<br>
<input type="text" name="total_amount"><br>
描述 :<br>
<input type="text" name="body"><br>
<input type="submit" value="支付宝支付">
</form>
</body>
</html>
请求订单 支付宝网关
package com.example.alipay.config;
import com.alibaba.fastjson.JSON;
import com.alipay.api.*;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.alipay.bean.AlipayBean;
import com.example.alipay.bean.Alipayconfig;
import com.example.alipay.config.PropertiesConfig;
public class AlipayUtil {
public static String connect(AlipayBean alipayBean) throws AlipayApiException {
Alipayconfig alipayconfig=new Alipayconfig();
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipaydev.com/gateway.do"
,
"2016101500691213" ,
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDP/WmHgxX+OPps4MY7FOpxs3SDCd94lcM2auu1yUHMYdCPu5MTr84+leYc6q8JZuz6ZKuJYwOhatkPqmv0nBfBHF+kzifKeAI/36peQ67j+3VTxpO9YB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UBAoGBAJoPbKE28mbYjlN1/8sMwK8X6ArZhbR9Fsg4wEIFAkOz60gU58Xk9tvEuO+1kJbIyAP7+jv/zC5F9Gw9tl+6IbJnn7SwjJFQkntvo3sWvS+sQyrchsarJcW9+6lIPTvxQxS2+d/D0npzXowuqo41fctRAu9Oq3g0zhZBEc+G0deFAoGAbEuyAeyCaTBClK+ksxsAbLsqJqUd41mznkYOXIrIGoKpQTCyZkDsk0tdtY+BDn92bzCxSSvPCO2Kw+5Z6y6CgBpNwemL25GpF0J7akKZmdZsqXTZGfrUZ3BdBz7YfuitWFaXe+2gWYOYt1dfklcI0QzWbKw0VM43Ae4xiBfoUuI=",
"json",
"utf-8" ,
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB" ,
"RSA2"
);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(PropertiesConfig.getKey("return_url"));
alipayRequest.setNotifyUrl(PropertiesConfig.getKey("notify_url"));
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
String result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}
}
service层
package com.example.alipay.service;
import com.alipay.api.AlipayApiException;
import com.example.alipay.bean.AlipayBean;
import org.springframework.stereotype.Service;
public interface PayService {
String aliPay(AlipayBean alipayBean) throws AlipayApiException;
}
serviceimpl层
package com.example.alipay.serviceimpl;
import com.alipay.api.AlipayApiException;
import com.example.alipay.bean.AlipayBean;
import com.example.alipay.service.PayService;
import com.example.alipay.config.AlipayUtil;
import org.springframework.stereotype.Service;
@Service(value = "payService")
public class PayServiceImpl implements PayService {
@Override
public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
return AlipayUtil.connect(alipayBean);
}
}
控制层
package com.example.alipay.controller;
import com.alipay.api.AlipayApiException;
import com.example.alipay.bean.AlipayBean;
import com.example.alipay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController()
@RequestMapping("order")
public class OrderController {
@Autowired
private PayService payService;
@PostMapping(value = "alipay")
public String alipay(String out_trade_no,String subject,String total_amount,String body) throws AlipayApiException {
AlipayBean alipayBean = new AlipayBean();
alipayBean.setBody(body);
alipayBean.setOut_trade_no(out_trade_no);
alipayBean.setTotal_amount(new StringBuffer().append(total_amount));
alipayBean.setSubject(subject);
return payService.aliPay(alipayBean);
}
}
注意不要写错商户id 密钥 支付宝公钥