xiongzhu преди 4 години
родител
ревизия
75f8625d5b

+ 1 - 2
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -77,8 +77,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/404").permitAll()
                 .antMatchers("/500").permitAll()
                 .antMatchers("/MP_verify*").permitAll()
-                .antMatchers("/order/notify/*").permitAll()
-                .antMatchers("/payOrder/alipay").permitAll()
+                .antMatchers("/notify/**").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 1 - 4
src/main/java/com/izouma/nineth/web/OrderController.java

@@ -1,18 +1,15 @@
 package com.izouma.nineth.web;
 
-import com.alipay.api.AlipayApiException;
 import com.izouma.nineth.domain.Order;
-import com.izouma.nineth.enums.PayMethod;
-import com.izouma.nineth.service.OrderService;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.OrderRepo;
+import com.izouma.nineth.service.OrderService;
 import com.izouma.nineth.utils.ObjUtils;
 import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
-import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;

+ 68 - 0
src/main/java/com/izouma/nineth/web/OrderNotifyController.java

@@ -0,0 +1,68 @@
+package com.izouma.nineth.web;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alipay.api.AlipayApiException;
+import com.alipay.api.internal.util.AlipaySignature;
+import com.izouma.nineth.config.AlipayProperties;
+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.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
+
+@Slf4j
+@Controller
+@RequestMapping("/notify")
+@AllArgsConstructor
+public class OrderNotifyController {
+
+    private final AlipayProperties alipayProperties;
+
+    @PostMapping("/order/alipay")
+    @ResponseBody
+    public String notify(HttpServletRequest request) throws AlipayApiException {
+        Map<String, String> params = new HashMap<>();
+        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
+        for (Map.Entry<String, String[]> entry : entrySet) {
+            String name = entry.getKey();
+            String[] values = entry.getValue();
+            int valLen = values.length;
+
+            if (valLen == 1) {
+                params.put(name, values[0]);
+            } else if (valLen > 1) {
+                StringBuilder sb = new StringBuilder();
+                for (String val : values) {
+                    sb.append(",").append(val);
+                }
+                params.put(name, sb.substring(1));
+            } else {
+                params.put(name, "");
+            }
+        }
+        log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
+        AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
+        if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
+            JSONObject body = JSON.parseObject(params.get("body"));
+            String type = body.getString("type");
+            switch (type) {
+                case "deposit":
+                    break;
+                case "recharge":
+                    break;
+            }
+            return "success";
+        }
+        return "error";
+    }
+}

+ 1 - 0
src/main/java/com/izouma/nineth/web/OrderPayController.java

@@ -36,4 +36,5 @@ public class OrderPayController {
     public Object payOrderWeixin(@RequestParam Long id) {
         return orderService.payOrderWeixin(id);
     }
+
 }

+ 2 - 2
src/main/resources/application.yaml

@@ -103,7 +103,7 @@ mychain:
 #  appCertPath: classpath:cert/appCertPublicKey_2021002120645023.crt
 #  aliPubCertPath: classpath:cert/alipayCertPublicKey_RSA2.crt
 #  rootCertPath: classpath:cert/alipayRootCert.crt
-#  notifyUrl: http://9th.izouma.com/alipay/notify/alipay
+#  notifyUrl: http://9th.izouma.com/notify/alipay
 #  returnUrl: http://9th.izouma.com/9th/orders
 alipay:
   app-id: 2021002186606637
@@ -115,7 +115,7 @@ alipay:
   appCertPath: classpath:cert/appCertPublicKey_2021002120645023.crt
   aliPubCertPath: classpath:cert/alipayCertPublicKey_RSA2.crt
   rootCertPath: classpath:cert/alipayRootCert.crt
-  notifyUrl: http://9th.izouma.com/alipay/notify/alipay
+  notifyUrl: http://9th.izouma.com/notify/alipay
   returnUrl: http://9th.izouma.com/9th/orders
 ---