|
@@ -2,6 +2,7 @@ package com.izouma.nineth.service;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
import com.alipay.api.AlipayClient;
|
|
import com.alipay.api.AlipayClient;
|
|
|
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
|
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
|
|
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
@@ -11,8 +12,12 @@ import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
|
import com.github.binarywang.wxpay.constant.WxPayConstants;
|
|
import com.github.binarywang.wxpay.constant.WxPayConstants;
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
|
|
+import com.huifu.adapay.core.exception.BaseAdaPayException;
|
|
|
|
|
+import com.huifu.adapay.model.Payment;
|
|
|
|
|
+import com.izouma.nineth.config.AdapayProperties;
|
|
|
import com.izouma.nineth.config.AlipayProperties;
|
|
import com.izouma.nineth.config.AlipayProperties;
|
|
|
import com.izouma.nineth.config.WxPayProperties;
|
|
import com.izouma.nineth.config.WxPayProperties;
|
|
|
|
|
+import com.izouma.nineth.domain.Collection;
|
|
|
import com.izouma.nineth.domain.*;
|
|
import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.enums.*;
|
|
import com.izouma.nineth.enums.*;
|
|
@@ -26,6 +31,7 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.EncoderException;
|
|
import org.apache.commons.codec.EncoderException;
|
|
|
import org.apache.commons.codec.net.URLCodec;
|
|
import org.apache.commons.codec.net.URLCodec;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
@@ -38,9 +44,8 @@ import javax.transaction.Transactional;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@@ -65,6 +70,7 @@ public class OrderService {
|
|
|
private CollectionService collectionService;
|
|
private CollectionService collectionService;
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
private CommissionRecordRepo commissionRecordRepo;
|
|
private CommissionRecordRepo commissionRecordRepo;
|
|
|
|
|
+ private AdapayProperties adapayProperties;
|
|
|
|
|
|
|
|
public Page<Order> all(PageQuery pageQuery) {
|
|
public Page<Order> all(PageQuery pageQuery) {
|
|
|
return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
|
|
return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
|
|
@@ -238,6 +244,84 @@ public class OrderService {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public Object payAdapay(Long id, String payChannel, String openId) throws BaseAdaPayException {
|
|
|
|
|
+ Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
|
|
|
|
|
+ Collection collection = collectionRepo.findById(order.getCollectionId())
|
|
|
|
|
+ .orElseThrow(new BusinessException("藏品不存在"));
|
|
|
|
|
+ User invitor = userRepo.findById(order.getInvitor()).orElse(null);
|
|
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> paymentParams = new HashMap<>();
|
|
|
|
|
+ paymentParams.put("order_no", String.valueOf(new SnowflakeIdWorker(0, 0).nextId()));
|
|
|
|
|
+ paymentParams.put("pay_amt", order.getTotalPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
|
|
|
|
|
+ paymentParams.put("app_id", adapayProperties.getAppId());
|
|
|
|
|
+ paymentParams.put("pay_channel", payChannel);
|
|
|
|
|
+ paymentParams.put("goods_title", collection.getName());
|
|
|
|
|
+ paymentParams.put("time_expire", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
|
|
|
|
|
+ .format(LocalDateTime.now().plusMinutes(5)));
|
|
|
|
|
+ paymentParams.put("notify_url", adapayProperties.getNotifyUrl() + "/order/" + order.getId());
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> divMembers = new ArrayList<>();
|
|
|
|
|
+ BigDecimal restAmount = order.getTotalPrice().setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+
|
|
|
|
|
+ if (collection.getSource().equals(CollectionSource.TRANSFER)) {
|
|
|
|
|
+ Asset asset = assetRepo.findById(collection.getAssetId()).orElseThrow(new BusinessException("无记录"));
|
|
|
|
|
+ User owner = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("拥有者用户不存在"));
|
|
|
|
|
+
|
|
|
|
|
+ restAmount = divMoney(order.getTotalPrice(), restAmount, divMembers, "0",
|
|
|
|
|
+ collection.getServiceCharge() + collection.getRoyalties(), true);
|
|
|
|
|
+ restAmount = divMoney(order.getTotalPrice(), restAmount, divMembers, owner.getMemberId(),
|
|
|
|
|
+ -1, false);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (invitor != null) {
|
|
|
|
|
+ restAmount = divMoney(order.getTotalPrice(), restAmount, divMembers, invitor.getMemberId(),
|
|
|
|
|
+ invitor.getShareRatio().intValue(), false);
|
|
|
|
|
+ }
|
|
|
|
|
+ restAmount = divMoney(order.getTotalPrice(), restAmount, divMembers, "0",
|
|
|
|
|
+ -1, true);
|
|
|
|
|
+ }
|
|
|
|
|
+ paymentParams.put("div_members", JSON.toJSONString(divMembers));
|
|
|
|
|
+ if (restAmount.compareTo(BigDecimal.ZERO) != 0) {
|
|
|
|
|
+ log.error("分账出错 {}", JSON.toJSONString(divMembers, SerializerFeature.PrettyFormat));
|
|
|
|
|
+ throw new BusinessException("分账出错");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ("wx_pub".equals(payChannel) || "wx_lite".equals(payChannel)) {
|
|
|
|
|
+ if (StringUtils.isBlank(openId)) {
|
|
|
|
|
+ throw new BusinessException("缺少openId");
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> expend = new HashMap<>();
|
|
|
|
|
+ expend.put("open_id", openId);
|
|
|
|
|
+ expend.put("limit_pay", "1");
|
|
|
|
|
+ if ("wx_lite".equals(payChannel)) {
|
|
|
|
|
+ expend.put("wx_app_id", adapayProperties.getWxAppId());
|
|
|
|
|
+ }
|
|
|
|
|
+ paymentParams.put("expend", expend);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> response = Payment.create(paymentParams);
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static BigDecimal divMoney(BigDecimal totalAmount, BigDecimal restAmount, List<Map<String, Object>> divMembers,
|
|
|
|
|
+ String memberId, int ratio, boolean feeFlag) {
|
|
|
|
|
+ if (ratio == -1 || (ratio > 0 && ratio < 100)) {
|
|
|
|
|
+ BigDecimal divAmount = ratio == -1 ? restAmount :
|
|
|
|
|
+ totalAmount.multiply(BigDecimal.valueOf(ratio))
|
|
|
|
|
+ .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
|
|
|
|
|
+ Map<String, Object> divMem = new HashMap<>();
|
|
|
|
|
+ divMem.put("member_id", memberId);
|
|
|
|
|
+ divMem.put("amount", divAmount.toPlainString());
|
|
|
|
|
+ divMem.put("fee_flag", feeFlag ? "Y" : "N");
|
|
|
|
|
+ divMembers.add(divMem);
|
|
|
|
|
+ return restAmount.subtract(divAmount);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new BusinessException("分账比例错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
|
|
public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
|
|
|
Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|