支付宝支付
对接流程
- 申请阿里支付官方企业账号
- 配置应用
- 签约产品
- 获取RSAKey(非对称加密)必须获得两个加密串:一个公钥,一个密钥
- SDK功能开发
- 业务对接
- 支付回调
- 支付组件
核心所需的参数
- APPID
- 商家私钥
- 支付宝公钥
- 支付回调地址
- 网关地址
- 加密签名算法RSA2
申请注册
官方网址:https://open.alipay.com/
配置开发信息
接口加签方式
发起支付订单案例
pom.xml
<?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>3.2.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.sin</groupId><artifactId>demozifubao</artifactId><version>0.0.1-SNAPSHOT</version><name>demozifubao</name><description>demozifubao</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java --><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.39.40.ALL</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml
server:port: 80
alipay:# 应用IDappId: # 应用私钥privateKey: # 支付宝公钥publicKey: gatewayUrl: https://openapi.alipay.com/gateway.do
AlipayController.java
package com.sin.demo.controller;import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 支付宝支付控制器类* @createTime 2024/4/16 9:24* @createAuthor SIN* @use 用于生成支付二维码*/
@RestController
@RequestMapping(value = "/pay")
@Slf4j
public class AlipayController {// 从配置文件中获取参数值@Value("${alipay.appId}")private String appId; // 支付宝支付者id@Value("${alipay.privateKey}")private String privateKey; // 商户应用私钥@Value("${alipay.publicKey}")private String publicKey; // 支付宝公钥@Value("${alipay.gatewayUrl}")private String gatewayUrl; // 支付宝网关URL/*** 请求生成支付二维码的接口** @return 生成的支付二维码地址* @throws AlipayApiException 支付宝API异常*/@GetMapping("/qrCode")public String generateQrCode() throws AlipayApiException {// 创建支付宝客户端DefaultAlipayClient client = new DefaultAlipayClient(gatewayUrl, appId, privateKey, "json", "UTF-8", publicKey, "RSA2");// 创建预下单请求对象AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();// 设置测试业务参数request.setBizContent("{\"out_trade_no\":\"20220416001\"," // 商户订单号+ "\"total_amount\":\"0.01\"," // 订单总金额+ "\"subject\":\"测试订单\"}"); // 订单标题// 执行预下单请求AlipayTradePrecreateResponse response = client.execute(request);// 判断预下单请求是否成功if (response.isSuccess()) {// 获取生成的支付二维码信息final String qrCode = response.getQrCode();// 打印二维码信息到控制台System.out.println("二维码信息:" + qrCode);// 记录生成支付二维码成功的日志log.info("生成支付二维码成功:{}", response.getBody());// 返回生成的支付二维码地址return "支付宝支付二维码地址:" + qrCode;} else {// 记录生成支付二维码失败的日志log.error("生成支付二维码失败:{}", response.getSubMsg());// 返回生成支付二维码失败的信息return "生成支付二维码失败:" + response.getSubMsg();}}
}
将获取到的请求地址发送给客户,客户在支付宝中进行支付