浏览代码

feature: 医保预结算接口调试

Walker 1 年之前
父节点
当前提交
07d81865e6

+ 1 - 0
onemini-hospital-empty/src/main/java/com/ywt/alipaympapi/core/utils/BizUtil.java

@@ -157,6 +157,7 @@ public final class BizUtil {
             case NFYYBYFY_WXAPP:
             case NFYYBYFY_DOC_WX_OFFICIAL:
             case NFYYBYFY_OFFICIAL:
+            case NFYYBYFY_ALIPAY_MP:
                 return ConstantDef.NFYYBYFY_HOSPITAL_ID;
             case TAI_HE_INTERNET_HOSPITAL_PATIENT_MINI_PROGRAM:
             case TAI_HE_INTERNET_HOSPITAL_PATIENT_WX_OFFICIAL:

+ 5 - 3
onemini-hospital-empty/src/main/java/com/ywt/alipaympapi/models/outpatient/PrePayConfirmReq.java

@@ -16,10 +16,12 @@ public class PrePayConfirmReq {
     private String hisOrdNum;
     private int orderSum;
     private Integer payInsType;// 支付医保类型
-
     private String orderInsType;
-
     private String authCode;
-
     private String appId;
+    private Integer userId;
+    private String alipayUid;
+    private Integer terminal;
+    private Integer hospitalId;
+    private String callUrl;
 }

+ 14 - 5
onemini-hospital-empty/src/main/java/com/ywt/alipaympapi/service/impl/IsvPayServiceImpl.java

@@ -1,9 +1,13 @@
 package com.ywt.alipaympapi.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.alipay.api.AlipayClient;
+import com.alipay.api.DefaultAlipayClient;
 import com.alipay.api.domain.AlipayTradeQueryModel;
 import com.alipay.api.request.AlipayCommerceMedicalAuthinfoAuthQueryRequest;
+import com.alipay.api.request.AlipaySystemOauthTokenRequest;
 import com.alipay.api.response.AlipayCommerceMedicalAuthinfoAuthQueryResponse;
+import com.alipay.api.response.AlipaySystemOauthTokenResponse;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.ywt.alipaympapi.core.utils.*;
@@ -825,14 +829,19 @@ public class IsvPayServiceImpl implements IsvPayService {
      * 获取医保支付授权
      * <a href="https://opendocs.alipay.com/pre-open/02iqci#5.2.2%20%E6%8E%A5%E5%8F%A3%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E">参考文档</a>
      */
-    public GetPayAuthInfo getPayAuthInfo(String appId, String alipayUid, String callUrl) {
+    public GetPayAuthInfo getPayAuthInfo(String appId, String alipayUid, String authCode, String callUrl) {
         GetPayAuthInfo getPayAuthInfo = new GetPayAuthInfo();
         try {
             // 换取 token
-            String rk = String.format(Constants.RK_ACCESS_TOKEN, appId, alipayUid);
-            String accessToken = jedisCommands.get(rk);
-            CheckUtil.ensureNotEmpty(accessToken, rk + " 无法获取 accessToken");
-
+            AlipaySystemOauthTokenRequest req = new AlipaySystemOauthTokenRequest();
+            req.setGrantType("authorization_code");
+            req.setCode(authCode); //auth_code
+            AlipaySystemOauthTokenResponse resp = alipayService.getAlipayClient(appId).execute(req);
+            if (!resp.isSuccess()) {
+                log.error("IsvPayServiceImpl#getPayAuthInfo(appId={} , alipayUid={} , authCode={} , callUrl={} ):无法获取token: {}", appId, alipayUid, authCode, callUrl, JSONObject.toJSONString(resp));
+                throw new AppMessageException(String.format("无法获取 token: %s", resp.getMsg()));
+            }
+            String accessToken = resp.getAccessToken();
             int terminal = BizUtil.getAlipayMpCfgByAppId(appId).getTerminal();
             AlipayMpCfg cfg = BizUtil.getCfg(terminal);
 

+ 1 - 0
onemini-hospital-empty/src/main/java/com/ywt/alipaympapi/web/configs/WebMvcConfigure.java

@@ -15,6 +15,7 @@ public class WebMvcConfigure extends WebMvcConfigurerAdapter {
                 .addPathPatterns("/**")
                 // 排除的接口,无法通过 ContextHelper 获取 userId, terminal & alipayUid,也无法通过 BizUtil 获取 hospitalId,需要自行获取
                 .excludePathPatterns("/auth/*")
+                .excludePathPatterns("/outpatient/*")
                 .excludePathPatterns("/register/*")
                 .excludePathPatterns("/isvRequest.order.doctorScheduleList")
         ;

+ 26 - 9
onemini-hospital-empty/src/main/java/com/ywt/alipaympapi/web/controller/OutpatientController.java

@@ -3,6 +3,7 @@ package com.ywt.alipaympapi.web.controller;
 import com.ywt.alipaympapi.core.utils.*;
 import com.ywt.alipaympapi.models.BaseResponse;
 import com.ywt.alipaympapi.models.BaseResponse2;
+import com.ywt.alipaympapi.models.auth.AlipayMpCfg;
 import com.ywt.alipaympapi.models.enums.MedInsTypeEnum;
 import com.ywt.alipaympapi.models.enums.MpTypeEnum;
 import com.ywt.alipaympapi.models.enums.OutpatientOrderPayInsTypeEnum;
@@ -51,14 +52,31 @@ public class OutpatientController {
             String endDate = Checker.getStringValue(reqData.getEndDate());
             String clinicCode = Checker.getStringValue(reqData.getClinicCode());
             String hisOrdNum = Checker.getStringValue(reqData.getHisOrdNum());
+            String callUrl = Checker.getStringValue(reqData.getCallUrl());
             int orderSum = Checker.getIntegerValue(reqData.getOrderSum());
             int payInsType = Checker.getIntegerValue(reqData.getPayInsType());
-            int hospitalId = BizUtil.getHospitalIdByTerminalId(ContextHelper.getCurrentTerminalWrapped());
-            String appId = Checker.getStringValue(reqData.getAppId());
+            int terminal = ContextHelper.getCurrentTerminalWrapped();
+            if (terminal <= 0) {
+                terminal = Checker.getIntegerValue(reqData.getTerminal());
+            }
+            int hospitalId = BizUtil.getHospitalIdByTerminalId(terminal);
+            int userId = ContextHelper.getCurrentUserIdWrapped();
+            if (userId <= 0) {
+                userId = Checker.getIntegerValue(reqData.getUserId());
+            }
+            String alipayUid = ContextHelper.getAlipayUidWrapped();
+            if (StringHelper.isNullOrEmpty(alipayUid)) {
+                alipayUid = Checker.getStringValue(reqData.getAlipayUid());
+            }
+            CheckUtil.ensureNotEmpty(alipayUid, "无法获取支付宝用户信息");
             CheckUtil.ensureNotEmpty(patientId, "HIS 患者 id 不能为空");
             CheckUtil.ensureNotEmpty(clinicCode, "就诊流水号不能为空");
             CheckUtil.ensureNotEmpty(hisOrdNum, "HIS 订单号不能为空");
             CheckUtil.ensureLargerThanZero(orderSum, "订单金额需大于 0");
+            CheckUtil.ensureLargerThanZero(userId, "无法获取用户信息");
+            CheckUtil.ensureLargerThanZero(terminal, "无法获取终端信息");
+            AlipayMpCfg cfg = BizUtil.getCfg(terminal);
+            String appId = cfg.getAppId();
             CheckUtil.ensureNotEmpty(appId, "缺少 appId 参数");
             PrePayConfirmRequest req = PrePayConfirmRequest.newBuilder()
                     .setCardNo(cardNo)
@@ -74,7 +92,7 @@ public class OutpatientController {
             if (response.getCode() == ResultCode.SUCCEED_VALUE) {
                 if (!Checker.isNone(response.getListList())) {
                     // 预结算成功,自动取消之前生成的未支付订单,让前端可以重新创建订单
-                    autoCancelPrevOrder(hisOrdNum);
+                    autoCancelPrevOrder(hisOrdNum, terminal);
                     PrePayConfirm p = response.getListList().get(0);
                     PrePayConfirmResp resp = new PrePayConfirmResp();
                     String invoiceNo = p.getInvoiceNo();
@@ -95,7 +113,7 @@ public class OutpatientController {
                         GetMedicalCardInfoByPatientIdRequest gResp = GetMedicalCardInfoByPatientIdRequest.newBuilder()
                                 .setHisPatientId(patientId)
                                 .setHospitalId(hospitalId)
-                                .setUserId(ContextHelper.getCurrentUserIdWrapped())
+                                .setUserId(userId)
                                 .build();
                         GetMedicalCardInfoByPatientIdResponse gmcResp = taiheOutpatientServiceBlockingStub.getMedicalCardInfoByPatientId(gResp);
                         if (gmcResp.getCode() != ResultCode.SUCCEED_VALUE) {
@@ -110,7 +128,7 @@ public class OutpatientController {
                             String authCode = reqData.getAuthCode();
                             CheckUtil.ensureNotEmpty(authCode, "授权码不能为空");
                             // 换取支付授权码
-                            GetPayAuthInfo payAuthInfo = isvPayService.getPayAuthInfo(appId, ContextHelper.getAlipayUidWrapped(), "");
+                            GetPayAuthInfo payAuthInfo = isvPayService.getPayAuthInfo(appId, alipayUid, authCode, callUrl);
                             String authUrl = payAuthInfo.getAuthUrl(); // 当用户需要授权时,返回该参数,医药机构前端直接跳转该地址。
                             String payAuthNo = payAuthInfo.getPayAuthNo(); // 当用户已经授权支付时,直接返回该字段。线上支付授权编码。
                             String authNo = payAuthInfo.getAuthNo(); // 线上身份验证授权码,只能用户身份核验,不能用于支付业务。
@@ -188,8 +206,7 @@ public class OutpatientController {
                 return baseResponse.failed(BaseResponse2.APP_ERROR, "预结算:HIS 返回列表数据为空");
             }
             return baseResponse.failed(BaseResponse.APP_ERROR, response.getMsg());
-        } catch (
-                AppMessageException ame) {
+        } catch (AppMessageException ame) {
             return baseResponse.failed(BaseResponse.PARAMETER_ERROR, ame.getMessage());
         } catch (
                 Exception e) {
@@ -198,10 +215,10 @@ public class OutpatientController {
         }
     }
 
-    private void autoCancelPrevOrder(String hisOrderNo) {
+    private void autoCancelPrevOrder(String hisOrderNo, int terminal) {
         GetOutpatientOrderByHisOrderNoRequest request = GetOutpatientOrderByHisOrderNoRequest.newBuilder()
                 .setHisOrderNo(hisOrderNo)
-                .setHospitalId(BizUtil.getHospitalIdByTerminalId(ContextHelper.getCurrentTerminalWrapped()))
+                .setHospitalId(BizUtil.getHospitalIdByTerminalId(terminal))
                 .build();
         GetOutpatientOrderByHisOrderNoResponse response = taiheOutpatientServiceBlockingStub.getOutpatientOrderByHisOrderNo(request);
         if (response.getCode() == ResultCode.SUCCEED_VALUE) {