com.zopen.alipay.pay.service.AliPayService Maven / Gradle / Ivy
Show all versions of zopen-ato-starter Show documentation
package com.zopen.alipay.pay.service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.zcj.util.UtilString;
import com.zopen.alipay.exception.AlipayAssert;
import com.zopen.ato.properties.AlipayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
@Component("atoAlipayService")
public class AliPayService {
private static final Logger logger = LoggerFactory.getLogger(AliPayService.class);
@Autowired
private AlipayProperties alipayProperties;
@Autowired
private AlipayClient alipayClient;
/**
* 查询订单
*
* @param outTradeNo 商户订单号
* @return com.alipay.api.response.AlipayTradeQueryResponse
*/
public AlipayTradeQueryResponse orderQuery(String outTradeNo) {
// 验证参数
AlipayAssert.notNullAndEmpty(outTradeNo, "outTradeNo 不能为空");
// 填充请求内容
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
AlipayTradeQueryModel model = new AlipayTradeQueryModel();
model.setOutTradeNo(outTradeNo);
request.setBizModel(model);
// 发送请求
AlipayTradeQueryResponse response = null;
try {
logger.debug(">>发起查询支付宝订单请求,outTradeNo[{}]", outTradeNo);
response = alipayClient.execute(request);
logger.debug("<<查询结果返回:outTradeNo[{}]、code[{}]、msg[{}]", outTradeNo, response.getCode(), response.getMsg());
return response;
} catch (Exception e) {
logger.error(">>支付宝交易查询接口请求失败:" + e.getMessage(), e);
AlipayAssert.notNull(null, "查询失败");
return null;
}
}
/**
* 电脑网站支付(自动跳转到二维码页面)
*
* 直接输出表单内容到 HttpServletResponse
*
* @param body * 商品描述
* @param totalFee * 总金额,单位:分
* @param outTradeNo * 商户订单号
* @param qrPayMode 页面模式(0:600*300;1:300*600;3:75*75;4:自定义width;2:跳转模式)
* https://help.pingxx.com/article/1137360/
* @param qrcodeWidth 二维码尺寸
* @param httpResponse * HttpServletResponse
*/
public void tradePagePay(String body, Integer totalFee, String outTradeNo, String qrPayMode, Long qrcodeWidth, HttpServletResponse httpResponse) {
// 验证参数
AlipayAssert.notNullAndEmpty(alipayProperties.getDomainUrl(), "domainUrl 未设置");
AlipayAssert.notNullAndEmpty(body, "body 不能为空");
AlipayAssert.notNullAndEmpty(outTradeNo, "outTradeNo 不能为空");
AlipayAssert.checkArgument(totalFee != null && totalFee > 0, "totalFee 取值错误");
// 转换金额
String totalFeeString = new DecimalFormat("0.00").format(new BigDecimal(String.valueOf(totalFee)).divide(new BigDecimal("100")));
logger.debug("生成 APP 支付订单信息时金额单位转换:[{}] => [{}],订单号[{}]", totalFee, totalFeeString, outTradeNo);
// 填充请求内容:https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
AlipayTradePagePayModel model = new AlipayTradePagePayModel();
model.setOutTradeNo(outTradeNo);
model.setTotalAmount(totalFeeString);
model.setBody(body);
model.setSubject(body);
model.setQrPayMode(qrPayMode);
model.setQrcodeWidth(qrcodeWidth);
model.setProductCode("FAST_INSTANT_TRADE_PAY");
alipayRequest.setBizModel(model);
alipayRequest.setNotifyUrl(alipayProperties.getDomainUrl() + "/ato/alipay/pay/callback");// 支付成功的异步调用地址
if (UtilString.isNotBlank(alipayProperties.getPaySuccessUrl())) {
alipayRequest.setReturnUrl(alipayProperties.getDomainUrl() + "/ato/alipay/pay/callback-syn");// 支付成功的同步调用地址
}
// 发送请求
try {
logger.debug(">>发起支付宝页面支付生成表单请求,订单号[{}]、商品[{}]、金额[{}]元", outTradeNo, body, totalFeeString);
String form = alipayClient.pageExecute(alipayRequest).getBody();// 调用 SDK 生成表单
logger.debug("<<生成 APP 支付 Form 信息成功,订单号[{}]、表单内容:\n{}", outTradeNo, form);
httpResponse.setContentType("text/html;charset=utf-8");
httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
} catch (AlipayApiException e) {
logger.error("<<生成 APP 支付 Form 信息失败,订单号[" + outTradeNo + "]、异常原因[" + e.getMessage() + "]", e);
AlipayAssert.notNull(null, "下单失败");
} catch (IOException e) {
logger.error("<<输出 APP 支付 Form 信息失败,订单号[" + outTradeNo + "]、异常原因[" + e.getMessage() + "]", e);
AlipayAssert.notNull(null, "生成表单失败");
}
}
/**
* 电脑网站支付(自动跳转到支付宝收银台页面,两种付款方式:提供二维码供扫码支付;提供账号登录支付)
*
* 直接输出表单内容到 HttpServletResponse
*
* @param body * 商品描述
* @param totalFee * 总金额,单位:分
* @param outTradeNo * 商户订单号
* @param httpResponse * HttpServletResponse
*/
public void tradePagePay(String body, Integer totalFee, String outTradeNo, HttpServletResponse httpResponse) {
this.tradePagePay(body, totalFee, outTradeNo, null, null, httpResponse);
}
/**
* 统一下单
*
* @param body 商品描述
* @param totalFee 总金额,单位:分
* @param outTradeNo 商户订单号
* @return java.lang.String 就是 orderString 可以直接给客户端请求,无需再做处理
*/
public String unifiedOrder(String body, Integer totalFee, String outTradeNo) {
// 验证参数
AlipayAssert.notNullAndEmpty(alipayProperties.getDomainUrl(), "domainUrl 未设置");
AlipayAssert.notNullAndEmpty(body, "body 不能为空");
AlipayAssert.notNullAndEmpty(outTradeNo, "outTradeNo 不能为空");
AlipayAssert.checkArgument(totalFee != null && totalFee > 0, "totalFee 取值错误");
// 转换金额
String totalFeeString = new DecimalFormat("0.00").format(new BigDecimal(String.valueOf(totalFee)).divide(new BigDecimal("100")));
logger.debug("生成 APP 支付订单信息时金额单位转换:[{}] => [{}],订单号[{}]", totalFee, totalFeeString, outTradeNo);
// 填充请求内容
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(body);
model.setSubject(body);
model.setOutTradeNo(outTradeNo);
model.setTimeoutExpress("15d");
model.setTotalAmount(totalFeeString);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
request.setNotifyUrl(alipayProperties.getDomainUrl() + "/ato/alipay/pay/callback");
// 发送请求
try {
logger.debug(">>发起支付宝下单请求,订单号[{}]、商品[{}]、金额[{}]元", outTradeNo, body, totalFeeString);
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
String result = response.getBody();
logger.debug("<<生成 APP 支付订单信息成功,订单号[{}]、返回给客户端的内容:\n{}", outTradeNo, result);
return result;
} catch (Exception e) {
logger.error("<<生成 APP 支付订单信息失败,订单号[" + outTradeNo + "]、异常原因[" + e.getMessage() + "]", e);
AlipayAssert.notNull(null, "下单失败");
return null;
}
}
}