zbb 1 месяц назад
Родитель
Сommit
69eda4acb5

BIN
src/doc/Api-V1.8.13 (19) (2).docx


+ 173 - 0
src/main/java/com/vnpay/demo/controller/DemoPayController.java

@@ -178,6 +178,179 @@ public class DemoPayController extends BaseController {
         return "failed";
     }
     
+    /**
+     * 订单查询接口
+     * 用于查询订单状态和支付信息
+     */
+    @PostMapping("/queryOrder")
+    public Map<String, Object> queryOrder(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
+        Map<String, Object> result = new HashMap<>();
+        
+        try {
+            // 获取支付密钥
+            String paySecret = getString_UrlDecode_UTF8("paySecret");
+            if (paySecret == null || paySecret.isEmpty()) {
+                paySecret = PayConfigUtil.readConfig("paySecret");
+            }
+            
+            // 获取查询请求URL
+            String requestUrl = PayConfigUtil.readConfig("queryOrderUrl");
+            if (requestUrl == null || requestUrl.isEmpty()) {
+                requestUrl = "https://c.gmobvfxllc.com/gateway/api/trade";
+            }
+            
+            // 构建查询参数
+            Map<String, String> params = new HashMap<String, String>();
+            params.put("tradeType", "cs.order.query");
+            params.put("version", getString_UrlDecode_UTF8("version"));
+            if (params.get("version") == null || params.get("version").isEmpty()) {
+                params.put("version", "2.0");
+            }
+            params.put("mchNo", getString_UrlDecode_UTF8("mchNo"));
+            params.put("mchOrderNo", getString_UrlDecode_UTF8("mchOrderNo"));
+            
+            // 生成签名
+            Map<String, Object> signMap = new HashMap<String, Object>();
+            signMap.putAll(params);
+            String sign = MerchantApiUtil.getSign(signMap, paySecret);
+            params.put("sign", sign);
+            
+            // 发送查询请求
+            log.info("发送订单查询请求到: " + requestUrl);
+            log.info("查询参数: " + params);
+            String data = HttpUtil.postByHttpClient(requestUrl, params);
+            log.info("查询响应数据: " + data);
+            
+            // 解析响应
+            Map<String, Object> map = JSONObject.parseObject(data);
+            
+            if (map != null && map.size() > 0) {
+                result.putAll(map);
+                
+                if ((int) map.get("status") != 0) {
+                    result.put("success", false);
+                    result.put("message", map.get("message"));
+                    return result;
+                }
+                
+                if ((int) map.get("resultCode") == 0) {
+                    result.put("success", true);
+                    result.put("message", "查询成功");
+                    
+                    // 解析支付状态
+                    String payResult = (String) map.get("payResult");
+                    if ("0".equals(payResult)) {
+                        result.put("payStatus", "支付成功");
+                    } else if ("SUCCESS".equals(payResult)) {
+                        result.put("payStatus", "支付成功");
+                    } else if ("WAITING_PAYMENT".equals(payResult)) {
+                        result.put("payStatus", "等待支付");
+                    } else if ("FAILED".equals(payResult)) {
+                        result.put("payStatus", "支付失败");
+                    } else {
+                        result.put("payStatus", "未知状态: " + payResult);
+                    }
+                    
+                    result.put("orderAmount", map.get("amount"));
+                    result.put("traceTime", map.get("traceTime"));
+                    result.put("payInfo", map.get("payInfo"));
+                } else {
+                    result.put("success", false);
+                    result.put("message", "查询失败: " + map.get("errMsg"));
+                }
+            } else {
+                result.put("success", false);
+                result.put("message", "查询网关无响应");
+            }
+            
+        } catch (Exception e) {
+            log.error("订单查询异常", e);
+            result.put("success", false);
+            result.put("message", "订单查询异常: " + e.getMessage());
+        }
+        
+        return result;
+    }
+    
+    /**
+     * 订单回调接口
+     * 用于接收支付平台的页面回调通知
+     */
+    @GetMapping("/callback")
+    public String callback(HttpServletRequest request, HttpServletResponse response, Model model) {
+        try {
+            log.info("收到订单回调通知,参数: " + request.getQueryString());
+            
+            // 获取回调参数
+            String status = request.getParameter("status");
+            String resultCode = request.getParameter("resultCode");
+            String mchNo = request.getParameter("mchNo");
+            String mchOrderNo = request.getParameter("mchOrderNo");
+            String cpOrderNo = request.getParameter("cpOrderNo");
+            String payResult = request.getParameter("payResult");
+            String amount = request.getParameter("amount");
+            String traceTime = request.getParameter("traceTime");
+            String sign = request.getParameter("sign");
+            
+            // 验证签名
+            String paySecret = PayConfigUtil.readConfig("paySecret");
+            if (paySecret == null || paySecret.isEmpty()) {
+                paySecret = "39ff58d10b904db99259c35d0ea50432";
+            }
+            
+            Map<String, Object> params = new HashMap<>();
+            if (status != null) params.put("status", status);
+            if (resultCode != null) params.put("resultCode", resultCode);
+            if (mchNo != null) params.put("mchNo", mchNo);
+            if (mchOrderNo != null) params.put("mchOrderNo", mchOrderNo);
+            if (cpOrderNo != null) params.put("cpOrderNo", cpOrderNo);
+            if (payResult != null) params.put("payResult", payResult);
+            if (amount != null) params.put("amount", amount);
+            if (traceTime != null) params.put("traceTime", traceTime);
+            
+            String calculatedSign = MerchantApiUtil.getSign(params, paySecret);
+            
+            model.addAttribute("mchOrderNo", mchOrderNo);
+            model.addAttribute("cpOrderNo", cpOrderNo);
+            model.addAttribute("amount", amount);
+            model.addAttribute("traceTime", traceTime);
+            
+            if (calculatedSign.equals(sign)) {
+                log.info("回调签名验证成功,订单号: " + mchOrderNo);
+                
+                // 根据支付结果处理业务逻辑
+                if ("0".equals(payResult) || "SUCCESS".equals(payResult)) {
+                    log.info("订单支付成功: " + mchOrderNo);
+                    model.addAttribute("status", "success");
+                    model.addAttribute("message", "支付成功");
+                    
+                    // TODO: 更新订单状态为已支付
+                    
+                } else if ("WAITING_PAYMENT".equals(payResult)) {
+                    log.info("订单等待支付: " + mchOrderNo);
+                    model.addAttribute("status", "waiting");
+                    model.addAttribute("message", "等待支付");
+                } else {
+                    log.info("订单支付失败: " + mchOrderNo + ", 状态: " + payResult);
+                    model.addAttribute("status", "failed");
+                    model.addAttribute("message", "支付失败");
+                }
+            } else {
+                log.error("回调签名验证失败");
+                model.addAttribute("status", "error");
+                model.addAttribute("message", "签名验证失败");
+            }
+            
+        } catch (Exception e) {
+            log.error("处理订单回调异常", e);
+            model.addAttribute("status", "error");
+            model.addAttribute("message", "处理回调异常: " + e.getMessage());
+        }
+        
+        // 返回回调结果页面
+        return "callback-result";
+    }
+    
     /**
      * 测试接口
      * 用于测试服务是否正常

+ 3 - 0
src/main/resources/application.properties

@@ -26,6 +26,9 @@ paySecret=39ff58d10b904db99259c35d0ea50432
 # API??????
 apiPayUrl=https://c.gmobvfxllc.com/gateway/api/trade
 
+# ????????????
+queryOrderUrl=https://c.gmobvfxllc.com/gateway/api/trade
+
 # ????????
 scanPayUrl=http://localhost:8080/gateway/scanPay/initPay