springboot整合支付宝沙箱支付案例
时间: 2023-08-23 10:08:22 浏览: 171
SpringBoot集成支付宝沙箱支付(支付、退款)
很高兴能为您提供这篇文章,下面是springboot整合支付宝沙箱支付的案例。
1. 首先,您需要注册一个支付宝账号,并申请沙箱环境的权限。
2. 在支付宝开放平台申请应用,并获取应用的AppID、应用私钥和支付宝公钥。
3. 在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.0.ALL</version>
</dependency>
```
4. 创建一个AliPayConfig类,用于配置支付宝相关参数。
```
@Configuration
public class AliPayConfig {
//支付宝网关
private String gatewayUrl;
//商户应用id
private String appid;
//RSA私钥
private String privateKey;
//字符编码格式
private String charset;
//支付宝公钥
private String alipayPublicKey;
//签名类型
private String signType;
@Value("${ali.pay.gatewayUrl}")
private String aliPayGatewayUrl;
@Value("${ali.pay.appid}")
private String aliPayAppid;
@Value("${ali.pay.privateKey}")
private String aliPayPrivateKey;
@Value("${ali.pay.charset}")
private String aliPayCharset;
@Value("${ali.pay.alipayPublicKey}")
private String aliPayAlipayPublicKey;
@Value("${ali.pay.signType}")
private String aliPaySignType;
@PostConstruct
public void init() {
this.gatewayUrl = aliPayGatewayUrl;
this.appid = aliPayAppid;
this.privateKey = aliPayPrivateKey;
this.charset = aliPayCharset;
this.alipayPublicKey = aliPayAlipayPublicKey;
this.signType = aliPaySignType;
}
@Bean
public AlipayClient alipayClient() {
return new DefaultAlipayClient(gatewayUrl, appid, privateKey, "json", charset, alipayPublicKey, signType);
}
}
```
5. 创建一个AliPayService类,用于处理支付宝相关业务逻辑。
```
@Service
public class AliPayService {
@Autowired
private AlipayClient alipayClient;
@Value("${ali.pay.notifyUrl}")
private String aliPayNotifyUrl;
public String aliPay(String orderId, String totalAmount, String subject) {
String result = "";
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl("http://localhost:8080/returnUrl");
alipayRequest.setNotifyUrl(aliPayNotifyUrl);
alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderId + "\","
+ "\"total_amount\":\"" + totalAmount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
try {
result = alipayClient.pageExecute(alipayRequest).getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
return result;
}
public void aliPayNotify(Map<String, String> params) {
boolean verifyResult = false;
try {
verifyResult = AlipaySignature.rsaCheckV1(params, alipayClient.getAlipayPublicKey(), "UTF-8", "RSA2");
} catch (AlipayApiException e) {
e.printStackTrace();
}
if (verifyResult) {
String outTradeNo = params.get("out_trade_no");
String tradeNo = params.get("trade_no");
String tradeStatus = params.get("trade_status");
if ("TRADE_SUCCESS".equals(tradeStatus)) {
//支付成功
}
}
}
}
```
6. 在controller中调用AliPayService类进行支付宝支付。
```
@Controller
public class AliPayController {
@Autowired
private AliPayService aliPayService;
@RequestMapping("/aliPay")
@ResponseBody
public String aliPay(HttpServletRequest request, HttpServletResponse response) throws IOException {
String orderId = "201908140020000001";
String totalAmount = "0.01";
String subject = "测试支付";
String result = aliPayService.aliPay(orderId, totalAmount, subject);
return result;
}
@RequestMapping("/returnUrl")
public String returnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = iter.next();
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
aliPayService.aliPayNotify(params);
return "success";
}
}
```
以上就是springboot整合支付宝沙箱支付的案例,希望能对您有所帮助。
阅读全文