|
@@ -2,9 +2,13 @@ package thyyxxk.wxservice_server.controller.api;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.tencent.mip.DataHandler;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import thyyxxk.wxservice_server.config.auth.PassToken;
|
|
@@ -12,20 +16,25 @@ import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
|
|
|
import thyyxxk.wxservice_server.dao.api.PowersiPluginDao;
|
|
|
import thyyxxk.wxservice_server.entity.ResultVo;
|
|
|
import thyyxxk.wxservice_server.entity.medinsmobilepay.InsuinfoRequest;
|
|
|
+import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.OrdState;
|
|
|
+import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.request.CommonRequest;
|
|
|
+import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.request.PowersiMipSetlinfo;
|
|
|
+import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.request.RevokeSettleRequest;
|
|
|
import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.response.BillInfo;
|
|
|
import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.response.CommonResponse;
|
|
|
import thyyxxk.wxservice_server.entity.medinsmobilepay.frompowersiplugin.response.Upload6201;
|
|
|
import thyyxxk.wxservice_server.factory.thmz.ThmzService;
|
|
|
import thyyxxk.wxservice_server.factory.thmz.model.QueryReceiptRequest;
|
|
|
+import thyyxxk.wxservice_server.factory.thmz.model.SaveMzPayRequest;
|
|
|
import thyyxxk.wxservice_server.service.IdCardAnalyzeService;
|
|
|
+import thyyxxk.wxservice_server.utils.DateUtil;
|
|
|
import thyyxxk.wxservice_server.utils.ResultVoUtil;
|
|
|
+import thyyxxk.wxservice_server.utils.SnowFlakeId;
|
|
|
import thyyxxk.wxservice_server.utils.TokenUtil;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
@@ -60,17 +69,6 @@ public class PowersiMiPayPlugin {
|
|
|
|
|
|
@PostMapping("/getOpenid")
|
|
|
public ResultVo<String> getOpenid(@RequestBody InsuinfoRequest request) throws Exception {
|
|
|
- String[] hisOrdInfo = request.getHisOrdNum().split("_");
|
|
|
- if (hisOrdInfo.length == 3) {
|
|
|
- request.setPatNo(hisOrdInfo[0]);
|
|
|
- request.setTimes(Integer.parseInt(hisOrdInfo[1]));
|
|
|
- request.setSocialNo(dao.selectIdNo(hisOrdInfo[0]));
|
|
|
- ResultVo<String> insuinfo = new RestTemplate().postForObject
|
|
|
- (MZ_API_URL+"/queryInsuInfo", request, ResultVo.class);
|
|
|
- if (insuinfo.getCode() != ExceptionEnum.SUCCESS.getCode()) {
|
|
|
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, insuinfo.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
return ResultVoUtil.success(TokenUtil.getInstance().getUserOpenid());
|
|
|
}
|
|
|
|
|
@@ -169,6 +167,16 @@ public class PowersiMiPayPlugin {
|
|
|
request.put("times", Integer.parseInt(arr[1]));
|
|
|
|
|
|
Upload6201 upload6201 = new RestTemplate().postForObject(MZ_API_URL + "/getUpload6201", request, Upload6201.class);
|
|
|
+ if (null == upload6201) {
|
|
|
+ JSONObject res = new JSONObject();
|
|
|
+ res.put("code", -1);
|
|
|
+ res.put("success", false);
|
|
|
+ res.put("message", "网络异常,请稍后再试。");
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ upload6201.setIdNo(request.getString("idNo"));
|
|
|
+ upload6201.setIdType(request.getString("idType"));
|
|
|
+ upload6201.setUserName(request.getString("userName"));
|
|
|
|
|
|
String raw = dataHandler.buildReqData(JSONObject.toJSONString(upload6201));
|
|
|
JSONObject response = JSONObject.parseObject(raw);
|
|
@@ -182,9 +190,150 @@ public class PowersiMiPayPlugin {
|
|
|
@PostMapping("/settleNotify")
|
|
|
public CommonResponse settleNotify(@RequestBody JSONObject body) throws Exception {
|
|
|
JSONObject request = decryptRequest(body);
|
|
|
- log.info("结算通知:{}", request);
|
|
|
- // todo 结算成功后的业务处理
|
|
|
- return new CommonResponse();
|
|
|
+ PowersiMipSetlinfo setlinfo = dao.selectById(request.getString("platformOrderId"));
|
|
|
+ if (null != setlinfo) {
|
|
|
+ return new CommonResponse();
|
|
|
+ }
|
|
|
+ setlinfo = JSONObject.parseObject(request.toJSONString(), PowersiMipSetlinfo.class);
|
|
|
+ int insert = dao.insert(setlinfo);
|
|
|
+ if (insert == 1) {
|
|
|
+ SaveMzPayRequest saveMzPayRequest = new SaveMzPayRequest.Builder()
|
|
|
+ .payTime(DateUtil.formatDatetime(setlinfo.getTraceTime()))
|
|
|
+ .patCardNo(setlinfo.getMedOrgOrd().split("_")[0])
|
|
|
+ .agtOrdNum(setlinfo.getThirdOrderId())
|
|
|
+ .payAmt(setlinfo.getFeeSumamt())
|
|
|
+ .fundpayAmt(setlinfo.getFundPay())
|
|
|
+ .acctpayAmt(setlinfo.getPsnAcctPay())
|
|
|
+ .couponAmt(BigDecimal.ZERO)
|
|
|
+ .cashpayAmt(setlinfo.getOwnpayAmt())
|
|
|
+ .hisOrdNum(setlinfo.getMedOrgOrd())
|
|
|
+ .psOrdNum(setlinfo.getPlatformOrderId()).build();
|
|
|
+ thmzService.saveMzPay(saveMzPayRequest);
|
|
|
+ return new CommonResponse();
|
|
|
+ }
|
|
|
+ return new CommonResponse("接收通知失败。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @GetMapping("/isMipOrder")
|
|
|
+ public ResultVo<String> isMipOrder(@RequestParam("hisOrdNum") String hisOrdNum) throws Exception {
|
|
|
+ PowersiMipSetlinfo setlinfo = getSetlinfo(hisOrdNum, OrdState.SETTLED);
|
|
|
+ if (null == setlinfo) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
|
|
|
+ }
|
|
|
+ return ResultVoUtil.success(setlinfo.getPlatformOrderId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @PostMapping("/revokeMipSettle")
|
|
|
+ public ResultVo<String> revokeMipSettle(@RequestBody CommonRequest request) throws Exception {
|
|
|
+ PowersiMipSetlinfo setlinfo = getRefundableSetl(request.getHisOrdNum());
|
|
|
+ if (null == setlinfo) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有可以退款的结算交易。");
|
|
|
+ }
|
|
|
+ Date now = new Date();
|
|
|
+ RevokeSettleRequest refd = new RevokeSettleRequest();
|
|
|
+ refd.setMedOrgOrd(request.getHisOrdNum());
|
|
|
+ refd.setMedRefdId(SnowFlakeId.instance().nextId());
|
|
|
+ refd.setRefdTime(DateUtil.formatDatetime(now));
|
|
|
+ refd.setTotlRefdAmt(setlinfo.getFeeSumamt());
|
|
|
+ refd.setPsnAcctRefdAmt(setlinfo.getPsnAcctPay());
|
|
|
+ refd.setFundRefdAmt(setlinfo.getFundPay());
|
|
|
+ refd.setCashRefdAmt(setlinfo.getOwnpayAmt());
|
|
|
+ refd.setRefdReason(request.getRefundReason());
|
|
|
+ refd.setEcToken(request.getEcToken());
|
|
|
+ refd.setPlatformOrderId(setlinfo.getPlatformOrderId());
|
|
|
+
|
|
|
+ JSONObject raw = JSONObject.parseObject(JSONObject.toJSONString(refd));
|
|
|
+ String body = dataHandler.buildReqData(raw);
|
|
|
+ String url = "http://webhis.thyy.cn:8077/powersiMipRefund";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(body, headers);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String encRes = restTemplate.postForObject(url, entity, String.class);
|
|
|
+ String decRes = dataHandler.processRspData(encRes);
|
|
|
+ JSONObject resObj = JSONObject.parseObject(decRes);
|
|
|
+
|
|
|
+ log.info("医保移动支付退款结果: {}", resObj);
|
|
|
+ Integer code = resObj.getInteger("code");
|
|
|
+ if (null != code && code == 0) {
|
|
|
+ JSONObject data = resObj.getJSONObject("data");
|
|
|
+ OrdState state = OrdState.get(data.getString("refStatus"));
|
|
|
+ if (null == state) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
+ }
|
|
|
+ String refId = data.getString("platformRefdId");
|
|
|
+ dao.updateRevokeInfo(request.getStaffId(), now, refId, setlinfo.getMedOrgOrd(), state);
|
|
|
+ if (state == OrdState.REFUND_SUCCEED) {
|
|
|
+ return ResultVoUtil.success("医保移动支付退款成功");
|
|
|
+ }
|
|
|
+ if (state == OrdState.REFUND_PROCEEDING) {
|
|
|
+ return ResultVoUtil.success("医保移动支付退款进行中,请稍后查询退款结果。");
|
|
|
+ }
|
|
|
+ if (state == OrdState.REFUND_ABNORMAL) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保移动支付退款异常,请稍后查询退款结果。");
|
|
|
+ }
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, data.getString("failMsg"));
|
|
|
+ }
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, resObj.getString("message"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private PowersiMipSetlinfo getRefundableSetl(String id) {
|
|
|
+ PowersiMipSetlinfo setlinfo = getSetlinfo(id, OrdState.SETTLED);
|
|
|
+ if (null == setlinfo) {
|
|
|
+ setlinfo = getSetlinfo(id, OrdState.REFUND_FAILED);
|
|
|
+ if (null == setlinfo) {
|
|
|
+ setlinfo = getSetlinfo(id, OrdState.REFUND_ABNORMAL);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return setlinfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PowersiMipSetlinfo getSetlinfo(String id, OrdState state) {
|
|
|
+ QueryWrapper<PowersiMipSetlinfo> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("med_org_ord", id);
|
|
|
+ wrapper.eq("ord_state", state);
|
|
|
+ return dao.selectOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @GetMapping("/queryRefundState")
|
|
|
+ public ResultVo<String> queryRefundState(@RequestParam("hisOrdNum") String hisOrdNum) throws Exception {
|
|
|
+ QueryWrapper<PowersiMipSetlinfo> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("med_org_ord", hisOrdNum);
|
|
|
+ wrapper.ne("ord_state", OrdState.SETTLED);
|
|
|
+ wrapper.isNotNull("platform_refd_id");
|
|
|
+ PowersiMipSetlinfo setlinfo = dao.selectOne(wrapper);
|
|
|
+ if (null == setlinfo) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
|
|
|
+ }
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("platformRefdId", setlinfo.getPlatformRefdId());
|
|
|
+ params.put("orgCodg", "H43010500370");
|
|
|
+ JSONObject raw = JSONObject.parseObject(JSONObject.toJSONString(params));
|
|
|
+ String body = dataHandler.buildReqData(raw);
|
|
|
+ String url = "http://webhis.thyy.cn:8077/powersiMipRefundQuery";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(body, headers);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String encRes = restTemplate.postForObject(url, entity, String.class);
|
|
|
+ String decRes = dataHandler.processRspData(encRes);
|
|
|
+ JSONObject resObj = JSONObject.parseObject(decRes);
|
|
|
+ Integer code = resObj.getInteger("code");
|
|
|
+ if (null != code && code == 0) {
|
|
|
+ JSONObject data = resObj.getJSONObject("data");
|
|
|
+ data.remove("hiExtData");
|
|
|
+ OrdState state = OrdState.get(data.getString("refdStatus"));
|
|
|
+ if (null == state) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
|
|
|
+ }
|
|
|
+ String refId = data.getString("platformRefdId");
|
|
|
+ dao.updateRefundState(refId, state);
|
|
|
+ return ResultVoUtil.success(state.toString());
|
|
|
+ }
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, resObj.getString("message"));
|
|
|
}
|
|
|
|
|
|
}
|