matrix.business.pay.templates.AlipayTemplate Maven / Gradle / Ivy
package matrix.business.pay.templates;
import com.alibaba.druid.util.StringUtils;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import matrix.boot.common.exception.ServiceException;
import matrix.boot.common.utils.StringUtil;
import matrix.business.common.utils.RequestUtil;
import matrix.business.pay.builder.request.QueryRefundRequest;
import matrix.business.pay.builder.response.QueryPayResponse;
import matrix.business.pay.builder.response.QueryRefundResponse;
import matrix.business.pay.constants.AlipayConstant;
import matrix.business.pay.constants.PayConstant;
import matrix.business.pay.converter.AlipayConvert;
import matrix.business.pay.converter.HtmlConvert;
import matrix.business.pay.dto.RefundResultDto;
import matrix.business.pay.entity.MatrixBusinessPayEntity;
import matrix.business.pay.enums.PayMode;
import matrix.business.pay.properties.PayProperties;
import matrix.business.pay.dto.PayDto;
import matrix.business.pay.dto.RefundDto;
import matrix.business.pay.selector.AlipaySelector;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* 支付宝模板
*
* @author wangcheng
*/
@Slf4j
public class AlipayTemplate extends AbstractTemplate {
/**
* 支付配置参数
*/
@Resource
private PayProperties payProperties;
/**
* 支付宝客户端选择器
*/
private final AlipaySelector alipaySelector;
public AlipayTemplate(AlipaySelector alipaySelector) {
this.alipaySelector = alipaySelector;
}
@Override
protected String invokePay(String typeCode, PayMode payMode, PayDto payVo) {
try {
//获取用户配置
PayProperties.AlipayProperties alipayProperties = alipaySelector.getAlipayProperties(typeCode);
AlipayRequest extends AlipayResponse> alipayRequest = AlipayConvert.convertPayRequest(payMode, payVo,
payProperties.getNotifyUrl() + PayConstant.NOTIFY_PAY_URL_PREFIX + getName(),
StringUtil.isEmpty(payVo.getRedirectUrl()) ? alipayProperties.getRedirectUrl() : payVo.getRedirectUrl());
//获取客户端
AlipayClient alipayClient = alipaySelector.getAlipayClient(typeCode);
AlipayResponse resp;
if (PayMode.APP.getCode().equals(payMode.getCode())) {
resp = alipayClient.sdkExecute(alipayRequest);
} else {
resp = alipayClient.pageExecute(alipayRequest);
}
if (resp.isSuccess()) {
return resp.getBody();
}
throw new ServiceException("invoke alipay error");
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage(), e);
}
}
@Override
protected void invokeRefund(String typeCode, RefundDto refundVo) {
try {
AlipayTradeRefundRequest refundRequest = AlipayConvert.convertRefundRequest(refundVo);
//获取客户端
AlipayClient alipayClient = alipaySelector.getAlipayClient(typeCode);
AlipayTradeRefundResponse refundResponse = alipayClient.execute(refundRequest);
if (refundResponse.isSuccess() && AlipayConstant.REFUND_SUCCESS_FLAG.equalsIgnoreCase(refundResponse.getMsg())) {
//查询退款响应参数封装
QueryRefundResponse queryRefundResponse = new QueryRefundResponse(null, refundVo.getRefundId(),
null, null, null);
callbackChangeRefund(queryRefundResponse, PayConstant.REFUNDED);
} else {
//查询退款响应参数封装
QueryRefundResponse queryRefundResponse = new QueryRefundResponse(null, refundVo.getRefundId(),
null, null, null);
callbackChangeRefund(queryRefundResponse, PayConstant.REFUND_FAIL);
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
@Override
protected List invokeQueryPay(List payIds) {
List result = new ArrayList<>();
if (!CollectionUtils.isEmpty(payIds)) {
for (String payId : payIds) {
try {
//获取客户端
AlipayClient alipayClient = alipaySelector.getAlipayClient(payService.getTypeCode(payId));
AlipayTradeQueryRequest queryRequest = AlipayConvert.convertQueryPayRequest(payId);
AlipayTradeQueryResponse response = alipayClient.execute(queryRequest);
String payStatus = response.getTradeStatus();
if (AlipayConstant.PAY_SUCCESS_FLAG.equals(payStatus) || AlipayConstant.PAY_FINISHED_FLAG.equals(payStatus)) {
result.add(new QueryPayResponse(payId, response.getTradeNo(), response.getTotalAmount()));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
return result;
}
@Override
protected RefundResultDto invokeQueryRefund(List queryRefundRequests) {
RefundResultDto result = new RefundResultDto();
if (!CollectionUtils.isEmpty(queryRefundRequests)) {
for (QueryRefundRequest queryRefundRequest : queryRefundRequests) {
try {
//获取客户端
AlipayClient alipayClient = alipaySelector.getAlipayClient(refundService.getTypeCode(queryRefundRequest.getRefundId()));
AlipayTradeFastpayRefundQueryRequest queryRequest = AlipayConvert.convertQueryRefundRequest(queryRefundRequest);
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(queryRequest);
if (response.isSuccess()) {
if (AlipayConstant.REFUND_SUCCESS_FLAG.equalsIgnoreCase(response.getMsg()) && !StringUtils.isEmpty(response.getRefundAmount())) {
//退款成功
result.getSuccessRefundIds().add(queryRefundRequest.getRefundId());
} else {
//退款失败
result.getFailRefundIds().add(queryRefundRequest.getRefundId());
}
} else {
throw new ServiceException("alipay refund query fail" + response.getMsg());
}
} catch (Exception e) {
throw new ServiceException(e.getMessage(), e);
}
}
}
return result;
}
@Override
public String parsePayNotify(HttpServletRequest request) {
try {
Map contentMap = RequestUtil.parse(request);
//参数校验
this.payNotifyParamValid(contentMap);
//支付流水
String payId = contentMap.get("out_trade_no");
//类型编码
String typeCode = payService.getTypeCode(payId);
//获取配置资源
PayProperties.AlipayProperties alipayProperties = alipaySelector.getAlipayProperties(typeCode);
//签名校验
boolean signVerified = AlipaySignature.rsaCheckV1(contentMap,
alipayProperties.getPublicKey(), StandardCharsets.UTF_8.name(),
alipayProperties.getSignType());
if (!signVerified) {
log.debug("支付宝异步通知,签名校验失败:" + payId);
return AlipayConstant.FAIL_RETURN_CODE;
}
//第三方网关号
String outTradeNo = contentMap.get("trade_no");
//实际支付金额
String actualPrice = contentMap.get("total_amount");
//支付状态
String payStatus = contentMap.get("trade_status");
if (AlipayConstant.PAY_SUCCESS_FLAG.equals(payStatus) || AlipayConstant.PAY_FINISHED_FLAG.equals(payStatus)) {
callbackPaySuccess(new QueryPayResponse(payId, outTradeNo, actualPrice));
}
return AlipayConstant.SUCCESS_RETURN_CODE;
} catch (Exception e) {
throw new ServiceException(e);
}
}
@Override
public String parseRefundNotify(HttpServletRequest request) {
//支付宝无退款通知
return null;
}
@Override
public String obtainSDKPayInfo(String payId) {
MatrixBusinessPayEntity payEntity = payService.getPayEntityByPayId(payId);
return HtmlConvert.bodyConvert(payEntity.getBody());
}
@Override
public String getName() {
return "alipay";
}
@Override
public String getDefaultTypeCode() {
return alipaySelector.getDefaultTypeCode();
}
/**
* 支付通知参数校验
*
* @param contentMap 参数信息
*/
private void payNotifyParamValid(final Map contentMap) {
if (!contentMap.containsKey("trade_no")) {
throw new ServiceException("trade_no不能为空");
}
if (!contentMap.containsKey("out_trade_no")) {
throw new ServiceException("out_trade_no不能为空");
}
if (!contentMap.containsKey("total_amount")) {
throw new ServiceException("total_amount不能为空");
}
if (!contentMap.containsKey("sign")) {
throw new ServiceException("sign不能为空");
}
}
}