|
@@ -4,7 +4,7 @@ import com.alipay.easysdk.factory.Factory;
|
|
|
import com.alipay.easysdk.kernel.Config;
|
|
|
import com.alipay.easysdk.kernel.util.ResponseChecker;
|
|
|
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
|
|
|
-import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
|
|
|
+import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
|
|
|
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
|
|
|
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
@@ -13,9 +13,9 @@ import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
import com.zanxiang.common.enums.OrderStateEnum;
|
|
|
import com.zanxiang.common.enums.OsEnum;
|
|
|
import com.zanxiang.common.exception.BaseException;
|
|
|
+import com.zanxiang.module.util.DateUtil;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
import com.zanxiang.module.util.URIUtil;
|
|
|
-import com.zanxiang.module.util.pojo.ResultVO;
|
|
|
import com.zanxiang.mybatis.entity.Order;
|
|
|
import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
|
|
|
import com.zanxiang.sdk.domain.dto.*;
|
|
@@ -36,7 +36,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.net.URL;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
@@ -55,31 +55,30 @@ public class AliPayService extends PayBaseService {
|
|
|
private Pattern param = Pattern.compile("value=\"(.*)\">", Pattern.CASE_INSENSITIVE);
|
|
|
|
|
|
/**
|
|
|
- * 异步回调地址配置
|
|
|
+ * 支付成功
|
|
|
*/
|
|
|
- @Value("${payConfig.aliPay.returnUrl}")
|
|
|
- private String returnUrlConfig;
|
|
|
+ private static final String TRADE_STATUS_SUCCESS = "TRADE_SUCCESS";
|
|
|
|
|
|
/**
|
|
|
- * 同步回调地址配置
|
|
|
+ * 订单关闭
|
|
|
*/
|
|
|
- @Value("${payConfig.aliPay.notifyUrl}")
|
|
|
- private String notifyUrlConfig;
|
|
|
+ private static final String TRADE_STATUS_CLOSED = "TRADE_CLOSED";
|
|
|
|
|
|
/**
|
|
|
- * 异步回调地址
|
|
|
+ * 订单超时自动关闭时间
|
|
|
*/
|
|
|
- private String returnUrl;
|
|
|
+ private static final long TIME_EXPIRE = 2L;
|
|
|
|
|
|
/**
|
|
|
- * 同步回调地址
|
|
|
+ * 同步回调地址配置
|
|
|
*/
|
|
|
- private String notifyUrl;
|
|
|
+ @Value("${payConfig.aliPay.notifyUrl}")
|
|
|
+ private String notifyUrlConfig;
|
|
|
|
|
|
/**
|
|
|
- * 支付配置
|
|
|
+ * 同步回调地址
|
|
|
*/
|
|
|
- private Config config;
|
|
|
+ private String notifyUrl;
|
|
|
|
|
|
/**
|
|
|
* 商户信息
|
|
@@ -100,7 +99,6 @@ public class AliPayService extends PayBaseService {
|
|
|
//初始化支付配置
|
|
|
this.configInit(gamePayWayDTO);
|
|
|
//回调地址中拼接游戏具体支付方式
|
|
|
- this.returnUrl = this.returnUrlConfig + "?" + "gamePayWayId=" + this.attach.getGamePayWayId();
|
|
|
this.notifyUrl = this.notifyUrlConfig + "?" + "gamePayWayId=" + this.attach.getGamePayWayId();
|
|
|
//支付方式
|
|
|
int payDevice = product.getPayDevice().intValue();
|
|
@@ -119,11 +117,12 @@ public class AliPayService extends PayBaseService {
|
|
|
default:
|
|
|
throw new RuntimeException("未知支付方式");
|
|
|
}
|
|
|
+ //订单支付添加过期缓存
|
|
|
+ this.orderExpire(product.getOutTradeNo());
|
|
|
//更新订单商户信息
|
|
|
orderService.update(new LambdaUpdateWrapper<Order>()
|
|
|
.set(Order::getMerchantNo, payMerchant.getMerchantNo())
|
|
|
.set(Order::getMerchantName, payMerchant.getMerchantName())
|
|
|
- .set(Order::getStatus, OrderStateEnum.WAIT_PAY.getCode())
|
|
|
.eq(Order::getOrderId, product.getOutTradeNo()));
|
|
|
//返回结果
|
|
|
return resultMap;
|
|
@@ -131,72 +130,49 @@ public class AliPayService extends PayBaseService {
|
|
|
|
|
|
@Override
|
|
|
public String notify(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- try {
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- Enumeration<String> parameterNames = request.getParameterNames();
|
|
|
- while (parameterNames.hasMoreElements()) {
|
|
|
- String parameterName = parameterNames.nextElement();
|
|
|
- params.put(parameterName, request.getParameter(parameterName));
|
|
|
- }
|
|
|
- String orderNo = request.getParameter("out_trade_no");
|
|
|
- String gamePayWayId = params.get("gamePayWayId");
|
|
|
- //回调地址拼接参数不参加sign验证
|
|
|
- params.remove("gamePayWayId");
|
|
|
- //初始化配置
|
|
|
- configInit(gamePayWayService.getById(Long.valueOf(gamePayWayId)));
|
|
|
- Boolean flag = Factory.Payment.Common().verifyNotify(params);
|
|
|
- if (flag) {
|
|
|
- if (paySuccess(orderNo, params.get("total_amount"), params.get("trade_no"))) {
|
|
|
- log.info("支付宝异步回调成功, orderId : {}", orderNo);
|
|
|
- return HttpStatusEnum.SUCCESS.getMsg();
|
|
|
- }
|
|
|
- }
|
|
|
- log.error("支付宝异步回调-验证签名失败 request:{}", request);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("支付宝异步回调 -------start----->");
|
|
|
+ //解密回调参数
|
|
|
+ Map<String, String> params = new HashMap<>(26);
|
|
|
+ Enumeration<String> parameterNames = request.getParameterNames();
|
|
|
+ while (parameterNames.hasMoreElements()) {
|
|
|
+ String parameterName = parameterNames.nextElement();
|
|
|
+ params.put(parameterName, request.getParameter(parameterName));
|
|
|
}
|
|
|
- return HttpStatusEnum.FAIL.getMsg();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ResultVO synNotify(HttpServletRequest request) {
|
|
|
+ //订单号orderId
|
|
|
+ String orderNo = request.getParameter("out_trade_no");
|
|
|
+ //订单支付结果
|
|
|
+ String tradeStatus = params.get("trade_status");
|
|
|
+ //订单支付方式id, 因为回调地址拼接参数不参加sign验证, 所以这里使用移除
|
|
|
+ String gamePayWayId = params.remove("gamePayWayId");
|
|
|
+ log.error("支付宝异步回调获取到的参数, gamePayWayId : {}, params : {}", gamePayWayId, JsonUtil.toString(params));
|
|
|
+ //初始化配置
|
|
|
+ this.configInit(gamePayWayService.getById(Long.valueOf(gamePayWayId)));
|
|
|
+ //支付宝签名认证
|
|
|
+ Boolean verifyNotify;
|
|
|
try {
|
|
|
- //获取支付宝GET过来反馈信息
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- Map<String, String[]> requestParams = request.getParameterMap();
|
|
|
- for (String name : requestParams.keySet()) {
|
|
|
- String[] values = requestParams.get(name);
|
|
|
- String valueStr = "";
|
|
|
- for (int i = 0; i < values.length; i++) {
|
|
|
- valueStr = (i == values.length - 1) ? valueStr + values[i]
|
|
|
- : valueStr + values[i] + ",";
|
|
|
- }
|
|
|
- //乱码解决,这段代码在出现乱码时使用
|
|
|
- valueStr = new String(valueStr.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
|
|
- params.put(name, valueStr);
|
|
|
- }
|
|
|
- //商户订单号
|
|
|
- String orderNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
|
|
- String gamePayWayId = params.get("gamePayWayId");
|
|
|
- //回调地址拼接参数不参加sign验证
|
|
|
- params.remove("gamePayWayId");
|
|
|
- //初始化配置
|
|
|
- configInit(gamePayWayService.getById(Long.valueOf(gamePayWayId)));
|
|
|
- //验证签名
|
|
|
- Boolean flag = Factory.Payment.Common().verifyNotify(params);
|
|
|
- if (flag) {
|
|
|
- if (paySuccess(orderNo, params.get("total_amount"), params.get("trade_no"))) {
|
|
|
- log.info("支付宝同步回调成功, orderId : {}", orderNo);
|
|
|
- return ResultVO.ok();
|
|
|
- }
|
|
|
- }
|
|
|
- log.error("支付宝同步回调验证签名结果失败, orderId : {}", orderNo);
|
|
|
- return ResultVO.fail(HttpStatusEnum.FAIL.getMsg());
|
|
|
+ verifyNotify = Factory.Payment.Common().verifyNotify(params);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- //处理异常信息
|
|
|
- return ResultVO.fail(e.getMessage());
|
|
|
+ log.error("支付宝异步回调-验证签名异常 params : {}, e : {}", JsonUtil.toString(params), e.getMessage());
|
|
|
+ return HttpStatusEnum.FAIL.getMsg();
|
|
|
+ }
|
|
|
+ if (!verifyNotify) {
|
|
|
+ log.error("支付宝异步回调-验证签名失败, params : {}", JsonUtil.toString(params));
|
|
|
+ return HttpStatusEnum.FAIL.getMsg();
|
|
|
+ }
|
|
|
+ //签名验证通过, 判断是否支付成功
|
|
|
+ if (Objects.equals(TRADE_STATUS_SUCCESS, tradeStatus)) {
|
|
|
+ this.paySuccess(orderNo, params.get("total_amount"), params.get("trade_no"));
|
|
|
}
|
|
|
+ //订单自动关闭
|
|
|
+ if (Objects.equals(TRADE_STATUS_CLOSED, tradeStatus)) {
|
|
|
+ orderService.update(new LambdaUpdateWrapper<Order>()
|
|
|
+ .set(Order::getStatus, OrderStateEnum.CANCEL_PAY.getCode())
|
|
|
+ .set(Order::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(Order::getOrderId, orderNo));
|
|
|
+ }
|
|
|
+ //返回消息接收成功
|
|
|
+ log.error("支付宝异步回调 -------end----->");
|
|
|
+ return HttpStatusEnum.SUCCESS.getMsg();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -206,16 +182,23 @@ public class AliPayService extends PayBaseService {
|
|
|
platformOrderDTO.getPayWayId(), platformOrderDTO.getPayDeviceId());
|
|
|
//初始化配置
|
|
|
this.configInit(gamePayWayDTO);
|
|
|
- //取消订单
|
|
|
- AlipayTradeCloseResponse response;
|
|
|
+ //订单id
|
|
|
+ String orderId = platformOrderDTO.getOrderId();
|
|
|
+ //查询订单
|
|
|
+ AlipayTradeQueryResponse queryResponse;
|
|
|
try {
|
|
|
- response = Factory.Payment.Common().close(platformOrderDTO.getOrderId());
|
|
|
- if (ResponseChecker.success(response)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- log.error("支付宝取消订单失败, platformOrderDTO : {}, response : {}", JsonUtil.toString(platformOrderDTO), JsonUtil.toString(response));
|
|
|
+ queryResponse = Factory.Payment.Common().query(orderId);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("支付宝取消订单异常, platformOrderDTO : {}, e:{}", JsonUtil.toString(platformOrderDTO), e.getMessage());
|
|
|
+ log.error("关闭订单查询订单信息异常, orderId : {}, e : {}", orderId, e.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //未下单的订单, 直接取消(后续如果用户成功支付, 也是可以收到支付成功的回调的)
|
|
|
+ if (!ResponseChecker.success(queryResponse)) {
|
|
|
+ log.error("关闭订单查询订单信息不存在, orderId : {}, queryResponse : {}", orderId, JsonUtil.toString(queryResponse));
|
|
|
+ orderService.update(new LambdaUpdateWrapper<Order>()
|
|
|
+ .set(Order::getStatus, OrderStateEnum.CANCEL_PAY.getCode())
|
|
|
+ .set(Order::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(Order::getOrderId, orderId));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -223,7 +206,8 @@ public class AliPayService extends PayBaseService {
|
|
|
AlipayTradeWapPayResponse response;
|
|
|
try {
|
|
|
response = Factory.Payment.Wap().asyncNotify(notifyUrl)
|
|
|
- .pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), "", this.returnUrl);
|
|
|
+ .optional("time_expire", DateUtil.formatLocalDateTime(LocalDateTime.now().plusMinutes(TIME_EXPIRE)))
|
|
|
+ .pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), "", "");
|
|
|
} catch (Exception e) {
|
|
|
log.error("支付宝H5支付异常, product : {}, e:{}", JsonUtil.toString(product), e.getMessage());
|
|
|
throw new BaseException("支付宝H5支付异常");
|
|
@@ -325,7 +309,8 @@ public class AliPayService extends PayBaseService {
|
|
|
AlipayTradePagePayResponse response;
|
|
|
try {
|
|
|
response = Factory.Payment.Page().asyncNotify(notifyUrl)
|
|
|
- .pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), this.returnUrl);
|
|
|
+ .optional("time_expire", DateUtil.formatLocalDateTime(LocalDateTime.now().plusMinutes(TIME_EXPIRE)))
|
|
|
+ .pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), "");
|
|
|
} catch (Exception e) {
|
|
|
log.error("支付宝pc端支付异常, product : {}, e:{}", JsonUtil.toString(product), e.getMessage());
|
|
|
throw new BaseException("支付宝pc端支付异常");
|
|
@@ -345,6 +330,7 @@ public class AliPayService extends PayBaseService {
|
|
|
AlipayTradeAppPayResponse response;
|
|
|
try {
|
|
|
response = Factory.Payment.App().asyncNotify(notifyUrl)
|
|
|
+ .optional("time_expire", DateUtil.formatLocalDateTime(LocalDateTime.now().plusMinutes(TIME_EXPIRE)))
|
|
|
.pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee());
|
|
|
} catch (Exception e) {
|
|
|
log.error("支付宝app端支付异常, product : {}, e:{}", JsonUtil.toString(product), e.getMessage());
|
|
@@ -375,7 +361,5 @@ public class AliPayService extends PayBaseService {
|
|
|
//赋值配置信息
|
|
|
Config config = JsonUtil.toObj(JsonUtil.toString(payConfigMap), Config.class);
|
|
|
Factory.setOptions(config);
|
|
|
- this.config = JsonUtil.toObj(JsonUtil.toString(payConfigMap), Config.class);
|
|
|
- log.error("初始化的支付配置信息, config : {}", JsonUtil.toString(this.config));
|
|
|
}
|
|
|
}
|