From d06cb3afa622a0baeafca6997bf0215424cc1ea1 Mon Sep 17 00:00:00 2001 From: zhangtao <1316069495@qq.com> Date: Tue, 17 Nov 2020 11:25:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=BE=B3=E6=B4=B2=E5=88=86?= =?UTF-8?q?=E8=A1=8C=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../merchants/core/bank/AustraliaBank.java | 73 +++++++++++++++++++ .../AustraliaBankClientNullException.java | 14 ++++ .../core/bank/AustraliaBankInfo.java | 50 +++++++++++++ .../core/impls/ClientManagerImpl.java | 45 +++++++----- .../web/PartnerManageController.java | 10 ++- 5 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBank.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankClientNullException.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankInfo.java diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBank.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBank.java new file mode 100644 index 000000000..5f823c9e2 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBank.java @@ -0,0 +1,73 @@ +package au.com.royalpay.payment.manage.merchants.core.bank; + +import cn.yixblog.platform.http.HttpRequestGenerator; +import cn.yixblog.platform.http.HttpRequestResult; +import com.alibaba.fastjson.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.io.IOException; + + +/** + * @Description + * @title: 澳大利亚银行 + * @Date 2020/11/12 14:24 + * @author: zhangTao + */ +public class AustraliaBank { + + private static Logger logger = LoggerFactory.getLogger(AustraliaBank.class); + private static final String AUSTRALIA_BSB_NUMBERS_API = "https://bankcodesapi.com/au-bsb/json"; + private static final String API_KEY = "ab9379cfdab559509bbdcdd11923489f"; + + + public static AustraliaBankInfo getBankInfo(String bsbNo) { + logger.info("<------------------>Start submitting branch bsb Numbers<--------------------->"); + + try { + HttpRequestResult result = + new HttpRequestGenerator(new URL(AUSTRALIA_BSB_NUMBERS_API, API_KEY, bsbNo).toString(), RequestMethod.GET) + .execute(); + + if (result.isSuccess()) { + JSONObject res = result.getResponseContentJSONObj(); + + logger.info("Success submitted branch bsb Numbers message ------> {}", res); + + return AustraliaBankInfo.instance(res); + } + } catch (IOException e) { + logger.error("fail to submitting branch bsb Numbers, error ---> {}", e.getMessage()); + } + + return AustraliaBankInfo.noneInstance(); + } + + + private static class URL { + private static String URL_TEMPLATE = "{url}/{apiKey}/{bsbNo}/"; + + private String url; + private String apiKey; + private String bsbNo; + + public URL(String url, String apiKey, String bsbNo) { + this.url = url; + this.apiKey = apiKey; + this.bsbNo = bsbNo; + } + + public String toString() { + return URL_TEMPLATE.replace("{url}", this.url) + .replace("{apiKey}", apiKey) + .replace("{bsbNo}", bsbNo); + } + + + } + + + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankClientNullException.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankClientNullException.java new file mode 100644 index 000000000..f81271bea --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankClientNullException.java @@ -0,0 +1,14 @@ +package au.com.royalpay.payment.manage.merchants.core.bank; + +/** + * @Description + * @title: + * @Date 2020/11/12 16:07 + * @author: zhangTao + */ +public class AustraliaBankClientNullException extends RuntimeException{ + + public AustraliaBankClientNullException() { + + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankInfo.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankInfo.java new file mode 100644 index 000000000..2574899e0 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/bank/AustraliaBankInfo.java @@ -0,0 +1,50 @@ +package au.com.royalpay.payment.manage.merchants.core.bank; + +import com.alibaba.fastjson.JSONObject; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +import java.awt.*; + +/** + * @Description + * @title: + * @Date 2020/11/12 15:56 + * @author: zhangTao + */ +@Getter +public class AustraliaBankInfo { + private String bsb; + private String valid; + private String bank; + private String address; + private String system; + private String city; + private String postcode; + private String state; + private String branch; + + public boolean isNone() { + return StringUtils.isBlank(this.bsb); + } + + static AustraliaBankInfo instance(JSONObject australiaBankInfo){ + AustraliaBankInfo bankInfo = new AustraliaBankInfo(); + bankInfo.bsb = australiaBankInfo.getString("bsb"); + bankInfo.valid = australiaBankInfo.getString("valid"); + bankInfo.bank = australiaBankInfo.getString("bank"); + bankInfo.address = australiaBankInfo.getString("address"); + bankInfo.system = australiaBankInfo.getString("system"); + bankInfo.city = australiaBankInfo.getString("city"); + bankInfo.postcode = australiaBankInfo.getString("postcode"); + bankInfo.state = australiaBankInfo.getString("state"); + bankInfo.branch = australiaBankInfo.getString("branch"); + return bankInfo; + } + + static AustraliaBankInfo noneInstance() { + return new AustraliaBankInfo(); + } + + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java index d247aee02..9c7d68214 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java @@ -47,6 +47,9 @@ import au.com.royalpay.payment.manage.merchants.core.ClientConfigService; import au.com.royalpay.payment.manage.merchants.core.ClientInfoCacheSupport; import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.merchants.core.ClientModifySupport; +import au.com.royalpay.payment.manage.merchants.core.bank.AustraliaBank; +import au.com.royalpay.payment.manage.merchants.core.bank.AustraliaBankClientNullException; +import au.com.royalpay.payment.manage.merchants.core.bank.AustraliaBankInfo; import au.com.royalpay.payment.manage.merchants.entity.impls.*; import au.com.royalpay.payment.manage.merchants.enums.UPayAuthFileEnum; import au.com.royalpay.payment.manage.notice.core.MailService; @@ -98,7 +101,6 @@ import com.alibaba.fastjson.JSONObject; import com.github.miemiedev.mybatis.paginator.domain.Order; import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import com.github.miemiedev.mybatis.paginator.domain.PageList; -import io.jsonwebtoken.lang.Assert; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; @@ -2746,24 +2748,33 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid @Override public JSONObject getBankInfo(JSONObject manager, String clientMoniker, String bsbNo) { - JSONObject client = getClientInfoByMoniker(clientMoniker); - if (client == null) { - throw new InvalidShortIdException(); + Optional clientOptional = Optional.ofNullable(getClientInfoByMoniker(clientMoniker)); + + if (!clientOptional.isPresent()) { + throw new AustraliaBankClientNullException(); } - checkOrgPermission(manager, client); - // bd451cc3e3ad66c75165dc852507e8f0 - // 收费 ab9379cfdab559509bbdcdd11923489f - String url = "https://api.bank.codes/au-bsb/json/ab9379cfdab559509bbdcdd11923489f/" + bsbNo; - JSONObject res = new JSONObject(); - try { - HttpRequestResult result = new HttpRequestGenerator(url, RequestMethod.GET).execute(); - if (result.isSuccess()) { - res = result.getResponseContentJSONObj(); - } - } catch (IOException e) { - logger.error(e.getMessage(), e); + + checkOrgPermission(manager, clientOptional.get()); + + AustraliaBankInfo bankInfo = AustraliaBank.getBankInfo(bsbNo); + + if (bankInfo.isNone()) { + return new JSONObject(); } - return res; + + return new JSONObject() { + { + put("bsb", bankInfo.getBsb()); + put("valid", bankInfo.getValid()); + put("bank", bankInfo.getBank()); + put("address", bankInfo.getAddress()); + put("system", bankInfo.getSystem()); + put("city", bankInfo.getCity()); + put("postcode", bankInfo.getPostcode()); + put("state", bankInfo.getState()); + put("branch", bankInfo.getBranch()); + } + }; } @Override diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/web/PartnerManageController.java b/src/main/java/au/com/royalpay/payment/manage/merchants/web/PartnerManageController.java index adb4a313f..d1cbdc529 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/web/PartnerManageController.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/web/PartnerManageController.java @@ -1,9 +1,11 @@ package au.com.royalpay.payment.manage.merchants.web; import au.com.royalpay.payment.channels.rpaypaymentsvc.runtime.request.entities.RPayMerchantEntity; +import au.com.royalpay.payment.core.exceptions.InvalidShortIdException; import au.com.royalpay.payment.manage.dev.core.MerchantLocationService; import au.com.royalpay.payment.manage.merchants.beans.*; import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.manage.merchants.core.bank.AustraliaBankClientNullException; import au.com.royalpay.payment.manage.permission.manager.ManagerMapping; import au.com.royalpay.payment.manage.permission.manager.RequireManager; import au.com.royalpay.payment.manage.pos.datasource.ReadOnlyConnection; @@ -435,9 +437,13 @@ public class PartnerManageController { } @GetMapping("/{clientMoniker}/bank_account/bank_info/{bsb_no}") - public JSONObject getBankInfo(@PathVariable String clientMoniker, @PathVariable String bsb_no, + public JSONObject getBankInfo(@PathVariable String clientMoniker, @PathVariable("bsb_no") String bsbNo, @ModelAttribute(CommonConsts.MANAGER_STATUS) JSONObject manager) { - return clientManager.getBankInfo(manager, clientMoniker, bsb_no); + try { + return clientManager.getBankInfo(manager, clientMoniker, bsbNo); + }catch (AustraliaBankClientNullException e){ + throw new InvalidShortIdException(); + } } @ManagerMapping(value = "/{clientMoniker}/bank_account", method = RequestMethod.PUT, role = {ManagerRole.OPERATOR, ManagerRole.BD_USER, ManagerRole.FINANCIAL_STAFF})