|
|
@@ -3,7 +3,12 @@ package com.cyksj.service.order.impl;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alipay.api.AlipayApiException;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.domain.AlipayTradeWapPayModel;
|
|
|
import com.alipay.api.internal.util.AlipaySignature;
|
|
|
+import com.alipay.api.request.AlipayTradeQueryRequest;
|
|
|
+import com.alipay.api.response.AlipayTradeQueryResponse;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.cyksj.common.constant.Constant;
|
|
|
@@ -11,6 +16,7 @@ import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.snowflake.Sequence;
|
|
|
import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.config.zfb.AliPayClientFactory;
|
|
|
import com.cyksj.config.zfb.BaseZfbConfig;
|
|
|
import com.cyksj.config.zfb.ZfbProductCode;
|
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
|
@@ -240,6 +246,70 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
return orderDon;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void closeOrderDon(long userId, Long orderId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ VipOrderDon orderDon = getById(orderId);
|
|
|
+ if (!userIdList.contains(orderDon.getUserId())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("订单错误");
|
|
|
+ }
|
|
|
+ if (orderDon.getStatus() != VipOrderDon.Status.noPayment) {
|
|
|
+ throw BusinessRuntimeException.getInstance("订单状态错误");
|
|
|
+ }
|
|
|
+ VipOrderDon.Status status = isHasPaid(orderDon);
|
|
|
+ if (status == VipOrderDon.Status.noPayment) {
|
|
|
+ log.info("关闭订单 orderId:{}", orderId);
|
|
|
+ orderDon.setStatus(VipOrderDon.Status.close);
|
|
|
+ updateById(orderDon);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private VipOrderDon.Status isHasPaid(VipOrderDon orderDon) {
|
|
|
+ if (orderDon.getType() == VipOrderDon.Type.ALI_PAY) {
|
|
|
+ try {
|
|
|
+ AlipayTradeQueryResponse zfbOrderDetail = getZfbOrderDetail(orderDon.getShopId(), orderDon.getOrderNo());
|
|
|
+ if (zfbOrderDetail != null) {
|
|
|
+ String tradeStatus = zfbOrderDetail.getTradeStatus();
|
|
|
+ if ("TRADE_SUCCESS".equals(tradeStatus)) {
|
|
|
+ orderDon.setStatus(VipOrderDon.Status.complete);
|
|
|
+ orderDon.setTransactionId(zfbOrderDetail.getTradeNo());
|
|
|
+ Date sendPayDate = zfbOrderDetail.getSendPayDate();
|
|
|
+ orderDon.setPayTime(sendPayDate);
|
|
|
+ //买家支付宝账号
|
|
|
+ String buyerId = Optional.ofNullable(zfbOrderDetail.getBuyerUserId()).orElse(zfbOrderDetail.getBuyerOpenId());
|
|
|
+ orderDon.setBuyerId(buyerId);
|
|
|
+ unifiedHandlerOrderNotify(orderDon);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return orderDon.getStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询支付宝订单详情
|
|
|
+ */
|
|
|
+ public AlipayTradeQueryResponse getZfbOrderDetail(String shopId, String orderNo) throws AlipayApiException {
|
|
|
+ AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(shopId);
|
|
|
+ AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
|
|
+ AlipayTradeWapPayModel bizModel = new AlipayTradeWapPayModel();
|
|
|
+ bizModel.setOutTradeNo(orderNo);
|
|
|
+ request.setBizModel(bizModel);
|
|
|
+ AlipayTradeQueryResponse response;
|
|
|
+ ShopConfig shopConfig = shopConfigMapper.selectOne(Wrappers.lambdaQuery(ShopConfig.class).eq(ShopConfig::getAppId, shopId));
|
|
|
+ if (shopConfig != null && shopConfig.getIsCert()) {
|
|
|
+ response = alipayClient.certificateExecute(request);
|
|
|
+ } else {
|
|
|
+ response = alipayClient.execute(request);
|
|
|
+ }
|
|
|
+ if (response.isSuccess()) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private void unifiedHandlerOrderNotify(VipOrderDon orderDon) {
|
|
|
orderDon.setStatus(VipOrderDon.Status.complete);
|
|
|
this.updateById(orderDon);
|