|
@@ -55,6 +55,31 @@ public class AliPayService extends PayBaseService {
|
|
|
|
|
|
private Pattern param = Pattern.compile("value=\"(.*)\">", Pattern.CASE_INSENSITIVE);
|
|
|
|
|
|
+ /**
|
|
|
+ * 支付成功并且已经超过可退款时限
|
|
|
+ */
|
|
|
+ private static final String TRADE_STATUS_FINISHED = "TRADE_FINISHED";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付成功
|
|
|
+ */
|
|
|
+ private static final String TRADE_STATUS_SUCCESS = "TRADE_SUCCESS";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单待支付(正确输入了支付密码, 但是余额不足, 未支付成功)
|
|
|
+ */
|
|
|
+ private static final String TRADE_STATUS_WAIT = "WAIT_BUYER_PAY";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单关闭
|
|
|
+ */
|
|
|
+ private static final String TRADE_STATUS_CLOSED = "TRADE_CLOSED";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单超时自动关闭时间
|
|
|
+ */
|
|
|
+ private static final long TIME_EXPIRE = 2L;
|
|
|
+
|
|
|
/**
|
|
|
* 同步回调地址配置
|
|
|
*/
|
|
@@ -66,11 +91,6 @@ public class AliPayService extends PayBaseService {
|
|
|
*/
|
|
|
private String notifyUrl;
|
|
|
|
|
|
- /**
|
|
|
- * 支付配置
|
|
|
- */
|
|
|
- private Config config;
|
|
|
-
|
|
|
/**
|
|
|
* 商户信息
|
|
|
*/
|
|
@@ -121,9 +141,7 @@ public class AliPayService extends PayBaseService {
|
|
|
|
|
|
@Override
|
|
|
public String notify(HttpServletRequest request, HttpServletResponse response) {
|
|
|
-
|
|
|
log.error("支付宝异步回调 -------start----->");
|
|
|
-
|
|
|
//解密回调参数
|
|
|
Map<String, String> params = new HashMap<>(26);
|
|
|
Enumeration<String> parameterNames = request.getParameterNames();
|
|
@@ -137,9 +155,7 @@ public class AliPayService extends PayBaseService {
|
|
|
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)));
|
|
|
//支付宝签名认证
|
|
@@ -165,67 +181,73 @@ public class AliPayService extends PayBaseService {
|
|
|
|
|
|
@Override
|
|
|
public void closeOrder(PlatformOrderDTO platformOrderDTO) {
|
|
|
+ //支付配置
|
|
|
+ GamePayWayDTO gamePayWayDTO = gamePayWayService.getGamePayWay(platformOrderDTO.getGameId(),
|
|
|
+ platformOrderDTO.getPayWayId(), platformOrderDTO.getPayDeviceId());
|
|
|
+ //初始化配置
|
|
|
+ this.configInit(gamePayWayDTO);
|
|
|
+ //订单id
|
|
|
+ String orderId = platformOrderDTO.getOrderId();
|
|
|
//查询订单
|
|
|
- AlipayTradeQueryResponse queryResponse = null;
|
|
|
+ AlipayTradeQueryResponse queryResponse;
|
|
|
try {
|
|
|
- queryResponse = Factory.Payment.Common().query(platformOrderDTO.getOrderId());
|
|
|
+ queryResponse = Factory.Payment.Common().query(orderId);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("关闭订单查询订单信息异常, orderId : {}, e : {}", orderId, e.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!ResponseChecker.success(queryResponse)) {
|
|
|
+ log.error("关闭订单查询订单信息不存在, orderId : {}, queryResponse : {}", orderId, JsonUtil.toString(queryResponse));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.error("支付宝关闭订单, 查询阿里订单接口 queryResponse : {}", JsonUtil.toString(queryResponse));
|
|
|
+ //订单已经自动关闭
|
|
|
+ if (Objects.equals(queryResponse.getTradeStatus(), TRADE_STATUS_CLOSED)) {
|
|
|
+ //本地订单未更新为取消状态, 更新订单最新状态
|
|
|
+ if (!Objects.equals(platformOrderDTO.getStatus(), OrderStateEnum.CANCEL_PAY.getCode())) {
|
|
|
+ orderService.update(new LambdaUpdateWrapper<Order>()
|
|
|
+ .set(Order::getStatus, OrderStateEnum.CANCEL_PAY.getCode())
|
|
|
+ .set(Order::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(Order::getOrderId, orderId));
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //订单是支付成功或者完成状态, 本地订单非成功状态(支付成功的订单, 到不了关闭逻辑)
|
|
|
+ if (Objects.equals(queryResponse.getTradeStatus(), TRADE_STATUS_SUCCESS)
|
|
|
+ || Objects.equals(queryResponse.getTradeStatus(), TRADE_STATUS_FINISHED)) {
|
|
|
+ this.paySuccess(orderId, platformOrderDTO.getAmount().toString(), this.payMerchant.getMerchantNo());
|
|
|
+ }
|
|
|
+ //订单已经超时, 还是待支付状态(该逻辑只是兜底, 按照支付宝自动超时关闭, 正常情况下该逻辑不会执行)
|
|
|
+ if (Objects.equals(queryResponse.getTradeStatus(), TRADE_STATUS_WAIT)) {
|
|
|
+ log.error("订单超时, 支付宝订单未自动关闭, orderId : {}", orderId);
|
|
|
+ //执行关闭订单
|
|
|
+ AlipayTradeCloseResponse response;
|
|
|
+ try {
|
|
|
+ response = Factory.Payment.Common().close(orderId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("支付宝执行关闭订单异常, orderId : {}, e : {}", orderId, e.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //订单关闭成功, 订单状态更新为取消
|
|
|
+ if (ResponseChecker.success(response)) {
|
|
|
+ orderService.update(new LambdaUpdateWrapper<Order>()
|
|
|
+ .set(Order::getStatus, OrderStateEnum.CANCEL_PAY.getCode())
|
|
|
+ .set(Order::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(Order::getOrderId, orderId));
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- log.error("关闭订单, 查询阿里订单接口 queryResponse : {}", JsonUtil.toString(queryResponse));
|
|
|
-
|
|
|
-
|
|
|
-// //支付宝预下单的订单不需要关闭
|
|
|
-// if (Objects.equals(platformOrderDTO.getStatus(), OrderStateEnum.READY_PAY.getCode())) {
|
|
|
-// log.error("支付宝预下单订单, 不需要关闭");
|
|
|
-// return;
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// //支付配置
|
|
|
-// GamePayWayDTO gamePayWayDTO = gamePayWayService.getGamePayWay(platformOrderDTO.getGameId(),
|
|
|
-// platformOrderDTO.getPayWayId(), platformOrderDTO.getPayDeviceId());
|
|
|
-// //初始化配置
|
|
|
-// this.configInit(gamePayWayDTO);
|
|
|
-// //取消订单
|
|
|
-// AlipayTradeCloseResponse response = null;
|
|
|
-// try {
|
|
|
-// log.error("支付宝关闭订单号 , outTradeNo : {}", platformOrderDTO.getOrderId());
|
|
|
-//// response = Factory.Payment.Common().close(platformOrderDTO.getOrderId());
|
|
|
-// if (ResponseChecker.success(response)) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// log.error("支付宝取消订单失败, platformOrderDTO : {}, response : {}", JsonUtil.toString(platformOrderDTO), JsonUtil.toString(response));
|
|
|
-// } catch (Exception e) {
|
|
|
-// log.error("支付宝取消订单异常, platformOrderDTO : {}, e:{}", JsonUtil.toString(platformOrderDTO), e.getMessage());
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> h5Pay(ProductPayParamBO product) {
|
|
|
AlipayTradeWapPayResponse response;
|
|
|
try {
|
|
|
- log.error("支付宝H5下单订单号 , outTradeNo : {}", product.getOutTradeNo());
|
|
|
-
|
|
|
- String timeExpire = DateUtil.formatLocalDateTime(LocalDateTime.now().plusMinutes(3));
|
|
|
-
|
|
|
response = Factory.Payment.Wap().asyncNotify(notifyUrl)
|
|
|
- .optional("time_expire", timeExpire)
|
|
|
+ .optional("time_expire", DateUtil.formatLocalDateTime(LocalDateTime.now().plusMinutes(TIME_EXPIRE)))
|
|
|
.pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), "", "");
|
|
|
-
|
|
|
-
|
|
|
- log.error("支付宝H5下单订单号 , outTradeNo : {}, timeExpire : {}", product.getOutTradeNo(), timeExpire);
|
|
|
-
|
|
|
-
|
|
|
-// response = Factory.Payment.Wap().asyncNotify(notifyUrl)
|
|
|
-// .pay(product.getSubject(), product.getOutTradeNo(), product.getTotalFee(), "", "");
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
log.error("支付宝H5支付异常, product : {}, e:{}", JsonUtil.toString(product), e.getMessage());
|
|
|
throw new BaseException("支付宝H5支付异常");
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
//请求失败
|
|
|
if (!ResponseChecker.success(response)) {
|
|
|
log.error("支付宝H5支付失败, product : {}, response : {}", JsonUtil.toString(product), JsonUtil.toString(response));
|
|
@@ -323,6 +345,7 @@ public class AliPayService extends PayBaseService {
|
|
|
AlipayTradePagePayResponse response;
|
|
|
try {
|
|
|
response = Factory.Payment.Page().asyncNotify(notifyUrl)
|
|
|
+ .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());
|
|
@@ -343,6 +366,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());
|
|
@@ -373,7 +397,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));
|
|
|
}
|
|
|
}
|