From ab05bf79ce4384dc5c886eb449c127b51b2cf85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=AA?= <1044122544@qq.com> Date: Mon, 9 Jun 2025 16:53:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=88=B7=E8=84=B8=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/controller/WxFacePayController.java | 94 +++++++++++++------ .../wx/service/WeChatPayFaceService.java | 1 + .../scanface/wx/vo/WxFacePayAuthinfoResp.java | 1 + 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/dpkj/modules/scanface/wx/controller/WxFacePayController.java b/src/main/java/com/dpkj/modules/scanface/wx/controller/WxFacePayController.java index ee0f1b0..63d4c1c 100644 --- a/src/main/java/com/dpkj/modules/scanface/wx/controller/WxFacePayController.java +++ b/src/main/java/com/dpkj/modules/scanface/wx/controller/WxFacePayController.java @@ -102,7 +102,7 @@ public class WxFacePayController { //8、更新支付结果updateWxpayfacePayResult WxFacePayResp updFacePayResp = this.updateWxpayfacePayResult(wxFacePayResp0); - if(ObjUtil.isNotEmpty(updFacePayResp) && WxConstant.STATE_SUCCESS.equals(updFacePayResp.getReturn_code())){ + if (ObjUtil.isNotEmpty(updFacePayResp) && WxConstant.STATE_SUCCESS.equals(updFacePayResp.getReturn_code())) { //向后端发起更新订单,进行相应的操作、 this.updatePayResult(micropayResult); } @@ -129,7 +129,7 @@ public class WxFacePayController { // 构建请求参数的JSON字符串 WxFacePayReq wxFacePayReq = new WxFacePayReq("initWxpayface", "1", System.currentTimeMillis() / 1000, 1); WxFacePayResp wxFacePayResp = weChatPayFaceService.doWxPayIniMethod(wxFacePayReq); - System.out.println("**************1、程序启动时初始化:" + wxFacePayResp.toString()); + log.info("[WxFacePayController][initWxpayface][132] [1、程序启动时初始化:]:{}", wxFacePayResp.toString()); return wxFacePayResp; } @@ -143,7 +143,7 @@ public class WxFacePayController { // 构建请求参数的JSON字符串 WxFacePayReq wxFacePayReq = new WxFacePayReq("getWxpayfaceRawdata", "1", System.currentTimeMillis() / 1000); WxFacePayResp wxFacePayResp = weChatPayFaceService.doWxPayIniMethod(wxFacePayReq); - System.out.println("**************2、获取数据:" + wxFacePayResp.toString()); + log.info("[WxFacePayController][getWxpayfaceRawdata][146][2、获取数据getWxpayfaceRawdata:] :{}", wxFacePayResp.toString()); return wxFacePayResp; } @@ -156,8 +156,7 @@ public class WxFacePayController { public WxFacePayAuthinfoResp getWxpayfaceAuthinfo(String rawdata) throws Exception { // 构建请求参数的JSON字符串 WxFacePayAuthinfoResp wxFacePayResp = weChatPayFaceService.getWxFaceAuthInfoReqMap(rawdata); - System.out.println("**************3、获取调用凭证get_wxpayface_authinfo:" + wxFacePayResp.toString()); - + log.info("[WxFacePayController][getWxpayfaceAuthinfo][159][3、获取调用凭证get_wxpayface_authinfo:] :{}", wxFacePayResp.toString()); return wxFacePayResp; } @@ -185,7 +184,8 @@ public class WxFacePayController { .setFace_code_type("1"); WxFacePayAuthinfoResp authinfoResp = weChatPayFaceService.getWxpayfaceCode(wxFacePayReq); - System.out.println("**************4、进行人脸识别结果:" + authinfoResp.toString()); + log.info("[WxFacePayController][getWxpayfaceCode][189][4、进行人脸识别结果:] :{}", authinfoResp.toString()); + return authinfoResp; } @@ -203,7 +203,7 @@ public class WxFacePayController { .body(); JSONObject tradeNoResult = JSONObject.parseObject(req0); String outTradeNo = tradeNoResult.getString("result"); - System.out.println("获取流水号" + outTradeNo); + log.info("[WxFacePayController][getOutTradeNo][208][获取流水号] :{}", outTradeNo); return outTradeNo; } @@ -232,14 +232,14 @@ public class WxFacePayController { .body(serverParams.toString()) .execute() .body(); - System.out.println("5、向后台发起进行发起订单支付 结果:" + req); + log.info("[WxFacePayController][toCreateWxOrder][237][5、向后台发起进行发起订单支付 结果] :{}", req); JSONObject serverResult = JSONObject.parseObject(req); - Map result = (Map) serverResult.get("result"); + Map result = (Map) serverResult.get("result"); log.info("[WxFacePayController][toCreateWxOrder][140][5、向后端进行发起订单支付请求结果] :{}", result.toString()); WxPayMicropayResult micropayResult = new WxPayMicropayResult(); - System.out.println("5、调用后台人脸支付API发起支付结果:"+result.toString()); - if(ObjUtil.isNotEmpty(result)){ + log.info("[WxFacePayController][toCreateWxOrder][243][5、调用后台人脸支付API发起支付结果:] :{}", result.toString()); + if (ObjUtil.isNotEmpty(result)) { micropayResult.setOutTradeNo(result.get("outTradeNo").toString()); micropayResult.setTransactionId(result.get("transactionId").toString()); micropayResult.setTotalFee(Integer.valueOf(result.get("totalFee").toString())); @@ -253,13 +253,13 @@ public class WxFacePayController { /** * 轮询逻辑封装方法 * 查询微信刷脸订单状态 + * 30秒 * 调用步骤6 - * * @param micropayResult * @return */ public boolean pollOrderStatus(WxPayMicropayResult micropayResult) { - final long timeoutMillis = 60_000; // 60秒超时(毫秒) + final long timeoutMillis = 30_000; // 30秒超时(毫秒) final long pollInterval = 2_000; // 轮询间隔2秒 long startTime = System.currentTimeMillis(); @@ -267,17 +267,18 @@ public class WxFacePayController { while (true) { // 1. 查询订单状态 WxPayOrderQueryResult queryResult = this.queryOrderByNo(micropayResult); + log.info("[WxFacePayController][pollOrderStatus][272][1. 查询订单状态:] :{}", queryResult); // 2. 检查成功状态 if (WxConstant.STATE_SUCCESS.equals(queryResult.getReturnCode())) { - System.out.println("订单支付成功!"); + log.info("[WxFacePayController][pollOrderStatus][275][订单支付成功!]"); return true; } // 3. 检查超时 long elapsed = System.currentTimeMillis() - startTime; if (elapsed >= timeoutMillis) { - System.out.println("支付超时,未获取成功状态"); + log.info("[WxFacePayController][pollOrderStatus][284][支付超时,未获取成功状态]"); return false; } @@ -287,7 +288,7 @@ public class WxFacePayController { TimeUnit.MILLISECONDS.sleep(nextPoll); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - System.out.println("轮询被中断"); + log.info("[WxFacePayController][pollOrderStatus][293] [轮询被中断]"); return false; } } @@ -312,14 +313,13 @@ public class WxFacePayController { .body(serverParams.toString()) .execute() .body(); - System.out.println("6、后台发起查询订单 结果:" + req); + log.info("[WxFacePayController][queryOrderByNo][318][6、后台发起查询订单 结果:] :{}", req); JSONObject serverResult = JSONObject.parseObject(req); - Map result = (Map) serverResult.get("result"); + Map result = (Map) serverResult.get("result"); log.info("[WxFacePayController][toCreateWxOrder][140][6、后台发起查询订单 请求结果] :{}", result.toString()); - System.out.println("5、调用后台人脸支付API发起支付结果:"+result.toString()); - if(ObjUtil.isNotEmpty(result)){ + if (ObjUtil.isNotEmpty(result)) { queryResult.setReturnCode(result.get("returnCode").toString()); queryResult.setOutTradeNo(result.get("outTradeNo").toString()); queryResult.setTransactionId(result.get("transactionId").toString()); @@ -337,6 +337,7 @@ public class WxFacePayController { * 支付交易返回失败或支付系统超时,调用该接口撤销交易。 * 如果此订单用户支付失败,微信支付系统会将此订单关闭; * 如果用户支付成功,微信支付系统会将此订单资金退还给用户。 + * * @param reverseRequest * @return */ @@ -353,7 +354,7 @@ public class WxFacePayController { .body(serverParams.toString()) .execute() .body(); - System.out.println("向后台发起撤销交易 结果:" + req); + log.info("[WxFacePayController][toReverseOrder][359][向后台发起撤销交易 结果:] :{}", req); JSONObject serverResult = JSONObject.parseObject(req); reverseResult = (WxPayOrderReverseResult) serverResult.get("result"); log.info("[WxFacePayController][toReverseOrder][314][7、向后台发起撤销交易请求结果] :{}", reverseResult.toString()); @@ -362,8 +363,6 @@ public class WxFacePayController { } - - /** * 8、更新支付结果updateWxpayfacePayResult * @@ -381,7 +380,47 @@ public class WxFacePayController { .setAuthinfo(wxFacePayResp0.getAuthinfo()); WxFacePayResp wxFacePayResp = weChatPayFaceService.doWxPayIniMethod(wxFacePayReq); - System.out.println("**************8、更新支付结果updateWxpayfacePayResult:" + wxFacePayResp.toString()); + log.info("[WxFacePayController][updateWxpayfacePayResult][385][8、更新支付结果updateWxpayfacePayResult] :{}", wxFacePayResp.toString()); + return wxFacePayResp; + } + + + /** + * 9、停止刷脸支付stopWxpayface + * 仅在人脸凭证/付款码face_code【第4步骤】未返回前可用,face_code返回后不可停止 + * + * @return + * @throws JsonProcessingException + */ + @RequestMapping(value = "/stopWxpayface", method = RequestMethod.POST) + public WxFacePayResp stopWxpayface(WxFacePayAuthinfoResp wxFacePayResp0) throws JsonProcessingException, UnsupportedEncodingException, WxpayFaceSDKDll.DllRegistrationException { + // 构建请求参数的JSON字符串 + WxFacePayReq wxFacePayReq = new WxFacePayReq("stopWxpayface", "1", System.currentTimeMillis() / 1000); + wxFacePayReq.setAppid(wxMpProperties.getMchConfig().getAppId()) + .setMch_id(wxMpProperties.getMchConfig().getMchId()) + .setStore_id(WxConstant.STORE_ID) + .setAuthinfo(wxFacePayResp0.getAuthinfo()); + + WxFacePayResp wxFacePayResp = weChatPayFaceService.doWxPayIniMethod(wxFacePayReq); + log.info("[WxFacePayController][stopWxpayface][407][9、停止刷脸支付stopWxpayface的结果:] :{}", wxFacePayResp.toString()); + return wxFacePayResp; + } + + + /** + * 10、释放资源releaseWxpayface + * 首次刷脸需要initWxpayface,刷脸结束后不需要调用releaseWxpayface,否则下次刷脸需要重新initWxpayface,影响启动耗时; + * + * @return + * @throws JsonProcessingException + */ + @RequestMapping(value = "/releaseWxpayface", method = RequestMethod.POST) + public WxFacePayResp releaseWxpayface(WxFacePayAuthinfoResp wxFacePayResp0) throws JsonProcessingException, UnsupportedEncodingException, WxpayFaceSDKDll.DllRegistrationException { + // 构建请求参数的JSON字符串 + WxFacePayReq wxFacePayReq = new WxFacePayReq("releaseWxpayface", "1", System.currentTimeMillis() / 1000); + + WxFacePayResp wxFacePayResp = weChatPayFaceService.doWxPayIniMethod(wxFacePayReq); + log.info("[WxFacePayController][releaseWxpayface][425][10、释放资源releaseWxpayface的结果:] :{}", wxFacePayResp.toString()); return wxFacePayResp; } @@ -403,20 +442,17 @@ public class WxFacePayController { .body(serverParams.toString()) .execute() .body(); - System.out.println("11、向后端发起更新状态为成功 结果:" + req); - + log.info("[WxFacePayController][updatePayResult][447][11、向后端发起更新状态为成功 结果:] :{}", req.toString()); JSONObject jsonObject = JSONObject.parseObject(req); - Map result = (Map) jsonObject.get("result"); + Map result = (Map) jsonObject.get("result"); log.info("[WxFacePayController][updatePayResult][140][11、向后端发起更新状态为成功 请求结果] :{}", result.toString()); } return queryResult; } - /** * 4、进行人脸识别getWxpayfaceCode(获取支付凭证) - * * @return * @throws Exception */ diff --git a/src/main/java/com/dpkj/modules/scanface/wx/service/WeChatPayFaceService.java b/src/main/java/com/dpkj/modules/scanface/wx/service/WeChatPayFaceService.java index 69fa423..88e3cd3 100644 --- a/src/main/java/com/dpkj/modules/scanface/wx/service/WeChatPayFaceService.java +++ b/src/main/java/com/dpkj/modules/scanface/wx/service/WeChatPayFaceService.java @@ -20,6 +20,7 @@ public interface WeChatPayFaceService { * 调用方法 * 1、程序启动时初始化initWxpayface;2、获取数据getWxpayfaceRawdata; * 8、更新支付结果updateWxpayfacePayResult + * 9、停止刷脸支付stopWxpayface * @param wxFacePayReq * @return * @throws UnsupportedEncodingException diff --git a/src/main/java/com/dpkj/modules/scanface/wx/vo/WxFacePayAuthinfoResp.java b/src/main/java/com/dpkj/modules/scanface/wx/vo/WxFacePayAuthinfoResp.java index 25af81b..12ae59c 100644 --- a/src/main/java/com/dpkj/modules/scanface/wx/vo/WxFacePayAuthinfoResp.java +++ b/src/main/java/com/dpkj/modules/scanface/wx/vo/WxFacePayAuthinfoResp.java @@ -48,6 +48,7 @@ public class WxFacePayAuthinfoResp { * 3、获取调用凭证 * 必填:否 * authinfo的有效时间, 单位秒。 + * 在有效时间内, 对于同一台终端设备,相同的参数的前提下(如:相同的公众号、商户号、 门店编号等),可以用同一个authinfo,多次调用SDK的getWxpayfaceCode接口。 */ private int expires_in;