目录
一、支付宝沙箱环境介绍
二、沙箱环境准备
2.1 注册入驻支付宝开放平台
2.2 配置沙箱环境
2.3 沙箱应用
三、SpringBoot 结合 支付宝沙箱
3.1 pom.xml
3.2 application.yml
3.3 对应的配置类
3.4 支付接口,和支付的回调接口
3.5 支付页面 apy.html
3.6 支付成功的页面 payok.html
四、测试
4.1 访问http://localhost:8080/pay.html
一、支付宝沙箱环境介绍
沙箱环境是支付宝开放平台为开发者提供的安全低门槛的测试环境,开发者在沙箱环境中调用接口无需具备所需的商业资质,无需绑定和开通产品,同时不会对生产环境中的数据造成任何影响。合理使用沙箱环境,可以让研发流程与商业流程并行,降低联调资损风险,加速项目的交付。更为详细的说明可以看官方文档:https://opendocs.alipay.com/common/02kkv7
二、沙箱环境准备
2.1 注册入驻支付宝开放平台
登录支付宝开放平台,支付宝登录平台,找到开发接入入驻为开发者。
2.2 配置沙箱环境
进入到开发者的控制台,找到最下方的开发者工具推荐 ,点击沙箱。
2.3 沙箱应用
-
获取支付参数信息
打开沙箱应用后,需要拿到3个参数,appId,应用私钥,支付宝公钥,用于对接支付接口使用。
支付宝网关里的https://不要复制,会帮我们自动拼接
三、SpringBoot 结合 支付宝沙箱
3.1 pom.xml
<!--引入支付宝sdk -->
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.2</version>
</dependency>
3.2 application.yml
spring:
application:
name:
SpringBoot_Pay
alipay:
appId: 9021000137619573
appPrivateKey: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC43Mr6YjCPwWDhb+d+2fxatYjDeJjrNKj78KY6oD2hatVAWsQvG/dW9Sr3bouit2rvOc6AQIem7CJQ3gQEvw15dn8Q4Uh+LWxBB5aujCjgr2Fvp0ELqHlnKNUgxACPgzfqLCpIYGcGI6zqrrGtA2PCD3TyjNbgyo18c4OZLLWhpokr2Ml7fI/F0h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/J9aitPbFKZuXS7IaYe/BsPHZqqwMg+lMd5z5IVww5F+WFCeTahqNktrkX8k9DNbjKZkEOBBGfBnDcDOyt4qWK2n/7F8YFEtR6/ZmCnyPMKZnKG5um6kGPZQZNuB/iBsbbA8eb/DFrAoGAdM5Wn1q8G6uB7QsxHFfjir6kbvfrf3WMPDvIIDTKS49t0WucVvLPNtIPGCSb99MEWOEZvWvMhn0RmCZNHWX7Fy4HB4FcJW57aWvIDKwxEcI9gdy5Vmg0uRIsd3WPzcZxbTCERUvIXRgh/tacaTq9/wPTEq1vp0AGYXoJHQslxLs=
publicKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArEEt6BXEXKinvjjQooptYjjyJIy+4DHhxBK2BOMUWs2Ol9xQStPrkCz3gVwZg0yM1nIvv/G970u8siXL7GN42CbPEw3hVimCjiclDn1qMaurTakOUtUT0RygF118wThny40rZYsMbnDs7uCV4yynI8/5dEhHvUfe4pZm3by8Wy+aNjlX9Fwn1FbRa8aWnZmglcEp92xGkIyhC7Yc/DJA7J4xc44mgWVcHy8lNJv7Cznoehsq9dAew5mpjYvRCr1PIwGHw8D9Tkz/T1gOREiiejaBwF3zZpEUhq85t1GBoyPKwGtsb2Ve8ribP3EfKbqORgOc9ay7CnMkz8KRTRuK4wIDAQAB
# 回调函数的地址 http://localhost:8080/api/alipay/notify
# 要使用公网地址,用路由侠进行内网穿透
notifyUrl: https://o4662e9430.vicp.fun/api/alipay/notify
gatewayHost: openapi-sandbox.dl.alipaydev.com
3.3 对应的配置类
package com.by.config;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@ConfigurationProperties(prefix = "alipay")
@Data
public class AlipayConfig {
// 应用Id
private String appId;
// 应用私钥
private String appPrivateKey;
// 支付宝公钥
private String publicKey;
// 回调接口路径,要进行内网穿透
private String notifyUrl;
// 支付宝网关地址:配置文件中不要写https://,会自动补全
private String gatewayHost;
@PostConstruct
public void init(){
Config config = new Config();
// 基础配置
config.protocol = "https";
config.gatewayHost = this.gatewayHost;// 支付宝网关地址
config.signType = "RSA2";
// 业务配置
config.appId = this.appId;
config.merchantPrivateKey = this.appPrivateKey;
config.alipayPublicKey = this.publicKey;
config.notifyUrl = this.notifyUrl;
// 将配置信息, 添加到相应的工厂类
Factory.setOptions(config);
System.out.println("支付宝初始化配置完成");
}
}
3.4 支付接口,和支付的回调接口
package com.by.controller;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/alipay")
public class PayController {
/**
* 订单支付接口, 核心是调用支付宝的 Factory.Payment.Page().pay() 方法
* @param subject 支付对象信息
* @param outTradeNo 订单号
* @param totalAmount 订单金额
* @param returnUrl 支付成功以后返回的页面地址
* @return
*/
@GetMapping
public String pay(String subject, String outTradeNo, String totalAmount,String returnUrl) throws Exception {
AlipayTradePagePayResponse response = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount, returnUrl);
String body = response.getBody();
return body;
}
/**
* 支付回调接口,如果支付成功,支付宝将会请求此接口,将成功信息返回
*/
@PostMapping("/notify")
public String notify(@RequestParam Map parameterMap) throws Exception {
String tradeStatus = parameterMap.getOrDefault("trade_status","").toString();
if (tradeStatus.trim().equals("TRADE_SUCCESS")) {
// 验证请求的有效性
if (Factory.Payment.Common().verifyNotify(parameterMap)) {
System.out.println("通过支付宝的验证");
System.out.println("订单id:" + parameterMap.get("out_trade_no"));
}else {
System.out.println("支付验证不通过");
}
}
return "success";
}
}
3.5 支付页面 apy.html
传递的参数用于支付接口接收 ,其中里的
subject 商品名称或交易标题
outTradeNo 商户订单号
totalAmount 订单总金额,单位为元
returnUrl 订单返回URL,本来应该为http://localhost:8080/payok.html?orderId=1001,要传递一个下单id,因为有问号,所以要进行url编码
<body>
<a href="/api/alipay?subject=商品支付&outTradeNo=3123&totalAmount=16.8&returnUrl=http%3A%2F%2Flocalhost%3A8080%2Fpayok.html%3ForderId%3D1001" class="a">
<img src="https://zhengxin-pub.bj.bcebos.com/logopic/a9936a369e82e0c6c42112674a5220e8_fullsize.jpg?x-bce-process=image/resize,m_lfit,w_200" alt="支付宝支付">
<h2>支付宝支付</h2>
</a>
</body>
3.6 支付成功的页面 payok.html
<div class="alert alert-primary" role="alert">
支付成功
</div>
四、测试
4.1 访问http://localhost:8080/pay.html