|
|
@@ -12,6 +12,7 @@ import com.cyksj.config.WeChatConfig;
|
|
|
import com.cyksj.config.zfb.AliPayClientFactory;
|
|
|
import com.cyksj.mapper.*;
|
|
|
import com.cyksj.mapper.channel.ShopConfigMapper;
|
|
|
+import com.cyksj.mapper.vip.VipOrderDonMapper;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.request.OrderRefundReq;
|
|
|
import com.cyksj.service.order.OrderRefundService;
|
|
|
@@ -65,15 +66,17 @@ public class OrderRefundServiceImpl implements OrderRefundService {
|
|
|
|
|
|
private final SpotifyUserUpgradeOrderMapper spotifyUserUpgradeOrderMapper;
|
|
|
|
|
|
+ private final VipOrderDonMapper vipOrderDonMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public String centerRefund(OrderRefundReq refundReq, OrderDon orderDon) throws Exception {
|
|
|
OrderDon.Type type = orderDon.getType();
|
|
|
String refundType = refundReq.getRefundType();
|
|
|
if (type == OrderDon.Type.WeChat && !refundType.equals(OrderDon.Type.BALANCE.getType())) {
|
|
|
- return wxRefund(refundReq, orderDon);
|
|
|
+ return wxRefund(refundReq, orderDon.getId(), orderDon.getShopId(), orderDon.getTransactionId(), orderDon.getMoney());
|
|
|
}
|
|
|
if (type == OrderDon.Type.ALI_PAY && !refundType.equals(OrderDon.Type.BALANCE.getType())) {
|
|
|
- return zfbRefund(refundReq, orderDon);
|
|
|
+ return zfbRefund(refundReq, orderDon.getId(), orderDon.getShopId(), orderDon.getTransactionId(), orderDon.getMoney());
|
|
|
}
|
|
|
if (type == OrderDon.Type.PAYPAL && !refundType.equals(OrderDon.Type.BALANCE.getType())) {
|
|
|
if (!paypalRefund(refundReq, orderDon)) {
|
|
|
@@ -239,51 +242,50 @@ public class OrderRefundServiceImpl implements OrderRefundService {
|
|
|
return outRefundNo;
|
|
|
}
|
|
|
|
|
|
- public String wxRefund(OrderRefundReq refundReq, OrderDon orderDon) throws Exception {
|
|
|
- if (orderDon.getMoney().compareTo(BigDecimal.ZERO) <=0){
|
|
|
+ public String wxRefund(OrderRefundReq refundReq, Long orderId, String shopId, String transactionId, BigDecimal orderMoney) throws Exception {
|
|
|
+ if (orderMoney.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
return StrUtil.EMPTY;
|
|
|
}
|
|
|
long time = System.currentTimeMillis();
|
|
|
//退款订单号
|
|
|
String outRefundNo = Codec.DoDigest.custom()
|
|
|
.setAlgorithm(Codec.DoDigest.Algorithm.MD5)
|
|
|
- .setStringData(time + String.valueOf(orderDon.getId()))
|
|
|
+ .setStringData(time + String.valueOf(orderId))
|
|
|
.toHexString();
|
|
|
weChatService.refundWxOrder(weChatConfig.getAppid(),
|
|
|
- Optional.ofNullable(orderDon.getShopId()).orElse(ShopConfig.WX_DEFAULT_APP_ID),
|
|
|
- orderDon.getTransactionId(),
|
|
|
+ Optional.ofNullable(shopId).orElse(ShopConfig.WX_DEFAULT_APP_ID),
|
|
|
+ transactionId,
|
|
|
outRefundNo,
|
|
|
- orderDon.getMoney(),
|
|
|
+ orderMoney,
|
|
|
refundReq.getRefundMoney(),
|
|
|
weChatConfig.getNotifyDomain() + "manage/order/wx/refund/notify");
|
|
|
//记录退款信息
|
|
|
return outRefundNo;
|
|
|
}
|
|
|
|
|
|
- public String zfbRefund(OrderRefundReq refundReq, OrderDon orderDon) throws Exception {
|
|
|
- if (orderDon.getMoney().compareTo(BigDecimal.ZERO) <=0){
|
|
|
+ public String zfbRefund(OrderRefundReq refundReq, Long orderId, String shopId, String transactionId, BigDecimal orderMoney) throws Exception {
|
|
|
+ if (orderMoney.compareTo(BigDecimal.ZERO) <=0){
|
|
|
return StrUtil.EMPTY;
|
|
|
}
|
|
|
AlipayTradeRefundResponse response;
|
|
|
//支付宝退款
|
|
|
- AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(Optional.ofNullable(orderDon.getShopId()).orElse(ShopConfig.ZFB_DEFAULT_APP_ID));
|
|
|
+ AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(Optional.ofNullable(shopId).orElse(ShopConfig.ZFB_DEFAULT_APP_ID));
|
|
|
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
|
|
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
|
|
|
//支付宝交易号
|
|
|
- model.setTradeNo(orderDon.getTransactionId());
|
|
|
+ model.setTradeNo(transactionId);
|
|
|
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(orderDon.getId()))
|
|
|
+ .setStringData(time + String.valueOf(orderId))
|
|
|
.toHexString();
|
|
|
//退款请求号
|
|
|
model.setOutRequestNo(outRefundNo);
|
|
|
request.setBizModel(model);
|
|
|
- String shopId = orderDon.getShopId();
|
|
|
- ShopConfig shopConfig = shopConfigMapper.selectOne(Wrappers.lambdaQuery(ShopConfig.class).eq(ShopConfig::getAppId, orderDon.getShopId()));
|
|
|
+ ShopConfig shopConfig = shopConfigMapper.selectOne(Wrappers.lambdaQuery(ShopConfig.class).eq(ShopConfig::getAppId, shopId));
|
|
|
if (shopId != null && shopConfig != null && shopConfig.getIsCert()) {
|
|
|
response = alipayClient.certificateExecute(request);
|
|
|
} else {
|
|
|
@@ -478,6 +480,29 @@ public class OrderRefundServiceImpl implements OrderRefundService {
|
|
|
spotifyUserUpgradeOrderMapper.updateById(order);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String vipCenterRefund(OrderRefundReq refundReq, VipOrderDon orderDon) throws Exception {
|
|
|
+ VipOrderDon.Type type = orderDon.getType();
|
|
|
+ String refundType = refundReq.getRefundType();
|
|
|
+ if (type == VipOrderDon.Type.WeChat && !refundType.equals(VipOrderDon.Type.BALANCE.getType())) {
|
|
|
+ return wxRefund(refundReq, orderDon.getId(), orderDon.getShopId(), orderDon.getTransactionId(), orderDon.getMoney());
|
|
|
+ }
|
|
|
+ if (type == VipOrderDon.Type.ALI_PAY && !refundType.equals(VipOrderDon.Type.BALANCE.getType())) {
|
|
|
+ return zfbRefund(refundReq, orderDon.getId(), orderDon.getShopId(), orderDon.getTransactionId(), orderDon.getMoney());
|
|
|
+ }
|
|
|
+ if (type == VipOrderDon.Type.PAYPAL && !refundType.equals(VipOrderDon.Type.BALANCE.getType())) {
|
|
|
+ if (!paypalRefundVipOrder(refundReq, orderDon)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("paypal退款失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (type == VipOrderDon.Type.STRIPE && !refundType.equals(VipOrderDon.Type.BALANCE.getType())) {
|
|
|
+ if (!stripeRefundVipOrder(refundReq, orderDon)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("stripe退款失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return StrUtil.EMPTY;
|
|
|
+ }
|
|
|
+
|
|
|
private String zfbRefundWholesaleOrder(GoodsWholesaleOrderDon orderDon) throws Exception {
|
|
|
BigDecimal money = orderDon.getRefundMoney();
|
|
|
if (money.compareTo(BigDecimal.ZERO) <=0){
|
|
|
@@ -511,4 +536,49 @@ public class OrderRefundServiceImpl implements OrderRefundService {
|
|
|
if (!response.isSuccess()) throw BusinessRuntimeException.getInstance("退款失败,e:{0}", response.getSubMsg());
|
|
|
return outRefundNo;
|
|
|
}
|
|
|
+
|
|
|
+ private boolean stripeRefundVipOrder(OrderRefundReq refundReq, VipOrderDon orderDon) throws StripeException {
|
|
|
+ BigDecimal net = refundReq.getRefundMoney().divide(new BigDecimal(7), 2, RoundingMode.HALF_DOWN);
|
|
|
+ if (!refundReq.getIsDiffer()) {
|
|
|
+ net = net.subtract(orderDon.getAbroadTransactionFee());
|
|
|
+ }
|
|
|
+ log.info("stripe return order:{},money:{}",orderDon.getId(),net);
|
|
|
+ StripePayConfig stripePayConfig = stripePayConfigMapper.selectById(1);
|
|
|
+ Stripe.apiKey = stripePayConfig.getApiKey();
|
|
|
+ RefundCreateParams params =
|
|
|
+ RefundCreateParams.builder()
|
|
|
+ .setCharge(orderDon.getTransactionId())
|
|
|
+ .setAmount(net.longValue())
|
|
|
+ .build();
|
|
|
+ com.stripe.model.Refund refund = com.stripe.model.Refund.create(params);
|
|
|
+ log.info("stripe return refund:{}",refund);
|
|
|
+ if ("succeeded".equals(refund.getStatus())){
|
|
|
+ orderDon.setStatus(VipOrderDon.Status.refund);
|
|
|
+ vipOrderDonMapper.updateById(orderDon);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean paypalRefundVipOrder(OrderRefundReq refundReq, VipOrderDon orderDon) throws PayPalRESTException {
|
|
|
+ BigDecimal net = refundReq.getRefundMoney().divide(new BigDecimal(7), 2, RoundingMode.HALF_DOWN);
|
|
|
+ //非补差价 需要扣除手续费
|
|
|
+ if (!refundReq.getIsDiffer()) {
|
|
|
+ net = net.subtract(orderDon.getAbroadTransactionFee());
|
|
|
+ }
|
|
|
+ log.info("paypal return order:{},money:{}",orderDon.getId(),net);
|
|
|
+ PaypalPayConfig paypalPayConfig = paypalPayConfigMapper.selectById(1);
|
|
|
+ APIContext apiContext = getAPIContext(paypalPayConfig.getClientId(), paypalPayConfig.getClientSecret(),paypalPayConfig.getMode());
|
|
|
+ Sale sale = Sale.get(apiContext, orderDon.getTransactionId());
|
|
|
+ Refund refund = new Refund();
|
|
|
+ refund.setAmount(new Amount("USD", net.divide(new BigDecimal(100),2,RoundingMode.HALF_DOWN).toString()));
|
|
|
+ refund = sale.refund(apiContext, refund);
|
|
|
+ log.info("paypal return refund:{}",refund);
|
|
|
+ if ("completed".equals(refund.getState())){
|
|
|
+ orderDon.setStatus(VipOrderDon.Status.refund);
|
|
|
+ vipOrderDonMapper.updateById(orderDon);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|