TRX il y a 1 an
Parent
commit
470b348c90

+ 2 - 2
PaymentServer/src/main/java/com/zhongshu/payment/server/core/controller/unionFrictionlessPay/FrictionlessNotifyController.java

@@ -34,8 +34,8 @@ public class FrictionlessNotifyController {
     public void signNotify(
             HttpServletRequest request,
             @PathVariable(name = "contractNo") String contractNo) {
-        log.info("收到微信支付通知 appid:{}", contractNo);
-        unionNotifyService.signNotify(request);
+        log.info("商户签约通知回调 contractNo:{}", contractNo);
+        unionNotifyService.signNotify(request, contractNo);
     }
 
     //---------------------------通知 start-----------------------------

+ 7 - 0
PaymentServer/src/main/java/com/zhongshu/payment/server/core/domain/unionFrictionlessPay/UnionUserOpenInfo.java

@@ -2,6 +2,7 @@ package com.zhongshu.payment.server.core.domain.unionFrictionlessPay;
 
 import com.github.microservice.types.unionFrictionlessPayType.ContractState;
 import com.github.microservice.types.unionFrictionlessPayType.UnionUserOpenType;
+import com.zhongshu.card.client.model.org.UserCountModel;
 import com.zhongshu.payment.server.core.domain.base.SuperMain;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
@@ -26,6 +27,12 @@ public class UnionUserOpenInfo extends SuperMain {
     @Schema(description = "用户ID")
     private String userId;
 
+    @Schema(description = "用户名称")
+    private String userName;
+
+    @Schema(description = "用户信息")
+    private UserCountModel userCount;
+
     @Schema(description = "商户代码")
     private String mid;
 

+ 26 - 39
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/pay/impl/unionFrictionlessPay/UnionFrictionlessPayMainService.java

@@ -6,6 +6,7 @@ import com.github.microservice.app.stream.StreamHelper;
 import com.github.microservice.models.requestModel.SuperResponseModel;
 import com.github.microservice.models.type.PaymentType;
 import com.github.microservice.net.ResultContent;
+import com.zhongshu.card.client.model.org.UserCountModel;
 import com.zhongshu.card.client.service.feign.OrganizationFeignService;
 import com.zhongshu.card.client.service.feign.UserFeignService;
 import com.zhongshu.card.client.utils.DateUtils;
@@ -30,6 +31,7 @@ import com.zhongshu.payment.server.core.utils.AesUtils;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -76,6 +78,11 @@ public class UnionFrictionlessPayMainService extends SuperPayService {
     public ResultContent sign(SignInParam param) {
         String userId = getCurrentUserId();
         String shopOid = param.getShopOid();
+        ResultContent<UserCountModel> resultContent = userFeignService.getUserDetailByUserId(userId);
+        if (resultContent.isFailed()) {
+            return ResultContent.buildFail(String.format("查询用户信息出错:%s", resultContent.getMsg()));
+        }
+        UserCountModel userCountModel = resultContent.getContent();
 
         // 验证学校信息
         String contractNo = collectionIdService.getNextNo();
@@ -84,9 +91,17 @@ public class UnionFrictionlessPayMainService extends SuperPayService {
             unionUserOpenInfo = new UnionUserOpenInfo();
             unionUserOpenInfo.setShopOid(shopOid);
             unionUserOpenInfo.setUserId(userId);
-            unionUserOpenInfo.setUnionUserOpenType(UnionUserOpenType.Shop);
+            unionUserOpenInfo.setUnionUserOpenType(UnionUserOpenType.User);
             unionUserOpenInfo.setContractState(ContractState.UNSIGNED);
         }
+        unionUserOpenInfo.setUserName(userCountModel.getName());
+        unionUserOpenInfo.setUserCount(userCountModel);
+        // 判断签约状态
+        if (unionUserOpenInfo.getContractState() == ContractState.SIGNED) {
+            unionUserOpenInfoDao.save(unionUserOpenInfo);
+            return ResultContent.buildFail("当前用户已签约");
+        }
+
         unionUserOpenInfo.setContractNo(contractNo);
         Date expireDate = new Date(System.currentTimeMillis() + 60 * 1000);
         unionUserOpenInfo.setExpiredTime(expireDate.getTime());
@@ -102,12 +117,15 @@ public class UnionFrictionlessPayMainService extends SuperPayService {
         signParam.setInvokeScene(UnionPaymentConfig.invokeScene);
         signParam.setMchntWxMpAppId(UnionPaymentConfig.mchntWxMpAppId);
         signParam.setMchntWxMpPath(UnionPaymentConfig.mchntWxMpPath);
-        JSONObject jsonObject = new JSONObject();
-        jsonObject.set("certifTp", "01");
-        jsonObject.set("certifId", "511304198608153312");
-        jsonObject.set("customerNm", "唐仁祥");
-        signParam.setCustInfo(AesUtils.base64Encode(jsonObject.toString()));
-
+        // 身份信息
+        String cardNumber = userCountModel.getCardNumber();
+        if (StringUtils.isNotEmpty(cardNumber)) {
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.set("certifTp", "01");
+            jsonObject.set("certifId", cardNumber);
+            jsonObject.set("customerNm", userCountModel.getRealName());
+            signParam.setCustInfo(AesUtils.base64Encode(jsonObject.toString()));
+        }
         String url = payApiConfig.getUrl(UnionFrictionlessPayApiConfig.sign);
         SuperResponseModel requestAPI = unionRequestService.requestUnionAPI(url, signParam, SignResponse.class);
         if (requestAPI.isFailed()) {
@@ -116,38 +134,7 @@ public class UnionFrictionlessPayMainService extends SuperPayService {
         SignResponse response = (SignResponse) requestAPI;
         log.info("response: {}", JSONUtil.toJsonStr(response));
         unionUserOpenInfoDao.save(unionUserOpenInfo);
-
-
-//        JSONObject jsonObject = new JSONObject();
-//        jsonObject.set("walletOption", "SINGLE");
-//        jsonObject.set("billNo", "31940000201700002");
-//        jsonObject.set("billDate", "2017-06-26");
-//        jsonObject.set("sign", "2631915B7F7822C4B00A488A32E03764");
-//        jsonObject.set("requestTimestamp", "2017-06-26 17:28:02");
-//        jsonObject.set("instMid", "QRPAYDEFAULT");
-//        jsonObject.set("msgSrc", "WWW.TEST.COM");
-//        jsonObject.set("totalAmount", "1");
-//
-//        JSONArray array = new JSONArray();
-//        JSONObject good = new JSONObject();
-//        good.set("body", "微信二维码测试");
-//        good.set("price", "1");
-//        good.set("goodsName", "微信二维码测试");
-//        good.set("goodsId", "1");
-//        good.set("quantity", "1");
-//        good.set("goodsCategory", "TEST");
-//        array.add(good);
-//        jsonObject.set("goods", array);
-//
-//        jsonObject.set("msgType", "bills.getQRCode");
-//        jsonObject.set("mid", "898340149000005");
-//        jsonObject.set("tid", "88880001");
-//        jsonObject.set("payKey", "fcAmtnx7MwismjWNhNKdHC44mNXtnEQeJkRrhKJwyrW2ysRR");
-//
-//        String sign1 = unionRequestService.signByJSONObject(jsonObject);
-//        log.info("sign1: {}", sign1);
-
-        return ResultContent.buildSuccess();
+        return ResultContent.buildSuccess(response);
     }
 
     /**

+ 30 - 5
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/pay/impl/unionFrictionlessPay/UnionNotifyService.java

@@ -1,5 +1,7 @@
 package com.zhongshu.payment.server.core.service.pay.impl.unionFrictionlessPay;
 
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import com.github.microservice.app.stream.StreamHelper;
 import com.github.microservice.core.helper.JsonHelper;
 import com.github.microservice.net.ResultContent;
@@ -11,11 +13,15 @@ import jakarta.servlet.ServletInputStream;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
+import java.util.Enumeration;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 /**
  * @author TRX
@@ -38,7 +44,8 @@ public class UnionNotifyService {
      * @return
      */
     @SneakyThrows
-    public ResultContent signNotify(HttpServletRequest request) {
+    public ResultContent signNotify(HttpServletRequest request, String contractNo) {
+
         ServletInputStream inputStream = request.getInputStream();
         StringBuffer stringBuffer = new StringBuffer();
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
@@ -47,13 +54,31 @@ public class UnionNotifyService {
         while ((s = bufferedReader.readLine()) != null) {
             stringBuffer.append(s);
         }
-        String body = stringBuffer.toString();
-        log.info("签约通知字符串: {}", body);
-        SignNotifyParam signNotifyParam = jsonHelper.toObject(body, SignNotifyParam.class);
+        log.info("stringBuffer: {}", stringBuffer.toString());
+        Enumeration<String> exception = request.getHeaderNames();
+        while (exception.hasMoreElements()) {
+            String key = exception.nextElement();
+            String value = request.getHeader(key);
+            log.info("key: {}, value: {}", key, value);
+        }
+
+        String authorization = request.getHeader("X-Authorization");
+        Map<String, String[]> map = request.getParameterMap();
+        Map<String, String> paramMap = new LinkedHashMap<>();
+        map.forEach((String key, String[] values) -> {
+            log.info("key: {}, values: {}", key, values);
+            paramMap.put(key, values[0]);
+        });
+        JSONObject jsonObject = new JSONObject(paramMap);
+
+
+        SignNotifyParam signNotifyParam = new SignNotifyParam();
+        BeanUtils.copyProperties(paramMap, signNotifyParam);
+        log.info("签约通知字符串:{} {}", contractNo, paramMap);
+
 
         SignModel signModel = new SignModel();
         streamHelper.send(PaymentStreamType.UnionFrictionStateStream.name(), signModel);
-
         return ResultContent.buildSuccess();
     }
 

+ 93 - 29
PaymentServer/src/main/java/com/zhongshu/payment/server/core/service/pay/impl/unionFrictionlessPay/UnionRequestService.java

@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
+import com.github.microservice.core.helper.JsonHelper;
 import com.github.microservice.core.util.net.apache.HttpClientUtil;
 import com.github.microservice.core.util.net.apache.HttpModel;
 import com.github.microservice.core.util.net.apache.MethodType;
@@ -50,6 +51,9 @@ public class UnionRequestService extends SuperService {
 
     private static final List<String> exitKeys = List.of("signType", "sign", "payKey");
 
+    @Autowired
+    JsonHelper jsonHelper;
+
     /**
      * 银联请求
      *
@@ -71,20 +75,8 @@ public class UnionRequestService extends SuperService {
             ApiConfParam apiConfParam = new ApiConfParam();
             apiConfParam.setApiName(url);
             apiConfParam.setMethodType(MethodType.Json);
-
-//            data = "A";
-//            JSONObject jsonObject = new JSONObject();
-//            jsonObject.set("requestTimestamp", "2024-08-30 14:54:41");
-//            data = jsonObject.toString();
-//            log.info("data: {}", data);
-
-//            String sign = sign(JSONUtil.parseObj(JSONUtil.toJsonStr(data)));
-            String str = JSONUtil.toJsonStr(data);
-            log.info("str: {}", str);
-            String sign = AesUtils.signData(str);
-            log.info("sign: {}", sign);
-//            url += "?requestTimestamp=2024-08-30 14:54:41";
-
+            String dataJsonStr = JSONUtil.toJsonStr(data);
+            String sign = AesUtils.signData(dataJsonStr);
             // 请求时间记时
             StopWatch stopWatch = new StopWatch();
             stopWatch.start();
@@ -94,30 +86,100 @@ public class UnionRequestService extends SuperService {
             Map<String, Object> header = new HashMap<>();
             String c = String.format("%s%s%s%s", UnionPaymentConfig.appId, timestamp, nonce, sign);
 
-            log.info("appId: {}", UnionPaymentConfig.appId);
-            log.info("timestamp: {}", timestamp);
-            log.info("nonce: {}", nonce);
-            log.info("appKey: {}", UnionPaymentConfig.appKey);
-
             String signature = AesUtils.signMacSHA256(c, UnionPaymentConfig.appKey);
-            log.info("signature: {}", signature);
-
-            String sig = "OPEN-BODY-SIG AppId=" + "\"" + UnionPaymentConfig.appId + "\""
+            String authorization = "OPEN-BODY-SIG AppId=" + "\"" + UnionPaymentConfig.appId + "\""
                     + ",Timestamp=" + "\"" + timestamp + "\""
                     + ",Nonce=" + "\"" + nonce + "\""
                     + ",Signature=" + "\"" + signature + "\"";
-            log.info("sig: {}", sig);
+            header.put("authorization", authorization);
+            String response = "";
+            PrintWriter out = null;
+            BufferedReader in = null;
+            int code = 400;
+            try {
+                URL realUrl = new URL(url);
+                URLConnection conn = realUrl.openConnection();
+                HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
+                httpUrlConnection.setRequestProperty("Content-Type", "application/json");
+                httpUrlConnection.setRequestProperty("authorization", authorization);
+                httpUrlConnection.setDoOutput(true);
+                httpUrlConnection.setDoInput(true);
+                out = new PrintWriter(httpUrlConnection.getOutputStream());
+                out.write(dataJsonStr);
+                out.flush();
+                httpUrlConnection.connect();
+                code = httpUrlConnection.getResponseCode();
+                in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
+                String line;
+                while ((line = in.readLine()) != null) {
+                    response += line;
+                }
+                log.info("response: {}", response);
+                responseModel = jsonHelper.toObject(response, tClass);
+                responseModel.setCode(code);
+            } catch (Exception e) {
+                e.printStackTrace();
+            } finally {
+                try {
+                    if (out != null) {
+                        out.close();
+                    }
+                    if (in != null) {
+                        in.close();
+                    }
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                }
+            }
+            log.info("response: {} {}", responseModel.getErrCode(), code);
+            responseModel.setCode(code);
+            stopWatch.stop();
+            responseModel.setMillis(stopWatch.getTotalTimeMillis());
+        } catch (Exception e) {
+            e.printStackTrace();
+            responseModel.setFailed(String.format("请求出错:%s", e.getMessage()));
+            return responseModel;
+        }
+        param.setMillis(responseModel.getMillis());
+        param.setIsSuccess(responseModel.isSuccess);
+        param.setMsg(responseModel.getMsg());
+        param.setResponse(responseModel);
+        param.setRequestType(RequestType.UnionFrictionlessPay);
+        // 请求日志
+        requestInfoFeignService.saveRequestInfo(param);
+        return responseModel;
+    }
+
 
-            header.put("authorization", sig);
-            header.put("Content-Type", "application/json");
-            header.put("Accept_Charset", "UTF-8");
+    /**
+     * 通用请求
+     *
+     * @param url
+     * @param data
+     * @param tClass
+     * @return
+     */
+    public SuperResponseModel requestAPI(String url,
+            Object data,
+            Class<? extends SuperResponseModel> tClass) {
+        RequestInfoSaveParam param = new RequestInfoSaveParam();
+        param.setParam(data);
+        param.setApiParam(url);
+        param.setUserId(getCurrentUserId());
+        SuperResponseModel responseModel = null;
+        try {
+            responseModel = tClass.newInstance();
+            ApiConfParam apiConfParam = new ApiConfParam();
+            apiConfParam.setApiName(url);
+            apiConfParam.setMethodType(MethodType.Json);
+
+            // 请求时间记时
+            StopWatch stopWatch = new StopWatch();
+            stopWatch.start();
 
-            log.info("url: {}", url);
             HttpModel httpModel = HttpModel.builder()
                     .url(url).method(apiConfParam.getMethodType())
                     .charset("utf-8").body(data).build();
-
-            httpModel.setHeader(header);
             ResponseModel request = HttpClientUtil.request(httpModel);
             int code = request.getCode();
             if (code == 200) {
@@ -145,12 +207,14 @@ public class UnionRequestService extends SuperService {
         return responseModel;
     }
 
+
     @SneakyThrows
     public String signParam(UnionSuperParam param) {
         JSONObject entries = JSONUtil.parseObj(param);
         return signByJSONObject(entries);
     }
 
+
     @SneakyThrows
     public String signByJSONObject(JSONObject entries) {
         TreeMap<String, Object> map = new TreeMap<>();