一、支付环境准备
- 支付宝的公钥和私钥
- 支付的网关
- 支付的APPID
-
1、配置沙箱应用环境
- 1、打开支付宝开放平台,官网:支付宝开放平台
- 2、登录个人账户,然后点击控制台找到里面的沙箱
- 3、这里能够找到
APPID
和支付宝网关地址
和密钥
-
二、设置内网穿透环境
- 我用的花生壳:花生壳官网|动态域名|免费域名|域名注册|向日葵远程控制|远程桌面-贝锐开放的互联网应用服务引领者
-
三、实际后端开发
-
1、引入依赖
-
</dependency> <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk --> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.10.0.ALL</version> </dependency> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.22.110.ALL</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.20</version> </dependency>
-
2、服务端代码配置
-
接口环境调试接口是,开发者需要调整如下代码配置
- 支付宝网关地址:支付宝 - 网上支付 安全快速!
- APPID:支付宝开发平台中的APPID
- 3、客户端代码配置
-
AlipayConfig.java:package com.cxs.biyesheji.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:alipay.properties") public class AlipayConfig { @Value("${alipay.gatewayUrl}") private String gatewayUrl; @Value("${alipay.appId}") private String appId; @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 getGatewayUrl() { return gatewayUrl; } public String getAppId() { return appId; } public String getPrivateKey() { return privateKey; } public String getPublicKey() { return publicKey; } public String getNotifyUrl() { return notifyUrl; } public String getReturnUrl() { return returnUrl; } }
-
AlipayController.java:
package com.cxs.biyesheji.controller; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradePagePayRequest; import com.cxs.biyesheji.config.AlipayConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.UUID; @Controller public class AlipayController { private final AlipayConfig alipayConfig; @Autowired public AlipayController(AlipayConfig alipayConfig) { this.alipayConfig = alipayConfig; } @PostMapping(value = "/alipay", produces = "text/html; charset=UTF-8") @ResponseBody public String pay(@RequestParam("amount") String amount) { try { // Initialize AlipayClient AlipayClient alipayClient = new DefaultAlipayClient( alipayConfig.getGatewayUrl(), alipayConfig.getAppId(), alipayConfig.getPrivateKey(), "json", "UTF-8", alipayConfig.getPublicKey(), "RSA2" ); // Set request parameters AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(alipayConfig.getReturnUrl()); alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl()); // Generate a unique order number using UUID String orderNo = UUID.randomUUID().toString(); alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderNo + "\"," + "\"total_amount\":\"" + amount + "\"," + "\"subject\":\"订单支付\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); // Send the payment request and get the HTML content of the payment page String form = alipayClient.pageExecute(alipayRequest).getBody(); // Return the HTML content of the payment page to the frontend return form; } catch (AlipayApiException e) { System.err.println("Alipay API exception: " + e.getMessage()); e.printStackTrace(); // Return error message to the frontend return "支付初始化失败,请重试!原因:" + e.getMessage(); } } }
- alipay.properties:
# UTF-8 encoding declaration # 文件编码为 UTF-8 alipay.appId=支付的APPID;
alipay.privateKey=自己的私钥
alipay.publicKey=公钥
alipay.gatewayUrl=支付宝网关地址
alipay.notifyUrl=内网穿透的网址
alipay.returnUrl=内网穿透的网址