|
|
@@ -4,6 +4,10 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.domain.AlipayTradeRefundModel;
|
|
|
+import com.alipay.api.request.AlipayTradeRefundRequest;
|
|
|
+import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
@@ -12,6 +16,7 @@ import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.Xmls;
|
|
|
import com.cyksj.config.WeChatConfig;
|
|
|
+import com.cyksj.config.zfb.AliPayClientFactory;
|
|
|
import com.cyksj.dto.RedisKey;
|
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
@@ -45,6 +50,8 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@@ -245,6 +252,31 @@ public class RegisterOrderDonServiceImpl extends ServiceImpl<RegisterOrderDonMap
|
|
|
return couponUserView;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void unifiedRefund(OrderRefundReq refundReq) throws Exception {
|
|
|
+ Long orderId = refundReq.getOrderId();
|
|
|
+ RegisterOrderDon registerOrderDon = this.getById(orderId);
|
|
|
+ if (registerOrderDon == null) throw BusinessRuntimeException.getInstance("订单不存在");
|
|
|
+ if (registerOrderDon.getStatus() == RegisterOrderDon.Status.refund) throw BusinessRuntimeException.getInstance("该订单已退款");
|
|
|
+
|
|
|
+ if (registerOrderDon.getStatus() != RegisterOrderDon.Status.error && registerOrderDon.getStatus() != RegisterOrderDon.Status.hasPayment) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该订单状态不允许退款");
|
|
|
+ }
|
|
|
+ if (registerOrderDon.getMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该订单金额为0,无需退款");
|
|
|
+ }
|
|
|
+ if (!redisService.setNx(String.format("%s", refundReq.getOrderId()), refundReq.getOrderId(), 10l)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请勿重复点击,正在退款中..");
|
|
|
+ }
|
|
|
+ RegisterOrderDon.Type type = registerOrderDon.getType();
|
|
|
+ if (type == RegisterOrderDon.Type.WeChat) {
|
|
|
+ wxRefund(refundReq, registerOrderDon);
|
|
|
+ }
|
|
|
+ if (type == RegisterOrderDon.Type.ALI_PAY) {
|
|
|
+ zfbRefund(refundReq, registerOrderDon);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public H5JsPayParams pay(Long orderId) throws Exception {
|
|
|
log.info("注册 账号调起微信支付[start] orderId:{}", orderId);
|
|
|
@@ -332,23 +364,7 @@ public class RegisterOrderDonServiceImpl extends ServiceImpl<RegisterOrderDonMap
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result<String> wxRefund(OrderRefundReq refundReq) throws Exception {
|
|
|
- RegisterOrderDon registerOrderDon = this.getById(refundReq.getOrderId());
|
|
|
- if (registerOrderDon == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("该订单不存在");
|
|
|
- }
|
|
|
- if (registerOrderDon.getStatus() == RegisterOrderDon.Status.refund) {
|
|
|
- throw BusinessRuntimeException.getInstance("该订单已退款");
|
|
|
- }
|
|
|
- if (registerOrderDon.getStatus() != RegisterOrderDon.Status.error && registerOrderDon.getStatus() != RegisterOrderDon.Status.hasPayment) {
|
|
|
- throw BusinessRuntimeException.getInstance("该订单状态不允许退款");
|
|
|
- }
|
|
|
- if (registerOrderDon.getMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
- throw BusinessRuntimeException.getInstance("该订单金额为0,无需退款");
|
|
|
- }
|
|
|
- if (!redisService.setNx(String.format("%s", refundReq.getOrderId()), refundReq.getOrderId(), 10l)) {
|
|
|
- throw BusinessRuntimeException.getInstance("请勿重复点击,正在退款中..");
|
|
|
- }
|
|
|
+ public Result<String> wxRefund(OrderRefundReq refundReq,RegisterOrderDon registerOrderDon) throws Exception {
|
|
|
long time = System.currentTimeMillis();
|
|
|
//退款订单号
|
|
|
String outRefundNo = Codec.DoDigest.custom()
|
|
|
@@ -395,4 +411,33 @@ public class RegisterOrderDonServiceImpl extends ServiceImpl<RegisterOrderDonMap
|
|
|
}
|
|
|
log.info("微信退款: 退款单号[{}] 退款成功.", outRefundNo);
|
|
|
}
|
|
|
+
|
|
|
+ public void zfbRefund(OrderRefundReq refundReq, RegisterOrderDon registerOrderDon) throws Exception {
|
|
|
+ AlipayTradeRefundResponse response;
|
|
|
+ //支付宝退款
|
|
|
+ AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(Optional.ofNullable(registerOrderDon.getShopId()).orElse(ShopConfig.ZFB_DEFAULT_APP_ID));
|
|
|
+ AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
|
|
+ AlipayTradeRefundModel model = new AlipayTradeRefundModel();
|
|
|
+ //支付宝交易号
|
|
|
+ model.setTradeNo(registerOrderDon.getTransactionId());
|
|
|
+ model.setRefundAmount(refundReq.getRefundMoney().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_EVEN).toString());
|
|
|
+ model.setQueryOptions(List.of("gmt_refund_pay"));
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
+ //退款订单号
|
|
|
+ String outRefundNo = Codec.DoDigest.custom()
|
|
|
+ .setAlgorithm(Codec.DoDigest.Algorithm.MD5)
|
|
|
+ .setStringData(time + String.valueOf(registerOrderDon.getId()))
|
|
|
+ .toHexString();
|
|
|
+ //退款请求号
|
|
|
+ model.setOutRequestNo(outRefundNo);
|
|
|
+ request.setBizModel(model);
|
|
|
+ String shopId = registerOrderDon.getShopId();
|
|
|
+ ShopConfig shopConfig = shopConfigMapper.selectOne(Wrappers.lambdaQuery(ShopConfig.class).eq(ShopConfig::getAppId, registerOrderDon.getShopId()));
|
|
|
+ if (shopId != null && shopConfig != null && shopConfig.getIsCert()) {
|
|
|
+ response = alipayClient.certificateExecute(request);
|
|
|
+ } else {
|
|
|
+ response = alipayClient.execute(request);
|
|
|
+ }
|
|
|
+ if (!response.isSuccess()) throw BusinessRuntimeException.getInstance("退款失败,e:{0}", response.getSubMsg());
|
|
|
+ }
|
|
|
}
|