官方文档
https://opendocs.alipay.com/open/204/105297
开发之前建议先梳理下支付流程
整个支付流程中java后端需要提供两个接口,创建订单接口,和回调接口;
如果需要交易关闭、交易退款接口请参考官方文档;(如果创建订单接口可以成功,其他接口也就很简单了)
开发前准备
1.引入依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.15.6.ALL</version>
</dependency>
2.关键参数生成
appId:应用id(参考https://opendocs.alipay.com/open/204/105297 创建应用)
alipayPublicKeySha:支付宝公钥,由支付宝生成。(参考:https://opendocs.alipay.com/open/291/105971 生成秘钥)
appPrivateKey:开发者私钥,由开发者自己生成。(参考:https://opendocs.alipay.com/open/291/106074 生成秘钥验签)
3.创建订单接口
3.1阿里支付配置类
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
/**
* 阿里支付配置类
*/
@Component
@PropertySource(value = {"classpath:alipay.properties"})
public class AliPayConfig {
//支付宝网管地址
@Value("${openapi}")
private String openapi;
//应用id
@Value("${aliappId}")
private String appId;
//请求格式,固定值json
@Value("${type}")
private String format;
//字符集
@Value("${charset}")
private String charset;
//签名类型
@Value("${signType}")
private String signType;
//异步通知地址
@Value("${alinotifyUrl}")
private String notifyUrl;
//支付宝公钥,由支付宝生成。
@Value("${alipayPublicKeySha}")
private String alipayPublicKeySha;
//开发者私钥,由开发者自己生成。
@Value("${appPrivateKey}")
private String appPrivateKey;
}
3.2创建订单方法
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.smt.amblyopia.nd.comm.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class AliPay {
@Autowired
private AliPayConfig aliPayConfig;
/**
*
* @param body 支付信息描述 如 商品a 商品b (商品名称)
* @param subject 标题 例如:购买商品a
* @param outtradeno 商户唯一订单号
* @param amount 订单价格
* @return
*/
public String aliCreateOrder(String body , String subject,String outtradeno, String amount ){
AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig.getOpenapi(),
aliPayConfig.getAppId(),aliPayConfig.getAppPrivateKey(),
aliPayConfig.getFormat(),aliPayConfig.getCharset(),
aliPayConfig.getAlipayPublicKeySha(),aliPayConfig.getSignType());
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(body);
model.setSubject(subject);
model.setOutTradeNo(outtradeno);
model.setTimeoutExpress("30m");
model.setTotalAmount(amount);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
request.setNotifyUrl(aliPayConfig.getNotifyUrl());
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
//直接返回给前端,无需再做处理。
return response.getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
log.error("支付宝创建订单出错:{}" , e);
throw new BusinessException("支付宝创建订单出错");
}
}
}
3.3本地新增订单
请求支付宝创建订单成功以后,本地也新增一条订单记录(此时订单状态为未支付状态);
因为每个人业务需求不一样,此处不写本地创建订单代码;
4.支付宝异步回调接口
4.1 返回状态枚举类
/**
* 支付宝回调接口状态码
* TRADE_FINISHED 交易完成 true(触发通知)
* TRADE_SUCCESS 支付成功 true(触发通知)
* WAIT_BUYER_PAY 交易创建 false(不触发通知)
* TRADE_CLOSED 交易关闭 true(触发通知)
*/
public enum AliPayTradeStatusEnums {
TRADE_FINISHED("TRADE_FINISHED","交易完成"),
TRADE_SUCCESS("TRADE_SUCCESS","支付成功"),
WAIT_BUYER_PAY("WAIT_BUYER_PAY","交易创建"),
TRADE_CLOSED("TRADE_CLOSED","交易关闭")
;
private String type;
private String value;
AliPayTradeStatusEnums(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public String getValue() {
return value;
}
}
4.2异步接口处理方法
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.smt.amblyopia.nd.comm.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Component
@Slf4j
public class AliPay {
@Autowired
private AliPayConfig aliPayConfig;
public Map<String , String> callBack(HttpServletRequest request){
//获取支付宝POST过来反馈信息
boolean flag = false;
Map<String , String> result = new HashMap<>();
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);
}
log.info("callBack params:{}", JSON.toJSONString(params));String sign_type)
try {
flag = AlipaySignature.rsaCheckV1 (params,aliPayConfig.getAlipayPublicKeySha(),aliPayConfig.getCharset() ,"RSA2");
}catch (AlipayApiException e){
log.error("验证失败:{}",e);
}
if(!flag){
throw new BusinessException("验证失败");
}
String outTradeNo = params.get("out_trade_no");
//返回状态 为1.1枚举类状态
String tradeStatus = params.get("trade_status");
result.put("outTradeNo" ,outTradeNo );
result.put("tradeStatus" ,tradeStatus );
log.info("result:" , JSON.toJSONString(result));
return result;
}
}
4.3本地订单处理
根据 tradeStatus 状态,对照AliPayTradeStatusEnums 类中状态,更新创建订单时本地储存订单的状态;
此处更新的订单,为1.3步骤中本地创建的订单;