matrix.business.pay.converter.AlipayConvert Maven / Gradle / Ivy
package matrix.business.pay.converter;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.request.*;
import matrix.boot.based.utils.JacksonUtil;
import matrix.boot.common.exception.ServiceException;
import matrix.business.pay.builder.request.QueryRefundRequest;
import matrix.business.pay.builder.request.alipay.AlipayPayRequest;
import matrix.business.pay.builder.request.alipay.AlipayQueryPayRequest;
import matrix.business.pay.builder.request.alipay.AlipayQueryRefundRequest;
import matrix.business.pay.builder.request.alipay.AlipayRefundRequest;
import matrix.business.pay.constants.AlipayConstant;
import matrix.business.pay.enums.PayMode;
import matrix.business.pay.dto.PayDto;
import matrix.business.pay.dto.RefundDto;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* 支付宝信息转换
* @author wangcheng
*/
public class AlipayConvert {
/**
* 转换为支付宝请求参数
* @param payMode 支付方式
* @param payVo 支付参数
* @param notifyUrl 通知url
* @param redirectUrl 跳转url
* @return 支付宝请求参数
*/
public static AlipayRequest extends AlipayResponse> convertPayRequest(PayMode payMode, PayDto payVo, String notifyUrl, String redirectUrl) {
AlipayPayRequest payRequest = new AlipayPayRequest()
.setOutTradeNo(payVo.getPayId())
.setTotalAmount(payVo.getPrice().setScale(2, RoundingMode.HALF_UP).toPlainString())
.setSubject(payVo.getTitle())
.setBody(payVo.getDesc());
AlipayRequest extends AlipayResponse> alipayRequest = null;
if (PayMode.PC.equals(payMode) || PayMode.QrCode.equals(payMode)) {
alipayRequest = new AlipayTradePagePayRequest();
if (PayMode.QrCode.equals(payMode)) {
payRequest.setQrPayMode(AlipayConstant.QR_PAY_MODE);
payRequest.setQrcodeWidth(String.valueOf(payVo.getQrCodeWidth()));
}
payRequest.setProductCode(AlipayConstant.ALIPAY_PC_PRODUCT_CODE);
((AlipayTradePagePayRequest) alipayRequest).setBizContent(JacksonUtil.toJsonString(payRequest));
} else if (PayMode.H5.equals(payMode)) {
alipayRequest = new AlipayTradeWapPayRequest();
payRequest.setProductCode(AlipayConstant.ALIPAY_H5_PRODUCT_CODE);
((AlipayTradeWapPayRequest) alipayRequest).setBizContent(JacksonUtil.toJsonString(payRequest));
} else if (PayMode.APP.equals(payMode)) {
alipayRequest = new AlipayTradeAppPayRequest();
payRequest.setProductCode(AlipayConstant.ALIPAY_APP_PRODUCT_CODE);
((AlipayTradeAppPayRequest) alipayRequest).setBizContent(JacksonUtil.toJsonString(payRequest));
}
if (alipayRequest == null) {
throw new ServiceException("No Pay Mode!");
}
alipayRequest.setNotifyUrl(notifyUrl);
alipayRequest.setReturnUrl(redirectUrl);
return alipayRequest;
}
/**
* 转换为支付宝退款请求
* @param refundVo 退款参数
* @return 支付宝退款请求
*/
public static AlipayTradeRefundRequest convertRefundRequest(RefundDto refundVo) {
AlipayRefundRequest refundRequest = new AlipayRefundRequest()
.setOutRequestNo(refundVo.getRefundId())
.setOutTradeNo(refundVo.getPayId())
.setTradeNo(refundVo.getOutTradeNo())
.setRefundAmount(refundVo.getRefundAmount().setScale(2, RoundingMode.HALF_UP).toPlainString());
AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
alipayRequest.setBizContent(JacksonUtil.toJsonString(refundRequest));
return alipayRequest;
}
/**
* 转换为支付宝支付查询请求
* @param payId 支付ID
* @return 支付宝查询请求
*/
public static AlipayTradeQueryRequest convertQueryPayRequest(String payId) {
AlipayQueryPayRequest queryPayRequest = new AlipayQueryPayRequest().setOutTradeNo(payId);
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent(JacksonUtil.toJsonString(queryPayRequest));
return request;
}
/**
* 转换为支付宝快速退款查询请求
* @param refundRequest 退款请求
* @return 支付宝快速退款查询请求
*/
public static AlipayTradeFastpayRefundQueryRequest convertQueryRefundRequest(QueryRefundRequest refundRequest) {
AlipayQueryRefundRequest queryRefundRequest = new AlipayQueryRefundRequest();
queryRefundRequest.setOutRequestNo(refundRequest.getRefundId());
queryRefundRequest.setOutTradeNo(refundRequest.getPayId());
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
request.setBizContent(JacksonUtil.toJsonString(queryRefundRequest));
return request;
}
}