refactor(payment): 调整支付宝支付和订单状态
parent
00385906c3
commit
63f14ce3d1
|
@ -17,10 +17,12 @@ import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.jboss.marshalling.SimpleDataInput;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -179,15 +181,18 @@ public class MemberController {
|
||||||
@ApiOperation(value = "是否是会员")
|
@ApiOperation(value = "是否是会员")
|
||||||
public AjaxResult isMember() {
|
public AjaxResult isMember() {
|
||||||
Long userId = SecurityUtils.getUserId();
|
Long userId = SecurityUtils.getUserId();
|
||||||
Map<String, Long> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
Member member = memberService.getUseUserMemberByUserId(userId);
|
Member member = memberService.getUseUserMemberByUserId(userId);
|
||||||
if (Optional.ofNullable(member).isPresent()) {
|
if (Optional.ofNullable(member).isPresent()) {
|
||||||
map.put("result", 1L);
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date startDate = member.getStartDate();
|
||||||
|
Date endDate = member.getEndDate();
|
||||||
|
map.put("result", "1");
|
||||||
|
map.put("startDate", sdf.format(startDate));
|
||||||
|
map.put("endDate", sdf.format(endDate));
|
||||||
return AjaxResult.success(map);
|
return AjaxResult.success(map);
|
||||||
}
|
}
|
||||||
map.put("result", 0L);
|
map.put("result", "0");
|
||||||
map.put("startDate", member.getStartDate().getTime());
|
|
||||||
map.put("endDate", member.getEndDate().getTime());
|
|
||||||
|
|
||||||
return AjaxResult.success(map);
|
return AjaxResult.success(map);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.mcwl.web.controller.pay.AliPay;
|
package com.mcwl.web.controller.pay.AliPay;
|
||||||
|
|
||||||
import cn.hutool.core.lang.UUID;
|
import cn.hutool.core.lang.UUID;
|
||||||
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
||||||
import com.alipay.easysdk.factory.Factory;
|
import com.alipay.easysdk.factory.Factory;
|
||||||
import com.mcwl.common.annotation.Anonymous;
|
import com.mcwl.common.annotation.Anonymous;
|
||||||
import com.mcwl.common.core.controller.BaseController;
|
import com.mcwl.common.core.controller.BaseController;
|
||||||
|
@ -27,6 +28,7 @@ import javax.validation.constraints.NotNull;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,7 +199,7 @@ public class AliPayController extends BaseController {
|
||||||
OrderTrade orderTrade = orderTradeService.lambdaQuery()
|
OrderTrade orderTrade = orderTradeService.lambdaQuery()
|
||||||
.eq(OrderTrade::getCode, code)
|
.eq(OrderTrade::getCode, code)
|
||||||
.one();
|
.one();
|
||||||
if (orderTrade == null) {
|
if (Objects.isNull(orderTrade)) {
|
||||||
return "failure";
|
return "failure";
|
||||||
}
|
}
|
||||||
// 获取订单后缀
|
// 获取订单后缀
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.mcwl.web.controller.pay.AliPay;
|
||||||
import com.alipay.easysdk.factory.Factory;
|
import com.alipay.easysdk.factory.Factory;
|
||||||
import com.alipay.easysdk.kernel.Config;
|
import com.alipay.easysdk.kernel.Config;
|
||||||
import com.alipay.easysdk.payment.common.models.AlipayTradeCancelResponse;
|
import com.alipay.easysdk.payment.common.models.AlipayTradeCancelResponse;
|
||||||
|
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
|
||||||
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
|
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
|
||||||
import com.mcwl.common.JSONUtils;
|
import com.mcwl.common.JSONUtils;
|
||||||
import com.mcwl.common.core.controller.BaseController;
|
import com.mcwl.common.core.controller.BaseController;
|
||||||
|
@ -134,17 +135,30 @@ public class OrderTradeController extends BaseController {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 关闭交易
|
* 撤销交易
|
||||||
*/
|
*/
|
||||||
@GetMapping("/closeTrade")
|
@GetMapping("/cancelTrade")
|
||||||
public AjaxResult closeTrade(String outTradeNo) throws Exception {
|
public AjaxResult cancelTrade(String outTradeNo) throws Exception {
|
||||||
// 关闭交易
|
// 关闭交易
|
||||||
AlipayTradeCancelResponse close = Factory.Payment.Common().cancel(outTradeNo);
|
AlipayTradeCancelResponse cancel = Factory.Payment.Common().cancel(outTradeNo);
|
||||||
if (close.getCode().equals("10000")) {
|
if (cancel.getCode().equals("10000")) {
|
||||||
return AjaxResult.success("关闭成功");
|
return AjaxResult.success("关闭成功");
|
||||||
}
|
}
|
||||||
return AjaxResult.error("关闭失败");
|
return AjaxResult.error("关闭失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭交易
|
||||||
|
*/
|
||||||
|
// @GetMapping("/closeTrade")
|
||||||
|
// public AjaxResult closeTrade(String outTradeNo) throws Exception {
|
||||||
|
// // 关闭交易
|
||||||
|
// AlipayTradeCloseResponse close = Factory.Payment.Common().close(outTradeNo);
|
||||||
|
// if (close.getCode().equals("10000")) {
|
||||||
|
// return AjaxResult.success("关闭成功");
|
||||||
|
// }
|
||||||
|
// return AjaxResult.error("关闭失败");
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,6 +86,8 @@ public class AliPayServiceImpl implements AliPayService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private OrderTradeService orderTradeService;
|
private OrderTradeService orderTradeService;
|
||||||
|
|
||||||
|
private final int MAX_RETRY = 3; // 最大重试次数
|
||||||
|
|
||||||
|
|
||||||
public AliPayServiceImpl(Config config) {
|
public AliPayServiceImpl(Config config) {
|
||||||
Factory.setOptions(config);
|
Factory.setOptions(config);
|
||||||
|
@ -315,11 +317,13 @@ public class AliPayServiceImpl implements AliPayService {
|
||||||
|
|
||||||
// 查询redis中订单信息
|
// 查询redis中订单信息
|
||||||
String orderTradeJson = redisCache.getCacheObject(outTradeNo);
|
String orderTradeJson = redisCache.getCacheObject(outTradeNo);
|
||||||
OrderTrade orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
OrderTrade orderTrade = null;
|
||||||
|
if (StringUtils.isNotBlank(orderTradeJson)) {
|
||||||
|
orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
||||||
|
}
|
||||||
// 如果redis中存在该订单, 则直接返回该订单的支付状态
|
// 如果redis中存在该订单, 则直接返回该订单的支付状态
|
||||||
if (StringUtils.isNotBlank(orderTradeJson) && Objects.nonNull(orderTrade)) {
|
if (Objects.nonNull(orderTrade)) {
|
||||||
return AjaxResult.success("查询成功", orderTrade.getPayStatus());
|
return AjaxResult.success("查询成功", orderTrade.getOrderStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询支付宝订单
|
// 查询支付宝订单
|
||||||
|
@ -339,7 +343,7 @@ public class AliPayServiceImpl implements AliPayService {
|
||||||
String jsonString = JSONObject.toJSONString(tradStatus);
|
String jsonString = JSONObject.toJSONString(tradStatus);
|
||||||
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
||||||
Object tradeStatus = jsonObject.get("trade_status");
|
Object tradeStatus = jsonObject.get("trade_status");
|
||||||
if (tradeStatus != null && tradeStatus.equals("TRADE_SUCCESS")) {
|
if (Objects.nonNull(tradeStatus) && tradeStatus.equals("TRADE_SUCCESS")) {
|
||||||
Map<String, String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
params.put("buyer_pay_amount", jsonObject.get("buyer_pay_amount").toString());
|
params.put("buyer_pay_amount", jsonObject.get("buyer_pay_amount").toString());
|
||||||
params.put("out_trade_no", jsonObject.get("out_trade_no").toString());
|
params.put("out_trade_no", jsonObject.get("out_trade_no").toString());
|
||||||
|
@ -350,24 +354,33 @@ public class AliPayServiceImpl implements AliPayService {
|
||||||
// 获取订单后缀
|
// 获取订单后缀
|
||||||
String suffix = code.substring(code.lastIndexOf("_") + 1);
|
String suffix = code.substring(code.lastIndexOf("_") + 1);
|
||||||
orderTradeService.orderHandler(orderTrade, suffix, params);
|
orderTradeService.orderHandler(orderTrade, suffix, params);
|
||||||
return AjaxResult.success("查询成功", orderTrade.getPayStatus());
|
return AjaxResult.success("查询成功", orderTrade.getOrderStatus());
|
||||||
}
|
}
|
||||||
long time = orderTrade.getCreateTime().getTime() / 1000 / 60;
|
long time = (System.currentTimeMillis() - orderTrade.getCreateTime().getTime()) / 1000 / 60;
|
||||||
// time 超过15分钟,则删除redis中订单信息
|
// time 超过15分钟,则删除redis中订单信息
|
||||||
if (time >= 15) {
|
if (time >= 1) {
|
||||||
// 撤销交易
|
for (int i = 0; i < MAX_RETRY; i++) {
|
||||||
AlipayTradeCancelResponse cancel = Factory.Payment.Common().cancel(outTradeNo);
|
// 撤销交易
|
||||||
if (cancel.getCode().equals("10000")) {
|
AlipayTradeCancelResponse cancel = Factory.Payment.Common().cancel(outTradeNo);
|
||||||
orderTrade.setOrderStatus(4);
|
if (cancel.getMsg().equals("Success")) {
|
||||||
orderTradeService.updateById(orderTrade);
|
orderTrade.setOrderStatus(4);
|
||||||
redisCache.deleteObject(outTradeNo + "promotionId");
|
orderTradeService.updateById(orderTrade);
|
||||||
|
redisCache.deleteObject(outTradeNo + "promotionId");
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// 检查是否需要继续重试
|
||||||
|
if (!"Y".equals(cancel.getRetryFlag())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.sleep(2000); // 间隔2秒重试
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return AjaxResult.success("查询成功", orderTrade.getPayStatus());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redisCache.setCacheObject(outTradeNo, JSONUtil.toJsonStr(orderTrade), 5, TimeUnit.SECONDS);
|
redisCache.setCacheObject(outTradeNo, JSONUtil.toJsonStr(orderTrade), 10, TimeUnit.SECONDS);
|
||||||
return AjaxResult.success("查询成功", orderTrade.getPayStatus());
|
return AjaxResult.success("查询成功", orderTrade.getOrderStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue