package com.hj.payment.service.Impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.hj.common.exception.BusinessException;
import com.hj.common.util.ServletUtils;
import com.hj.common.util.UUIDUtils;
import com.hj.payment.common.config.AliPayApiConfig;
import com.hj.payment.common.pojo.AliPayRefundVO;
import com.hj.payment.common.pojo.AliPayVO;
import com.hj.payment.common.pojo.AliTradePayVO;
import com.hj.payment.common.properties.AliPayProperties;
import com.hj.payment.service.AliPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Service
public class AliPayServiceImpl implements AliPayService {
private Logger logger = LoggerFactory.getLogger(getClass());
private static final String CHARSET = "UTF-8";
private static final Integer ORDERNUMBER = 16;
private static final String EXPIRE = "30m";
@Autowired
private AliPayProperties aliPayProperties;
@Autowired
private AliPayApiConfig aliPayApiConfig;
@Override
public void pcPay(AliPayVO aliPayVO) {
logger.info("===========支付宝电脑网站支付============");
AlipayTradePagePayModel model = new AlipayTradePagePayModel();
String current = UUIDUtils.getRandomNumber(ORDERNUMBER);
logger.info("订单号 : {}", current);
model.setOutTradeNo(current);
model.setProductCode("FAST_INSTANT_TRADE_PAY");
model.setTotalAmount(aliPayVO.getAmount().toString());
model.setSubject("支付宝电脑网站支付");
model.setBody("支付宝支付,共" + aliPayVO.getAmount() + "元");
model.setTimeoutExpress(EXPIRE);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
try {
String form = aliPayApiConfig.getAliPayClient().pageExecute(request).getBody();
HttpServletResponse response = ServletUtils.getResponse();
response.setContentType("text/html;charset=" + CHARSET);
PrintWriter writer = response.getWriter();
logger.info("调用成功 : {}", form);
writer.write(form);
writer.flush();
writer.close();
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝电脑网站支付失败");
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("支付宝response对象获取流失败");
}
}
@Override
public String appPay(AliPayVO aliPayVO) {
logger.info("===========支付宝APP支付============");
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
String current = UUIDUtils.getRandomNumber(ORDERNUMBER);
logger.info("订单号 : {}", current);
model.setOutTradeNo(current);
model.setSubject("支付宝APP支付");
model.setTotalAmount(aliPayVO.getAmount().toString());
model.setBody("支付宝支付,共" + aliPayVO.getAmount() + "元");
model.setProductCode("QUICK_MSECURITY_PAY");
model.setTimeoutExpress(EXPIRE);
model.setPassbackParams("callback params");
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
AlipayTradeAppPayResponse response = null;
try {
response = aliPayApiConfig.getAliPayClient().sdkExecute(request);
logger.info("调用成功 : {}", response.getBody());
return response.getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝app支付失败");
}
}
@Override
public void WapPay(AliPayVO aliPayVO) {
logger.info("===========支付宝手机网站支付============");
AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
String current = UUIDUtils.getRandomNumber(ORDERNUMBER);
logger.info("订单号 : {}", current);
model.setOutTradeNo(current);
model.setSubject("支付宝手机网站支付");
model.setTotalAmount(aliPayVO.getAmount().toString());
model.setBody("支付宝支付,共" + aliPayVO.getAmount() + "元");
model.setProductCode("QUICK_WAP_PAY");
model.setTimeoutExpress(EXPIRE);
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
try {
String form = aliPayApiConfig.getAliPayClient().pageExecute(request).getBody();
HttpServletResponse response = ServletUtils.getResponse();
response.setContentType("text/html;charset=" + CHARSET);
logger.info("调用成功 : {}", form);
PrintWriter out = response.getWriter();
out.write(form);
out.flush();
out.close();
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝手机网站支付失败");
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException("支付宝response对象获取流失败");
}
}
@Override
public String tradePay(AliTradePayVO aliTradePayVO) {
logger.info("===========支付宝条码 声波支付============");
String subject = null;
String scene = aliTradePayVO.getScene();
if ("wave_code".equals(scene)) {
subject = "支付宝声波支付";
} else if ("bar_code".equals(scene)) {
subject = "支付宝条形码支付";
} else {
throw new BusinessException("入参错误");
}
AlipayTradePayModel model = new AlipayTradePayModel();
String current = UUIDUtils.getRandomNumber(ORDERNUMBER);
logger.info("订单号 : {}", current);
model.setOutTradeNo(current);
model.setScene(scene);
model.setAuthCode(aliTradePayVO.getAuthCode());
model.setSubject(subject);
model.setStoreId(aliTradePayVO.getStoreId());
model.setTotalAmount(aliTradePayVO.getAmount().toString());
model.setTimeoutExpress(EXPIRE);
AlipayTradePayRequest request = new AlipayTradePayRequest();
request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
try {
AlipayTradePayResponse response = aliPayApiConfig.getAliPayClient().execute(request);
logger.info("调用成功 : {}", response.getBody());
return response.getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝条码或声波支付失败");
}
}
@Override
public String tradePrecreatePay(AliTradePayVO aliTradePayVO) {
logger.info("===========支付宝扫码支付============");
AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
String current = UUIDUtils.getRandomNumber(ORDERNUMBER);
logger.info("订单号 : {}", current);
model.setOutTradeNo(current);
model.setTotalAmount(aliTradePayVO.getAmount().toString());
model.setSubject("支付宝扫码支付");
model.setStoreId(aliTradePayVO.getStoreId());
model.setTimeoutExpress(EXPIRE);
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
try {
String body = aliPayApiConfig.getAliPayClient().execute(request).getBody();
logger.info("调用成功 : {}", body);
JSONObject jsonObject = JSON.parseObject(body);
String objectString = jsonObject.getString("alipay_trade_precreate_response");
JSONObject json = JSON.parseObject(objectString);
if ("10000".equals(json.getString("code"))) {
return json.getString("qr_code");
}
return json.getString("sub_msg");
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝扫码支付失败");
}
}
@Override
public String tradeRefund(AliPayRefundVO aliPayRefundVO) {
logger.info("===========支付宝退款============");
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
model.setOutTradeNo(aliPayRefundVO.getOutTradeNo());
model.setTradeNo(aliPayRefundVO.getTradeNo());
model.setRefundAmount(aliPayRefundVO.getAmount().toString());
model.setRefundReason("支付宝退款");
if ("1".equals(aliPayRefundVO.getFlag())) {
model.setOutRequestNo(UUIDUtils.getRandomNumber(10));
}
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizModel(model);
AlipayTradeRefundResponse response =null;
try {
response = aliPayApiConfig.getAliPayClient().execute(request);
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("支付宝退款失败");
}
if (response.isSuccess()){
logger.info("成功信息 : {}",response.getBody());
return "退款成功";
}else {
logger.error("失败信息 : {}",response.getBody());
throw new BusinessException(response.getSubMsg());
}
}
@Override
public String tradeQuery(AliPayRefundVO aliPayRefundVO) {
logger.info("===========交易订单查询============");
AlipayTradeQueryModel model = new AlipayTradeQueryModel();
model.setOutTradeNo(aliPayRefundVO.getOutTradeNo());
model.setTradeNo(aliPayRefundVO.getTradeNo());
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizModel(model);
AlipayTradeQueryResponse response = null;
try {
response = aliPayApiConfig.getAliPayClient().execute(request);
} catch (AlipayApiException e) {
e.printStackTrace();
throw new BusinessException("交易订单查询失败");
}
if (response.isSuccess()){
logger.info("成功信息 : {}",response.getBody());
return response.getBody();
}else {
logger.error("失败信息 : {}",response.getBody());
throw new BusinessException(response.getSubMsg());
}
}
}