xiongzhu 4 жил өмнө
parent
commit
afe60d893c

+ 2 - 0
src/main/java/com/izouma/nineth/config/AdapayProperties.java

@@ -14,4 +14,6 @@ public class AdapayProperties {
     private String  publicKey;
     private String  privKey;
     private String  appPublicKey;
+    private String  wxAppId;
+    private String  notifyUrl;
 }

+ 46 - 8
src/main/java/com/izouma/nineth/service/GiftOrderService.java

@@ -10,6 +10,9 @@ import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
 import com.github.binarywang.wxpay.constant.WxPayConstants;
 import com.github.binarywang.wxpay.exception.WxPayException;
 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.WxPayProperties;
 import com.izouma.nineth.domain.Asset;
@@ -19,12 +22,14 @@ import com.izouma.nineth.enums.AssetStatus;
 import com.izouma.nineth.enums.OrderStatus;
 import com.izouma.nineth.enums.PayMethod;
 import com.izouma.nineth.exception.BusinessException;
-import com.izouma.nineth.repo.*;
+import com.izouma.nineth.repo.AssetRepo;
+import com.izouma.nineth.repo.GiftOrderRepo;
+import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.utils.SnowflakeIdWorker;
 import lombok.AllArgsConstructor;
 import org.apache.commons.codec.EncoderException;
 import org.apache.commons.codec.net.URLCodec;
-import org.springframework.context.ApplicationContext;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.core.env.Environment;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -32,9 +37,13 @@ import org.springframework.ui.Model;
 
 import javax.transaction.Transactional;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 @Service
 @AllArgsConstructor
@@ -42,20 +51,15 @@ public class GiftOrderService {
 
     private AssetRepo          assetRepo;
     private UserRepo           userRepo;
-    private NFTService         nftService;
-    private CollectionRepo     collectionRepo;
-    private ApplicationContext applicationContext;
-    private OrderRepo          orderRepo;
     private SysConfigService   sysConfigService;
     private GiftOrderRepo      giftOrderRepo;
-    private TokenHistoryRepo   tokenHistoryRepo;
     private AlipayProperties   alipayProperties;
     private AlipayClient       alipayClient;
     private WxPayProperties    wxPayProperties;
     private WxPayService       wxPayService;
     private Environment        env;
-    private AssetMintService   assetMintService;
     private AssetService       assetService;
+    private AdapayProperties   adapayProperties;
 
     @Transactional
     public GiftOrder gift(Long userId, Long assetId, Long toUserId) {
@@ -173,6 +177,40 @@ public class GiftOrderService {
         throw new BusinessException("不支持此付款方式");
     }
 
+    public Object payAdapay(Long id, String payChannel, String openId) throws BaseAdaPayException {
+        GiftOrder order = giftOrderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
+        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.getGasPrice().setScale(2, RoundingMode.HALF_UP).toPlainString());
+        paymentParams.put("app_id", adapayProperties.getAppId());
+        paymentParams.put("pay_channel", payChannel);
+        paymentParams.put("goods_title", "转赠GAS费");
+        paymentParams.put("time_expire", DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
+                .format(LocalDateTime.now().plusMinutes(5)));
+        paymentParams.put("notify_url", adapayProperties.getNotifyUrl() + "/giftOrder/" + order.getId());
+
+
+        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;
+    }
+
     @Scheduled(fixedRate = 60000)
     public void batchCancel() {
         List<GiftOrder> orders = giftOrderRepo.findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus.NOT_PAID,

+ 19 - 1
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -31,6 +31,7 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.EncoderException;
 import org.apache.commons.codec.net.URLCodec;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.context.event.EventListener;
 import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
@@ -43,6 +44,7 @@ import javax.transaction.Transactional;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -257,6 +259,9 @@ public class OrderService {
         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);
@@ -283,11 +288,24 @@ public class OrderService {
             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;
     }
 
-    private BigDecimal divMoney(BigDecimal totalAmount, BigDecimal restAmount, List<Map<String, Object>> divMembers,
+    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 :

+ 40 - 6
src/main/java/com/izouma/nineth/web/OrderNotifyController.java

@@ -8,6 +8,8 @@ import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
 import com.github.binarywang.wxpay.exception.WxPayException;
 import com.github.binarywang.wxpay.service.WxPayService;
+import com.huifu.adapay.core.AdapayCore;
+import com.huifu.adapay.core.util.AdapaySign;
 import com.izouma.nineth.config.AlipayProperties;
 import com.izouma.nineth.enums.PayMethod;
 import com.izouma.nineth.service.AssetService;
@@ -17,10 +19,7 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections.MapUtils;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.HashMap;
@@ -108,8 +107,43 @@ public class OrderNotifyController {
         return WxPayNotifyResponse.success("OK");
     }
 
-    @PostMapping("/adapay")
-    public void adapayNotify(HttpServletRequest request) {
+    @PostMapping("/adapay/order/{orderId}")
+    @ResponseBody
+    public void adapayNotify(@PathVariable Long orderId, HttpServletRequest request) {
         log.info("adapay notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
+        try {
+            String data = request.getParameter("data");
+            String sign = request.getParameter("sign");
+            boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
+            log.info("checkSign {}", checkSign);
+            if (checkSign) {
+                JSONObject jsonObject = JSON.parseObject(data);
+                String channel = jsonObject.getString("pay_channel");
+                String id = jsonObject.getString("id");
+                orderService.notifyOrder(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @PostMapping("/adapay/giftOrder/{orderId}")
+    @ResponseBody
+    public void adapayGiftNotify(@PathVariable Long orderId, HttpServletRequest request) {
+        log.info("adapay gift notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
+        try {
+            String data = request.getParameter("data");
+            String sign = request.getParameter("sign");
+            boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
+            log.info("checkSign {}", checkSign);
+            if (checkSign) {
+                JSONObject jsonObject = JSON.parseObject(data);
+                String channel = jsonObject.getString("pay_channel");
+                String id = jsonObject.getString("id");
+                giftOrderService.giftNotify(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 }

+ 4 - 1
src/main/resources/application.yaml

@@ -140,6 +140,8 @@ adapay:
   public-key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApyGiKxT+ffKnH/7Nxal8TE461QHr539cOQgDHSanERb9Fr3SxJjUccagGEi5f4kPAMlOMqlcJMGKr5/2hDQYNIkOYeam6MW/E+sZq2S3LZPe04iLglMG3e0OTQRyV8x5WinNadSgb0wiQzMPPrdCYd7AZAb7u5dCj5YhEB7TywQ5Cr8tzQUA+gTMuO5D3q7I0lB+dxwbzaXI3D2RqVF/05i90jZEvfEZKHmxcbqKyuVHWs2Decb2q433GM4koSI1N1iGSW+dgxlMWFk5OrySmayYcOwIViBEKR+6icb5pfwfETEOILh3bad6I8cJBw7WXT1Pa2PIuhB8G2uKjmIKGwIDAQA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G5cMDd3i3RiPeDSQKBgBs1ED+rS83iF5Eduy4H1vKZ94R7wRSty7ERjoGSXK/2fWl2Xp7dwXVEYucBUtQnzg2+XFKQHzY1jH19+SWdCF/UzQmPa2S+n6+ACwHvL1VGtjBpJLN2nccKJZsyzW+imTRhYSEdP6TSZUnay4idzFH8v/tsJHxVkw/ygnn+0PwpAn8uOHsWsrzgioWQYmc/wss1H7ghCX/PNU/IxTOxwb7IRGiXZa5pWqv4sgc0yA5J9L+6mTgUdLnK7ybCbUbWRJY18fAfxOHwi26y10oJEA/wtuBG9H/xHUjkcc1vs5s8TiNi2d73zcpYv3mK3lQ5MVNQ7nIk+Q+QIE3UkBxa0UgpAoGBAMDwg0ebzBEZsV2cr/Er2b25LsXteDJ+V67plBNrv+A1/omA9a52sWek4bY0D+Uu6zPTDaLj9BhHC2wJmThYl0eLRKyDKYQslBR3h253Gsn3If6RH9/tSyDsQ88iAEI1f6QH27bGHL9VDrsLGEFg5E7ZEzFQuJPqoUvBOoURNwa6
   app-public-key: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwN6xgd6Ad8v2hIIsQVnbt8a3JituR8o4Tc3B5WlcFR55bz4OMqrG/356Ur3cPbc2Fe8ArNd/0gZbC9q56Eb16JTkVNA/fye4SXznWxdyBPR7+guuJZHc/VW2fKH2lfZ2P3Tt0QkKZZoawYOGSMdIvO+WqK44updyax0ikK6JlNQIDAQAB
+  wx-app-id:
+  notify-url: https://nfttest.9space.vip/notify/adapay
 ---
 
 spring:
@@ -164,4 +166,5 @@ wx:
 alipay:
   notify-url: https://nft.9space.vip/notify/order/alipay
   return-url: https://nft.9space.vip/9th/home
-
+adapay:
+  notify-url: https://nft.9space.vip/notify/adapay

+ 3 - 2
src/test/java/com/izouma/nineth/service/AdapayServiceTest.java

@@ -34,12 +34,13 @@ public class AdapayServiceTest extends ApplicationTests {
 
         paymentParams.put("app_id", "app_f8760acc-f4d8-46f6-8f70-d80e36517075");
         paymentParams.put("order_no", "jsdk_payment" + System.currentTimeMillis());
-        paymentParams.put("pay_channel", "wx_pub");
+        paymentParams.put("pay_channel", "alipay_qr");
         paymentParams.put("pay_amt", "0.10");
         paymentParams.put("goods_title", "your goods title");
         paymentParams.put("goods_desc", "your goods desc");
         paymentParams.put("div_members", JSON.toJSONString(divMembers));
-        paymentParams.put("notify_url", "http://9th.frp.izouma.com/notify/adapay");
+        paymentParams.put("notify_url", "http://9th.frp.izouma.com/notify/adapay/order/1");
+        paymentParams.put("description", "orderId=1");
 
         Map<String, Object> expend = new HashMap<>();
         expend.put("open_id", "oWJG55wLnwdVzXoKka1-DzQKOd_Y");