注意区分支付宝同步通知和异步通知
同步通知:不能判断是否支付成功,用来支付完成后跳转页面 异步通知:需要外网可以访问,支付宝发送post请求的地址,可以判断是否支付成功,从而做业务上的持久层操作
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
AliPayConfig 支付宝参数配置类
public class AlipayConfig {
public static String app_id = "";
public static String merchant_private_key ="";
public static String alipay_public_key ="";
public static String notify_url = "ip+端口/notify_url";
public static String return_url = "ip+端口/return_url";
public static String sign_type = "RSA2";
public static String charset = "utf-8";
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
}
AliPayUtil 工具类
@Slf4j
public class AliPayUtil {
public static AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
public static String getByte(String param) {
try {
return new String(param.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static boolean rsaCheckV1(HttpServletRequest request) {
Map<String, String> params = new HashMap<>();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = (String) iter.next();
String[] values = (String[]) 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 signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
return signVerified;
} catch (AlipayApiException e) {
log.debug("verify sigin error, exception is:{}", e);
return false;
}
}
}
AliPayController 接口 这里没有写业务层和持久层,只是做了模拟操作
package com.yyx.controller;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.*;
import com.yyx.config.AliPayConfig;
import com.yyx.utils.AliPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
@RestController
public class AliPayController {
@GetMapping(value = "/toAlipay", produces = "text/html; charset=UTF-8")
public String goAlipay() throws AlipayApiException {
AlipayClient alipayClient = AliPayUtil.alipayClient;
AlipayTradePagePayModel model = new AlipayTradePagePayModel();
model.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
model.setSubject("支付测试");
model.setTotalAmount("0.01");
model.setBody("买不了吃亏买不了上当");
model.setProductCode("FAST_INSTANT_TRADE_PAY");
AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();
pagePayRequest.setReturnUrl(AliPayConfig.return_url);
pagePayRequest.setNotifyUrl(AliPayConfig.notify_url);
pagePayRequest.setBizModel(model);
String result = alipayClient.pageExecute(pagePayRequest).getBody();
return result;
}
@GetMapping("/return_url")
public String return_url(HttpServletRequest request){
log.info("=》》》》》进入同步通知接口------");
boolean verifyResult = AliPayUtil.rsaCheckV1(request);
if (verifyResult) {
String out_trade_no = AliPayUtil.getByte(request.getParameter("out_trade_no"));
String trade_no = AliPayUtil.getByte(request.getParameter("trade_no"));
log.info("商户订单号,支付宝交易号", out_trade_no, trade_no);
return "支付成功页面";
}
return "支付失败页面";
}
@PostMapping("/notify_url")
public String notify_url(HttpServletRequest request){
log.info("=》》》》》支付成功, 进入异步通知接口------");
boolean flag = AliPayUtil.rsaCheckV1(request);
if (flag) {
String tradeStatus = AliPayUtil.getByte(request.getParameter("trade_status"));
String out_trade_no = AliPayUtil.getByte(request.getParameter("out_trade_no"));
String trade_no = AliPayUtil.getByte(request.getParameter("trade_no"));
String total_amount = AliPayUtil.getByte(request.getParameter("total_amount"));
log.info("交易状态,商户订单号,支付宝交易号,付款金额", tradeStatus, out_trade_no, trade_no, total_amount);
if (tradeStatus.equals("TRADE_SUCCESS")) {
}
return "success";
}
return "fail";
}
}
访问接口测试
http://localhost:8080/goAlipay
观察日志信息,看是否成功调起同步通知和异步通知
|