springboot整合支付宝沙箱支付代码
时间: 2023-08-13 15:03:00 浏览: 113
好的,以下是Spring Boot整合支付宝沙箱支付的代码示例:
```java
@RestController
@RequestMapping("/alipay")
public class AlipayController {
@Autowired
private AlipayService alipayService;
/**
* 获取支付宝支付二维码
*
* @param orderNo 订单号
* @param amount 金额
* @return 二维码图片base64编码
*/
@GetMapping("/qrCode")
public String getQrCode(@RequestParam String orderNo, @RequestParam BigDecimal amount) {
return alipayService.getQrCode(orderNo, amount);
}
/**
* 处理支付宝异步通知
*
* @param request 请求参数
* @return
*/
@PostMapping("/notify")
public String handleNotify(HttpServletRequest request) {
return alipayService.handleNotify(request);
}
}
```
AlipayService的实现示例:
```java
@Service
public class AlipayServiceImpl implements AlipayService {
@Value("${alipay.appId}")
private String appId;
@Value("${alipay.gatewayUrl}")
private String gatewayUrl;
@Value("${alipay.privateKey}")
private String privateKey;
@Value("${alipay.publicKey}")
private String publicKey;
@Value("${alipay.notifyUrl}")
private String notifyUrl;
@Autowired
private AlipayClient alipayClient;
@Override
public String getQrCode(String orderNo, BigDecimal amount) {
try {
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
request.setNotifyUrl(notifyUrl);
request.setBizContent("{" +
"\"out_trade_no\":\"" + orderNo + "\"," +
"\"total_amount\":\"" + amount + "\"," +
"\"subject\":\"订单支付\"," +
"\"store_id\":\"001\"," +
"\"timeout_express\":\"90m\"}");
AlipayTradePrecreateResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getQrCode();
} else {
log.error("获取支付宝支付二维码失败:{}", response.getSubMsg());
return null;
}
} catch (AlipayApiException e) {
log.error("获取支付宝支付二维码异常:{}", e.getMessage());
return null;
}
}
@Override
public String handleNotify(HttpServletRequest request) {
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);
}
try {
boolean verifyResult = AlipaySignature.rsaCheckV1(params, publicKey, "UTF-8", "RSA2");
if (verifyResult) {
// 处理异步通知
String tradeStatus = params.get("trade_status");
String outTradeNo = params.get("out_trade_no");
if ("TRADE_SUCCESS".equals(tradeStatus)) {
// 支付成功
// TODO 处理支付成功的业务逻辑
} else {
// 支付失败
// TODO 处理支付失败的业务逻辑
}
return "success";
} else {
log.error("支付宝异步通知验签失败:{}", params.toString());
return "fail";
}
} catch (AlipayApiException e) {
log.error("支付宝异步通知验签异常:{}", e.getMessage());
return "fail";
}
}
}
```
在application.yml中添加支付宝相关配置:
```yml
alipay:
appId: your_app_id
gatewayUrl: https://openapi.alipaydev.com/gateway.do
privateKey: your_private_key
publicKey: your_public_key
notifyUrl: your_notify_url
```
其中,notifyUrl是支付宝异步通知的回调地址,需要在支付宝开放平台中配置。
以上代码仅供参考,具体实现还需要根据自己的业务需求进行调整。
阅读全文