From b4cc876519d91ff2ba16c9c7b960311be3c6626d Mon Sep 17 00:00:00 2001 From: zhaoyiran Date: Tue, 25 Jun 2024 13:40:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A4=E8=A1=8C=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/win/bank/domain/BankDO.java | 6 ++ .../com/win/bank/enums/BocomPayStatEnum.java | 32 +++++++++++ .../bank/service/bocom/BocomServiceImpl.java | 56 +++++++++++++++---- 3 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/win/bank/enums/BocomPayStatEnum.java diff --git a/src/main/java/com/win/bank/domain/BankDO.java b/src/main/java/com/win/bank/domain/BankDO.java index 9d313ce..83ee214 100644 --- a/src/main/java/com/win/bank/domain/BankDO.java +++ b/src/main/java/com/win/bank/domain/BankDO.java @@ -1,6 +1,7 @@ package com.win.bank.domain; import java.math.BigDecimal; +import java.util.Date; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -81,4 +82,9 @@ public class BankDO extends BaseDO { * 流水号 */ private String serialNumber; + + /** + * 最后查询时间 + */ + private Date lastQueryTime; } diff --git a/src/main/java/com/win/bank/enums/BocomPayStatEnum.java b/src/main/java/com/win/bank/enums/BocomPayStatEnum.java new file mode 100644 index 0000000..31c1298 --- /dev/null +++ b/src/main/java/com/win/bank/enums/BocomPayStatEnum.java @@ -0,0 +1,32 @@ +package com.win.bank.enums; + +public enum BocomPayStatEnum { + + INBANKPROCESSING("0", "银行处理中"), SUCCESS("1", "成功"), FAILED("2", "失败"), DOUBTFUL("3", "可疑"), RECORDNOTEXIST("4", "记录不存在"), TOBEAUTHORIZED("5", "待授权"), AUTHORIZATIONDENIAL("6", "授权拒绝"); + + private final String code; + private final String description; + + BocomPayStatEnum(String code, String description) { + this.code = code; + this.description = description; + } + + public String getCode() { + return code; + } + + public String getDescription() { + return description; + } + + public static BocomPayStatEnum getByCode(String code) { + for (BocomPayStatEnum status : BocomPayStatEnum.values()) { + if (status.getCode().equals(code)) { + return status; + } + } + throw new IllegalArgumentException("Invalid BocomPayStatEnum code: " + code); + } + +} diff --git a/src/main/java/com/win/bank/service/bocom/BocomServiceImpl.java b/src/main/java/com/win/bank/service/bocom/BocomServiceImpl.java index 5b98e43..c9fb29a 100644 --- a/src/main/java/com/win/bank/service/bocom/BocomServiceImpl.java +++ b/src/main/java/com/win/bank/service/bocom/BocomServiceImpl.java @@ -18,6 +18,7 @@ import org.w3c.dom.Element; import org.xml.sax.InputSource; import com.win.bank.domain.BankDO; +import com.win.bank.enums.BocomPayStatEnum; import com.win.bank.enums.PaymentStatusEnum; import com.win.bank.service.BaseBankService; import com.win.bank.service.bank.BankService; @@ -50,7 +51,7 @@ public class BocomServiceImpl implements BaseBankService { @Override public BankDO payment(BankDO bankDO) { - String trCode = "210201"; + String trCode = "210205"; String bussinessCode = bankService.getBussinessCode(bankDO); bankDO.setBusinessCode(bussinessCode); String head = generateHeadMessage(trCode, bussinessCode); @@ -62,9 +63,6 @@ public class BocomServiceImpl implements BaseBankService { body.append("").append(bankDO.getRcvBankName()).append(""); body.append("").append(bankDO.getRcvAccount().replaceAll("\\s+", "")).append(""); body.append("").append(bankDO.getRcvUserName()).append(""); - if (StringUtils.isNotEmpty(bankDO.getRcvBankNumber())) { - body.append("").append(bankDO.getRcvBankNumber()).append(""); - } body.append("CNY"); body.append("").append(bankDO.getAmount()).append(""); body.append("").append(bussinessCode).append(""); @@ -91,15 +89,15 @@ public class BocomServiceImpl implements BaseBankService { Element headElement = (Element)document.getDocumentElement().getElementsByTagName("head").item(0); String particularCode = headElement.getElementsByTagName("particular_code").item(0).getTextContent(); if (SUCCESS_CODE.equals(particularCode)) { - bankDO.setStatus(PaymentStatusEnum.SUCCESS.getCode()); + bankDO.setStatus(PaymentStatusEnum.PAYING.getCode()); String serialNumber = headElement.getElementsByTagName("serial_no").item(0).getTextContent(); bankDO.setSerialNumber(serialNumber); + message = PaymentStatusEnum.PAYING.getDescription(); } else { bankDO.setStatus(PaymentStatusEnum.FAILED.getCode()); + message = headElement.getElementsByTagName("particular_info").item(0).getTextContent(); } - message = headElement.getElementsByTagName("particular_info").item(0).getTextContent(); bankDO.setMessage(message); - } catch (Exception e) { e.printStackTrace(); } @@ -109,6 +107,9 @@ public class BocomServiceImpl implements BaseBankService { @Override public BankDO queryPaymentResult(BankDO bankDO) { + if (!timeDifference(bankDO)) { + return null; + } String trCode = "310204"; String bussinessCode = bankService.getBussinessCode(bankDO); String head = generateHeadMessage(trCode, bussinessCode); @@ -130,19 +131,41 @@ public class BocomServiceImpl implements BaseBankService { Element headElement = (Element)document.getDocumentElement().getElementsByTagName("head").item(0); Element bodyElement = (Element)document.getDocumentElement().getElementsByTagName("body").item(0); String particularCode = headElement.getElementsByTagName("particular_code").item(0).getTextContent(); + if (SUCCESS_CODE.equals(particularCode)) { - bankDO.setStatus(PaymentStatusEnum.SUCCESS.getCode()); + String description = ""; + String stat = bodyElement.getElementsByTagName("stat").item(0).getTextContent(); + String errMsg = bodyElement.getElementsByTagName("err_msg").item(0).getTextContent(); + BocomPayStatEnum statEnum = BocomPayStatEnum.getByCode(stat); String serialNumber = bodyElement.getElementsByTagName("ogl_serial_no").item(0).getTextContent(); + switch (statEnum) { + case SUCCESS: + bankDO.setStatus(PaymentStatusEnum.SUCCESS.getCode()); + break; + case INBANKPROCESSING: + case TOBEAUTHORIZED: + bankDO.setStatus(PaymentStatusEnum.PAYING.getCode()); + description = statEnum.getDescription(); + break; + default: + bankDO.setStatus(PaymentStatusEnum.FAILED.getCode()); + description = statEnum.getDescription(); + break; + } bankDO.setSerialNumber(serialNumber); + if (StringUtils.isNotEmpty(errMsg)) { + description = description + errMsg; + } + bankDO.setMessage(description); } else { bankDO.setStatus(PaymentStatusEnum.FAILED.getCode()); + message = headElement.getElementsByTagName("particular_info").item(0).getTextContent(); + bankDO.setMessage(message); } - message = headElement.getElementsByTagName("particular_info").item(0).getTextContent(); - bankDO.setMessage(message); + bankDO.setLastQueryTime(new Date()); } catch (Exception e) { e.printStackTrace(); } - return bankDO; } @@ -156,4 +179,15 @@ public class BocomServiceImpl implements BaseBankService { .append("10"); return head.toString(); } + + public boolean timeDifference(BankDO bankDO) { + if (bankDO.getLastQueryTime() == null) { + return true; + } + Date currentTime = new Date(); + long timeDifference = currentTime.getTime() - bankDO.getLastQueryTime().getTime(); + long minutesDifference = timeDifference / (60 * 1000); + + return minutesDifference > 5; + } }