From 3f17034c768ba4fc330e01f014b4f880e6a1569c Mon Sep 17 00:00:00 2001
From: Administrator <zhubaomin>
Date: 星期二, 12 三月 2024 16:03:30 +0800
Subject: [PATCH] 2024-03-12 朱宝民 增加已挂失,未补卡接口

---
 pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java |  239 ++++++++++++++++++++++++++---------------------------------
 1 files changed, 106 insertions(+), 133 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
index 5258444..4aad549 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sell/src/main/java/com/dy/pipIrrSell/wechatpay/PaymentCtrl.java
@@ -1,11 +1,12 @@
 package com.dy.pipIrrSell.wechatpay;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.dy.common.aop.SsoAop;
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
 import com.dy.common.webUtil.ResultCodeMsg;
+import com.dy.pipIrrGlobal.pojoSe.SeVcRefund;
+import com.dy.pipIrrGlobal.pojoSe.SeVcRefundItem;
 import com.dy.pipIrrGlobal.pojoSe.SeVirtualCard;
 import com.dy.pipIrrGlobal.pojoSe.SeWebchatLogonState;
 import com.dy.pipIrrGlobal.voSe.VoClient;
@@ -16,7 +17,12 @@
 import com.dy.pipIrrSell.util.RestTemplateUtil;
 import com.dy.pipIrrSell.virtualCard.VirtualCardSv;
 import com.dy.pipIrrSell.virtualCard.dto.DtoVirtualCard;
-import com.dy.pipIrrSell.wechatpay.dto.*;
+import com.dy.pipIrrSell.virtualCard.enums.LastOperateENUM;
+import com.dy.pipIrrSell.virtualCard.enums.RefundItemStateENUM;
+import com.dy.pipIrrSell.wallet.enums.RefundStatusENUM;
+import com.dy.pipIrrSell.wechatpay.dto.Code2Session;
+import com.dy.pipIrrSell.wechatpay.dto.DtoOrder;
+import com.dy.pipIrrSell.wechatpay.dto.OrderNotify;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.media.Content;
 import io.swagger.v3.oas.annotations.media.Schema;
@@ -65,25 +71,23 @@
     private final VirtualCardSv virtualCardSv;
     private final ClientSv clientSv;
 
-    private String privateCertFileName = PayInfo.privateCertFileName;
-    private String appid = PayInfo.appid;
-    private String mchid = PayInfo.mchid;
-    private String schema = PayInfo.schema;
-    private String signType = PayInfo.signType;
-    private String description = PayInfo.description;
-    private String loginUrl = PayInfo.loginUrl;
-    private String notifyUrl = PayInfo.notifyUrl;
-    private String grantType = PayInfo.grantType;
-    private String refundUrl = PayInfo.refundUrl;
+    private final String privateCertFileName = PayInfo.privateCertFileName;
+    private final String appid = PayInfo.appid;
+    private final String mchid = PayInfo.mchid;
+    private final String schema = PayInfo.schema;
+    private final String signType = PayInfo.signType;
+    private final String description = PayInfo.description;
+    private final String loginUrl = PayInfo.loginUrl;
+    private final String notifyUrl = PayInfo.notifyUrl;
+    private final String grantType = PayInfo.grantType;
 
     // 骞冲彴璇佷功鍏挜
-    private Map CERTIFICATE_MAP = new HashMap();
+    private final Map CERTIFICATE_MAP = new HashMap();
 
     /**
      * 鐧诲綍鍑瘉鏍¢獙
-     * @param appid 灏忕▼搴� appId
-     * @param secret 灏忕▼搴� appSecret
-     * @param js_code 涓存椂鐧诲綍鍑瘉code
+     * @param code2Session 鐧诲綍鍑瘉鏍¢獙浼犲叆瀵硅薄
+     * @param bindingResult
      * @return
      * @throws Exception
      */
@@ -99,11 +103,15 @@
     @PostMapping(path = "getSessionId")
     @Transactional(rollbackFor = Exception.class)
     @SsoAop()
-    public BaseResponse<Boolean> getSessionId(@RequestParam("appid")  String appid, @RequestParam("secret") String secret, @RequestParam("js_code") String js_code) throws Exception {
+    public BaseResponse<Boolean> getSessionId(@RequestBody @Valid Code2Session code2Session, BindingResult bindingResult) throws Exception {
+        if(bindingResult != null && bindingResult.hasErrors()){
+            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+        }
+
         Map<String, Object> queryParams = new HashMap<>();
         queryParams.put("appid", appid);
-        queryParams.put("secret", secret);
-        queryParams.put("js_code", js_code);
+        queryParams.put("secret", code2Session.getSecret());
+        queryParams.put("js_code", code2Session.getJs_code());
         queryParams.put("grant_type", grantType);
         Map<String, String> headerParams = new HashMap<>();
         JSONObject job = restTemplateUtil.get(loginUrl, queryParams, headerParams);
@@ -116,20 +124,20 @@
         String sessionKey = job.getString("session_key");
 
         // 妫�楠岀櫥褰曟��
-        JSONObject checkSessionKey = payHelper.checkSessionKey(appid, secret, openid, sessionKey);
-        if(checkSessionKey != null) {
-            Integer errcode = checkSessionKey.getInteger("errcode");
-            String errmsg = checkSessionKey.getString("errmsg");
-        }
+        //JSONObject checkSessionKey = payHelper.checkSessionKey(appid, secret, openid, sessionKey);
+        //if(checkSessionKey != null) {
+        //    Integer errcode = checkSessionKey.getInteger("errcode");
+        //    String errmsg = checkSessionKey.getString("errmsg");
+        //}
 
         // 閲嶇疆鐧诲綍鎬�
-        JSONObject resetUserSessionKey = payHelper.resetUserSessionKey(appid, secret, openid, sessionKey);
-        if(resetUserSessionKey != null) {
-            Integer errcode = checkSessionKey.getInteger("errcode");
-            String errmsg = checkSessionKey.getString("errmsg");
-            String openid_New = checkSessionKey.getString("openid");
-            String sessionKey_New = checkSessionKey.getString("session_key");
-        }
+        //JSONObject resetUserSessionKey = payHelper.resetUserSessionKey(appid, secret, openid, sessionKey);
+        //if(resetUserSessionKey != null) {
+        //    Integer errcode = checkSessionKey.getInteger("errcode");
+        //    String errmsg = checkSessionKey.getString("errmsg");
+        //    String openid_New = checkSessionKey.getString("openid");
+        //    String sessionKey_New = checkSessionKey.getString("session_key");
+        //}
 
         // 娣诲姞鐧诲綍鎬佽褰�
         SeWebchatLogonState po = new SeWebchatLogonState();
@@ -277,110 +285,14 @@
         headers.put("Accept", "application/json");
         headers.put("Content-Type", "application/json");
 
-        JSONObject job_result = restTemplateUtil.post(PayInfo.orderUrl, body, headers);
-        if(job_result != null) {
-            System.out.println(job_result.toString());
-            prepayId = job_result.getString("prepay_id");
-        }
+        // 鏆傛椂娉ㄩ噴鎺夛紝璁よ瘉閫氳繃鍚庡啀鏀惧紑
+        //JSONObject job_result = restTemplateUtil.post(PayInfo.orderUrl, body, headers);
+        //if(job_result != null) {
+        //    System.out.println(job_result.toString());
+        //    prepayId = job_result.getString("prepay_id");
+        //}
 
         return BaseResponseUtils.buildSuccess(prepayId) ;
-    }
-
-    /**
-     * 鐢宠閫�娆�
-     * @param po 閫�娆捐姹傚璞�
-     * @param bindingResult
-     * @return
-     * @throws NoSuchPaddingException
-     * @throws NoSuchAlgorithmException
-     * @throws InvalidKeySpecException
-     * @throws IOException
-     * @throws SignatureException
-     * @throws InvalidKeyException
-     */
-    @Operation(summary = "鐢宠閫�娆�", description = "鐢宠閫�娆�")
-    @ApiResponses(value = {
-            @ApiResponse(
-                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
-                    description = "鎿嶄綔缁撴灉锛歵rue锛氭垚鍔燂紝false锛氬け璐ワ紙BaseResponse.content锛�",
-                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
-                            schema = @Schema(implementation = Boolean.class))}
-            )
-    })
-    @PostMapping(path = "refunds")
-    @Transactional(rollbackFor = Exception.class)
-    @SsoAop()
-    public BaseResponse<Boolean> refunds(@RequestBody @Valid Refund po, BindingResult bindingResult) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, SignatureException, InvalidKeyException {
-        if(bindingResult != null && bindingResult.hasErrors()){
-            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
-        }
-
-        /**
-         * 1. 鍒ゆ柇浜ゆ槗鏃堕棿鏄惁瓒呰繃涓�骞�
-         * 2. 鍒ゆ柇閫�娆炬�婚噾棰濇槸鍚﹁秴杩囪鍗曢噾棰�
-         * 3. 鍒ゆ柇褰撳墠璁㈠崟閫�娆炬鏁版槸鍚﹁秴杩�50娆�
-         * 4. 鍒ゆ柇涓庤璁㈠崟涓婃閫�娆炬槸鍚︾浉闅�1鍒嗛挓
-         */
-
-        String tradeNo = po.getTradeNo();
-        String refundNo = po.getRefundNo();
-        Integer refund = po.getRefund();
-        if(refundNo == null || refundNo.length() <= 0) {
-            // 鏂版彁閫�娆剧敵璇凤紝鐢熸垚閫�娆惧崟鍙�
-            //refundNo = generateRefundNo(tradeNo);
-        }
-
-        // 鏍规嵁璁㈠崟鍙疯幏鍙栨�绘敮浠橀噾棰濆拰鎬婚��娆鹃噾棰�
-        Integer totalTradeAmount = 0;
-        Integer totalRefundAmount = 0;
-        //Integer totalTradeAmount = getTotalTradeAmount(tradeNo);
-        //Integer totalRefundAmount = getTotalRefundAmount(tradeNo);
-        if(totalRefundAmount > totalTradeAmount) {
-            return BaseResponseUtils.buildFail(SellResultCode.TOTAL_REFUND_EXCEED_TRADE.getMessage());
-        }
-
-        // 鐢熸垚body
-        RefundRequest.Amount amount = new RefundRequest.Amount();
-        amount.setRefund(refund);
-        amount.setTotal(totalTradeAmount);
-        amount.setCurrency("CNY");
-
-        RefundRequest refundRequest = new RefundRequest();
-        refundRequest.setOut_trade_no(tradeNo);
-        refundRequest.setOut_refund_no(refundNo);
-        refundRequest.setNotify_url(notifyUrl);
-        refundRequest.setAmount(amount);
-
-        // 鐢熸垚header
-        String nonceStr = payHelper.generateRandomString();
-        Long timestamp = System.currentTimeMillis() / 1000;
-
-        String method = "POST";
-        String httpUrl = "/v3/refund/domestic/refunds";
-
-        String body = JSONObject.toJSONString(refundRequest);
-        String header = schema + " " + payHelper.getToken(method, httpUrl, body, nonceStr, timestamp, privateCertFileName);
-
-        Map<String, String> headers = new HashMap<>();
-        headers.put("Authorization", header);
-        headers.put("Accept", "application/json");
-        headers.put("Content-Type", "application/json");
-
-        JSONObject job_refundResponse = restTemplateUtil.post(PayInfo.orderUrl, body, headers);
-        RefundResponse refundResponse = JSON.parseObject(job_refundResponse.toJSONString(), RefundResponse.class);
-
-        String status = refundResponse.getStatus();
-        if(status != null && status.equals("SUCCESS")) {
-            // 閫�娆剧敵璇峰凡鍙楃悊
-            return BaseResponseUtils.buildSuccess(true) ;
-        } else if(status != null && status.equals("PROCESSING")) {
-            // 閫�娆惧鐞嗕腑
-            return BaseResponseUtils.buildFail(SellResultCode.PROCESSING.getMessage());
-        } else {
-            // 閫�娆惧紓甯�
-            return BaseResponseUtils.buildError(SellResultCode.ABNORMAL.getMessage());
-        }
-
     }
 
     /**
@@ -491,7 +403,68 @@
             }
         } else if(eventType != null && eventType.equals("REFUND.SUCCESS")) {
             // 閫�娆炬垚鍔熷悗鍥炶皟
+            /**
+             * 閫�娆炬垚鍔熺殑鍥炶皟
+             * 鍙栧嚭閫氱煡鏁版嵁瀵硅薄锛岀户鑰屽彇鍑鸿В瀵嗘墍闇�鐨刟ssociatedData鍜宯once锛屼互鍙婂瘑鏂嘽iphertext
+             * 瑙e瘑ciphertext寰楀埌
+             */
+            OrderNotify.NotifyResource notifyResource = orderNotify.getResource();
+            String associatedData = notifyResource.getAssociated_data();
+            String nonce = notifyResource.getNonce();
+            String ciphertext = notifyResource.getCiphertext();
 
+            String resource = AesUtil.decryptToString(PayInfo.key.getBytes("utf-8"), associatedData.getBytes("utf-8"), nonce.getBytes("utf-8"), ciphertext);
+            JSONObject job_resource = JSONObject.parseObject(resource);
+
+            // 瑙e瘑鍚庡彇鍑猴細鍟嗘埛璁㈠崟鍛樸�佸井淇℃敮浠樿鍗曞彿銆佷氦鏄撶姸鎬併�佹敮浠樺畬鎴愭椂闂�
+            String out_trade_no = job_resource.getString("out_trade_no");
+            String transaction_id = job_resource.getString("transaction_id");
+            String out_refund_no = job_resource.getString("out_refund_no");
+            String refund_status = job_resource.getString("refund_status");
+            Date success_time = job_resource.getDate("success_time");
+            if(!refund_status.equals("SUCCESS")) {
+                response.setStatus(500);
+                result.put("code", "FAIL");
+                result.put("message", "澶辫触");
+                return result;
+            }
+
+            // 鏇存柊铏氭嫙鍗¤〃鍙婂厖鍊艰〃鍝嶅簲瀛楁
+            SeVcRefundItem seVcRefundItem = new SeVcRefundItem();
+            seVcRefundItem.setRefundTime(success_time);
+            seVcRefundItem.setRefundStatus(RefundItemStateENUM.REFUNDED.getCode());
+            Integer rec = virtualCardSv.updateRefundItem(seVcRefundItem);
+            if(rec == null && rec <= 0) {
+                response.setStatus(500);
+                result.put("code", "FAIL");
+                result.put("message", "澶辫触");
+                return result;
+            }
+
+            // 鏍规嵁閫�娆惧崟鍙峰弽鏌ラ��娆綢D锛屾牴鎹��娆綢D鑾峰彇閫�娆剧姸鎬佹槸鏈��娆剧殑璁板綍鏁伴噺锛屽鏋滄槸0鍒欒鏄庡叏閮ㄩ��娆惧畬鎴愶紝鏇存柊閫�娆捐〃鐘舵�佷负宸查��娆撅紝灏嗛��娆惧悗閲戦鏇存柊鍒拌櫄鎷熷崱琛�
+            /**
+             * 鏍规嵁閫�娆鹃�氱煡鎺ュ彛杩斿洖鐨勯��娆惧崟鍙峰弽鏌ラ��娆綢D锛屾煡璇㈣閫�娆綢D涓嬫湭閫�娆捐褰曟暟閲�
+             *      濡傛灉缁撴灉涓�0锛屽垯璇ラ��娆惧凡缁忓畬鎴�
+             *      1. 鏇存柊閫�娆捐〃鐘舵�佷负宸查��娆�
+             *      2. 灏嗛��娆惧悗浣欓鏇存柊鍒拌櫄鎷熷崱琛ㄤ腑
+             */
+            Integer noRefundedCount = virtualCardSv.getNoRefundedCount(out_refund_no);
+            if(noRefundedCount != null && noRefundedCount == 0) {
+                // 鑾峰彇閫�娆惧璞″苟淇敼閫�娆剧姸鎬�
+                Long refundId = virtualCardSv.getRefundIdByRefundNumber(out_refund_no);
+                SeVcRefund seVcRefund = virtualCardSv.selectRefundByRefundId(refundId);
+                seVcRefund.setRefundStatus(RefundStatusENUM.REFUNDED.getCode());
+                virtualCardSv.updateRefund(seVcRefund);
+
+                // 鑾峰彇铏氭嫙鍗″璞″苟淇敼浣欓銆佹渶鍚庢搷浣溿�佹渶鍚庢搷浣滄椂闂�
+                Long vcId = seVcRefund.getVcId();
+                Double afterRefund = seVcRefund.getAfterRefund();
+                SeVirtualCard seVirtualCard = virtualCardSv.selectVirtuCardById(vcId);
+                seVirtualCard.setMoney(afterRefund);
+                seVirtualCard.setLastOperate(LastOperateENUM.REFUND.getCode());
+                seVirtualCard.setLastOperateTime(new Date());
+                virtualCardSv.updateVirtualCard(seVirtualCard);
+            }
         }
 
         // 閫氱煡搴旂瓟

--
Gitblit v1.8.0