From 8214e507e67bdb34231bf1192c85190977289f07 Mon Sep 17 00:00:00 2001 From: yixian Date: Sat, 8 May 2021 17:32:50 +0800 Subject: [PATCH] =?UTF-8?q?sms=E6=9C=8D=E5=8A=A1=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/impls/ManageAppServiceImp.java | 8 +- .../core/impls/RetailAppServiceImp.java | 4 +- .../impls/SimpleClientApplyServiceImpl.java | 52 ++----- .../core/impls/ClientManagerImpl.java | 21 +-- .../core/impls/SignInAccountServiceImpl.java | 33 +---- .../payment/manage/support/sms/SmsSender.java | 34 ++--- .../manage/support/sms/SmsSenderDaasImpl.java | 130 ++++++++++++++++++ .../support/sms/SmsSenderQcloudImpl.java | 65 +++++++++ .../sms/daas/AuthCodeMessageTemplate.java | 27 ++++ .../support/sms/daas/DaasMessageTemplate.java | 9 ++ .../support/sms/daas/OpenMessageTemplate.java | 19 +++ .../sms/daas/RefuseMessageTemplate.java | 17 +++ .../support/sms/msg/AuthCodeMessage.java | 25 ++++ .../manage/support/sms/msg/OpenMessage.java | 14 ++ .../manage/support/sms/msg/RefuseMessage.java | 13 ++ .../sms/qcloud/QCloudMessageTemplate.java | 4 + src/main/resources/application.yml | 10 +- 17 files changed, 363 insertions(+), 122 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderDaasImpl.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderQcloudImpl.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/daas/AuthCodeMessageTemplate.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/daas/DaasMessageTemplate.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/daas/OpenMessageTemplate.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/daas/RefuseMessageTemplate.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/msg/AuthCodeMessage.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/msg/OpenMessage.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/msg/RefuseMessage.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/support/sms/qcloud/QCloudMessageTemplate.java diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/ManageAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/ManageAppServiceImp.java index 367d85e39..7c7acbebb 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/ManageAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/ManageAppServiceImp.java @@ -15,16 +15,13 @@ import au.com.royalpay.payment.manage.mappers.system.OrgMapper; import au.com.royalpay.payment.manage.merchants.beans.BankAccountInfo; import au.com.royalpay.payment.manage.merchants.beans.ClientAuthFilesInfo; import au.com.royalpay.payment.manage.merchants.beans.ClientRegisterInfo; -import au.com.royalpay.payment.manage.merchants.beans.PartnerQuery; import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.notice.core.MailService; import au.com.royalpay.payment.manage.product.beans.ProductBean; import au.com.royalpay.payment.manage.product.core.ClientProduct; import au.com.royalpay.payment.manage.signin.beans.ChangePwdBean; -import au.com.royalpay.payment.manage.signin.core.ManagerAccountsService; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; -import au.com.royalpay.payment.manage.support.sms.SmsSender; -import au.com.royalpay.payment.tools.device.DeviceSupport; +import au.com.royalpay.payment.manage.support.sms.SmsSenderQcloudImpl; import au.com.royalpay.payment.tools.device.ManageDeviceSupport; import au.com.royalpay.payment.tools.device.support.DeviceRegister; import au.com.royalpay.payment.tools.env.PlatformEnvironment; @@ -37,7 +34,6 @@ import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.ibatis.annotations.Param; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.Cacheable; @@ -91,7 +87,7 @@ public class ManageAppServiceImp implements ManageAppService { @Resource private SpringTemplateEngine thymeleaf; @Resource - private SmsSender smsSender; + private SmsSenderQcloudImpl smsSender; private final String BIND_MANAGE_EMAIL_PREFIX = "BIND_MANAGE_EMAIL"; private final String BIND_MANAGE_PHONE_PREFIX = "BIND_MANAGE_PHONE"; private final int BIND_PHONE_TEMPLID = 126978; diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index 3e9c842ab..9fb4b0595 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -46,7 +46,7 @@ import au.com.royalpay.payment.manage.riskbusiness.enums.RiskResultTypeEnum; import au.com.royalpay.payment.manage.signin.beans.ChangePwdBean; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.signin.core.impls.SignInAccountServiceImpl; -import au.com.royalpay.payment.manage.support.sms.SmsSender; +import au.com.royalpay.payment.manage.support.sms.SmsSenderQcloudImpl; import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery; import au.com.royalpay.payment.manage.tradelog.core.TradeLogService; import au.com.royalpay.payment.manage.tradelog.refund.RefundService; @@ -234,7 +234,7 @@ public class RetailAppServiceImp implements RetailAppService { private final String IMG_AGGREGATE_FILE = "https://file.royalpay.com.au/open/2020/04/08/1586313342533_41vI3w9R8OHrhAVYWvdv7S2IyQra4z.pdf"; @Resource - private SmsSender smsSender; + private SmsSenderQcloudImpl smsSender; private final String BIND_ACCOUNT_EMAIL_PREFIX = "BIND_ACCOUNT_EMAIL"; private final String BIND_ACCOUNT_PHONE_PREFIX = "BIND_ACCOUNT_PHONE"; private final String UNBIND_ACCOUNT_PHONE_PREFIX = "UHBIND_ACCOUNT_PHONE"; diff --git a/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java index 8db3798f0..357d06bb9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java @@ -14,6 +14,8 @@ import au.com.royalpay.payment.manage.notice.core.MailService; import au.com.royalpay.payment.manage.signin.beans.LoginInfo; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.support.sms.SmsSender; +import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage; +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; import au.com.royalpay.payment.manage.system.core.MailGunService; import au.com.royalpay.payment.tools.connections.attachment.core.AttachmentClient; import au.com.royalpay.payment.tools.env.PlatformEnvironment; @@ -55,6 +57,7 @@ import java.awt.image.BufferedImage; import java.io.*; import java.net.URISyntaxException; import java.net.URLDecoder; +import java.time.Duration; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -86,7 +89,7 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private SignInAccountService signInAccountService; @Resource private SimpleClientApplyService simpleClientApplyService; - private Logger logger = LoggerFactory.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); @Resource private ClientAccountMapper clientAccountMapper; @Resource @@ -117,8 +120,6 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private SmsSender smsSender; @Resource private StringRedisTemplate stringRedisTemplate; - private final int REGISTER_CLIENT_TEMPLID = 126978; - private final int REGISTER_CLIENT_TEMPLID_ENGLISH = 346078; private final String REGISTER_CLIENT_PREFIX = "REGISTER_CLIENT"; private final String REGISTER_CLIENT_PROCESS_PREFIX = "REGISTER_CLIENT_PROCESS"; private final String VERIFY_MAIL_PREFIX = "VERIFY_MAIL"; @@ -159,34 +160,18 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { if (StringUtils.isNotEmpty(value)) { throw new BadRequestException("SMS has been sent,Please check your messages or try again in 3 minutes."); } - ArrayList param = new ArrayList<>(); String registerClientCode = RandomStringUtils.random(6, false, true); - param.add("RoyalPay"); - param.add(registerClientCode); - String expireMin = "3"; - param.add(expireMin); - logger.debug("send sms code : {} ", registerClientCode); - try { - if(request.getLocales().nextElement().equals(Locale.CHINESE)|| request.getLocales().nextElement().equals(Locale.SIMPLIFIED_CHINESE)){ - smsSender.getSender().sendWithParam(nationCode.trim(), phoneNumber, REGISTER_CLIENT_TEMPLID, param, "RoyalPay", "", ""); - }else{ - smsSender.getSender().sendWithParam(nationCode.trim(), phoneNumber, REGISTER_CLIENT_TEMPLID_ENGLISH, param, "RoyalPay", "", ""); - } - } catch (Exception e) { - e.printStackTrace(); - throw new ServerErrorException("Phone number is wrong Please try again"); - } - stringRedisTemplate.boundValueOps(getRegisterClientRedisKey(phoneNumber)).set(registerClientCode, Long.parseLong(expireMin), TimeUnit.MINUTES); + int expireMin = 3; + logger.debug("send {} sms code : {} ",phoneNumber, registerClientCode); + smsSender.sendAuthCodeMessage(nationCode.trim(),phoneNumber,request.getLocales().nextElement(),new AuthCodeMessage("RoyalPay", registerClientCode,expireMin)); + stringRedisTemplate.boundValueOps(getRegisterClientRedisKey(phoneNumber)).set(registerClientCode, Duration.ofMinutes(expireMin)); return registerClientCode; } @Override public void sendOpenSms(String phoneNumber, String nationCode) { - ArrayList param = new ArrayList<>(); - String phone_Number = phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7, phoneNumber.length()); - param.add(phone_Number); try { - smsSender.getSender().sendWithParam(nationCode.trim(), phoneNumber, 462770, param, "RoyalPay", "", ""); + smsSender.sendOpenMessage(nationCode.trim(), phoneNumber, Locale.CHINESE, new OpenMessage(phoneNumber)); } catch (Exception e) { e.printStackTrace(); throw new ServerErrorException("Phone number is wrong Please try again"); @@ -857,24 +842,11 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { if (StringUtils.isNotEmpty(value)) { throw new BadRequestException("SMS has been sent,Please check your messages or try again in 1 minutes."); } - ArrayList param = new ArrayList<>(); String registerClientCode = RandomStringUtils.random(6, false, true); - param.add("RoyalPay"); - param.add(registerClientCode); - String expireMin = "1"; logger.debug("{} phone sms send code :{}",phoneNumber,registerClientCode); - param.add(expireMin); - try { - if(request.getLocales().nextElement().equals(Locale.CHINESE)|| request.getLocales().nextElement().equals(Locale.SIMPLIFIED_CHINESE)){ - smsSender.getSender().sendWithParam(nationCode.trim(), phoneNumber, REGISTER_CLIENT_TEMPLID, param, "RoyalPay", "", ""); - }else{ - smsSender.getSender().sendWithParam(nationCode.trim(), phoneNumber, REGISTER_CLIENT_TEMPLID_ENGLISH, param, "RoyalPay", "", ""); - } - } catch (Exception e) { - e.printStackTrace(); - throw new ServerErrorException("Phone number is wrong Please try again"); - } - stringRedisTemplate.boundValueOps(getLoginClientRedisKey(phoneNumber)).set(registerClientCode, Long.parseLong(expireMin), TimeUnit.MINUTES); + int expireMin = 1; + smsSender.sendAuthCodeMessage(nationCode.trim(),phoneNumber,request.getLocales().nextElement(),new AuthCodeMessage("RoyalPay", registerClientCode,expireMin)); + stringRedisTemplate.boundValueOps(getLoginClientRedisKey(phoneNumber)).set(registerClientCode, Duration.ofMinutes(expireMin)); } @Override 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 3b86340f9..00d205680 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 @@ -74,6 +74,7 @@ import au.com.royalpay.payment.manage.signin.core.ManagerTodoNoticeProvider; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.support.serverless.ServerlessFunctionTrigger; import au.com.royalpay.payment.manage.support.sms.SmsSender; +import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage; import au.com.royalpay.payment.manage.system.core.ClientContractService; import au.com.royalpay.payment.manage.system.core.MailGunService; import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery; @@ -205,9 +206,6 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid private ClientComplianceApply clientComplianceApply; @Resource private ClientIncrementalMapper clientIncrementalMapper; - - @Value("${client_card.account_reserve}") - private String cardAccountReserve; @Value("${client_card.annual_rate}") private String cardAnnualRate; @@ -364,7 +362,6 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid @Resource private MpWechatApiProvider mpWechatApiProvider; - private final int REFUSE_CLIENT_TEMPLID = 166108; @Resource private PaymentChannelMccGoodMapper paymentChannelMccGoodMapper; @@ -4968,13 +4965,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid if (client.getIntValue("source") == 4) { String contactPhone = client.getString("contact_phone"); if (contactPhone.startsWith("+61")) { - ArrayList param = new ArrayList<>(); - param.add(refuseRemark); - try { - smsSender.getSender().sendWithParam("61", contactPhone.replace("+61", ""), REFUSE_CLIENT_TEMPLID, param, "RoyalPay", "", ""); - } catch (Exception ignore) { - throw new ServerErrorException("Phone number is wrong :" + contactPhone); - } + smsSender.sendRefuseMessage("61", contactPhone, Locale.CHINESE, new RefuseMessage(refuseRemark)); } } if (client.getIntValue("source") == 5) { @@ -5015,13 +5006,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid if (client.getIntValue("source") == 4) { String contactPhone = client.getString("contact_phone"); if (contactPhone.startsWith("+61")) { - ArrayList param = new ArrayList<>(); - param.add(refuseRemark); - try { - smsSender.getSender().sendWithParam("61", contactPhone.replace("+61", ""), REFUSE_CLIENT_TEMPLID, param, "RoyalPay", "", ""); - } catch (Exception ignore) { - throw new ServerErrorException("Phone number is wrong :" + contactPhone); - } + smsSender.sendRefuseMessage("61", contactPhone, Locale.CHINESE, new RefuseMessage(refuseRemark)); } } if (client.getIntValue("source") == 5) { diff --git a/src/main/java/au/com/royalpay/payment/manage/signin/core/impls/SignInAccountServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/signin/core/impls/SignInAccountServiceImpl.java index 6b556e27a..c28390ea9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/signin/core/impls/SignInAccountServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/signin/core/impls/SignInAccountServiceImpl.java @@ -15,12 +15,12 @@ import au.com.royalpay.payment.manage.signin.core.SignInStatusManager; import au.com.royalpay.payment.manage.signin.events.ClientLoginEvent; import au.com.royalpay.payment.manage.signin.events.ManagerLoginEvent; import au.com.royalpay.payment.manage.support.sms.SmsSender; +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; import au.com.royalpay.payment.manage.system.core.PermissionClientModulesService; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.env.RequestEnvironment; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import au.com.royalpay.payment.tools.exceptions.ForbiddenException; -import au.com.royalpay.payment.tools.exceptions.ServerErrorException; import au.com.royalpay.payment.tools.locale.LocaleSupport; import au.com.royalpay.payment.tools.merchants.core.MerchantInfoProvider; import au.com.royalpay.payment.tools.permission.enums.ManagerRole; @@ -44,10 +44,7 @@ import org.thymeleaf.spring5.SpringTemplateEngine; import javax.annotation.PostConstruct; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -105,7 +102,7 @@ public class SignInAccountServiceImpl implements SignInAccountService, Applicati private final String[] KYC_FILE_KEYS = {"client_bank_file", "kyc_utility_bill_file", "client_id_file"}; private final String[] KYC_PUT_KEYS = {"file_bank_info", "utility_bill_info", "file_id_info"}; private final String[] KYC_FILE_NAMES = {"* ASIC File", "Utility Bill Files", "* ID"}; - private final int RESET_PASSWORD_TEMPLID = 126978; + private ApplicationEventPublisher publisher; private static final List tags = new ArrayList<>(); @@ -574,17 +571,7 @@ public class SignInAccountServiceImpl implements SignInAccountService, Applicati }); break; case "phone": - ArrayList param = new ArrayList<>(); - param.add("密码重置服务"); - param.add(codeKeyValue); - String expireMin = "5"; - param.add(expireMin); - try { - smsSender.getSender().sendWithParam(account.getString("nation_code").trim(), account.getString("contact_phone"), RESET_PASSWORD_TEMPLID, param, "RoyalPay", "", ""); - } catch (Exception e) { - e.printStackTrace(); - throw new ServerErrorException("Phone number is wrong.Please try again."); - } + smsSender.sendAuthCodeMessage(account.getString("nation_code").trim(), account.getString("contact_phone"), Locale.ENGLISH, new AuthCodeMessage("密码重置服务", codeKeyValue, 5)); break; } stringRedisTemplate.boundValueOps(getResetClientAccountKey(accountId)).set(codeKeyValue, 5, TimeUnit.MINUTES); @@ -620,17 +607,7 @@ public class SignInAccountServiceImpl implements SignInAccountService, Applicati }); break; case "phone": - ArrayList param = new ArrayList<>(); - param.add("密码重置服务"); - param.add(codeKeyValue); - String expireMin = "5"; - param.add(expireMin); - try { - smsSender.getSender().sendWithParam(account.getString("nation_code").trim(), account.getString("phone"), RESET_PASSWORD_TEMPLID, param, "RoyalPay", "", ""); - } catch (Exception e) { - e.printStackTrace(); - throw new ServerErrorException("Phone number is wrong.Please try again."); - } + smsSender.sendAuthCodeMessage(account.getString("nation_code").trim(), account.getString("phone"), Locale.CHINESE, new AuthCodeMessage("密码重置服务",codeKeyValue,5)); break; } stringRedisTemplate.boundValueOps(getResetManagerAccountKey(managerId)).set(codeKeyValue, 5, TimeUnit.MINUTES); diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSender.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSender.java index 8373b5a63..27d31dbba 100644 --- a/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSender.java +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSender.java @@ -1,33 +1,15 @@ package au.com.royalpay.payment.manage.support.sms; -import com.github.qcloudsms.SmsSingleSender; +import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage; +import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage; +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; +import java.util.Locale; -import javax.annotation.PostConstruct; +public interface SmsSender { + void sendOpenMessage(String nationCode, String mobile, Locale locale, OpenMessage msg); -/** - * @author kira - * @date 2018/8/1 - */ -@Component -public class SmsSender { - - @Value("${royalpay.sms.appid:1400094878}") - private int appId; - @Value("${royalpay.sms.appkey:43390d81e20c5191c278fbf4cd275be2}") - private String appKey; - - private SmsSingleSender sender = null; - - @PostConstruct - public void init() { - sender = new SmsSingleSender(appId, appKey); - } - - public SmsSingleSender getSender(){ - return sender; - } + void sendAuthCodeMessage(String nationCode, String mobile, Locale locale, AuthCodeMessage register); + void sendRefuseMessage(String nationCode, String mobile, Locale locale, RefuseMessage refuse); } diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderDaasImpl.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderDaasImpl.java new file mode 100644 index 000000000..ffc452a1a --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderDaasImpl.java @@ -0,0 +1,130 @@ +package au.com.royalpay.payment.manage.support.sms; + +import au.com.royalpay.payment.manage.support.sms.daas.DaasMessageTemplate; +import au.com.royalpay.payment.manage.support.sms.daas.OpenMessageTemplate; +import au.com.royalpay.payment.manage.support.sms.daas.RefuseMessageTemplate; +import au.com.royalpay.payment.manage.support.sms.daas.AuthCodeMessageTemplate; +import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage; +import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage; +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; +import org.apache.commons.codec.binary.Base64; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.FormHttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestClientResponseException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.server.ServerErrorException; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.util.Locale; +import java.util.Optional; + +@Service +@ConditionalOnProperty(value = "royalpay.sms.daas.enable", havingValue = "true") +public class SmsSenderDaasImpl implements SmsSender { + private final Logger logger = LoggerFactory.getLogger(getClass()); + private final String serectId; + private final String secretKey; + private final RestTemplate restTemplate; + private static final String URL = "https://service-rzq04n9p-1255701024.sh.apigw.tencentcs.com/release/superapi/super/sms/internation/single"; + + public SmsSenderDaasImpl(@Value("${royalpay.sms.daas.secret-id}") String serectId, @Value("${royalpay.sms.daas.secret-key}") String secretKey) { + this.serectId = serectId; + this.secretKey = secretKey; + this.restTemplate = new RestTemplateBuilder().messageConverters(new FormHttpMessageConverter(), + new StringHttpMessageConverter(StandardCharsets.UTF_8), + new FastJsonHttpMessageConverter()) + .build(); + } + + @Override + public void sendOpenMessage(String nationCode, String mobile, Locale locale, OpenMessage msg) { + sendSms(nationCode, mobile, new OpenMessageTemplate(msg)); + } + + @Override + public void sendAuthCodeMessage(String nationCode, String mobile, Locale locale, AuthCodeMessage register) { + sendSms(nationCode, mobile, new AuthCodeMessageTemplate(locale, register)); + } + + @Override + public void sendRefuseMessage(String nationCode, String mobile, Locale locale, RefuseMessage refuse) { + sendSms(nationCode, mobile, new RefuseMessageTemplate(refuse)); + } + + private void sendSms(String nationCode, String mobile, DaasMessageTemplate tpl) { + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("mobile", nationCode + mobile.replaceAll("^(\\+61)(61)", "")); + body.add("msg", "[RoyalPay]" + tpl.getMessage()); + RequestEntity> request = RequestEntity.post(URI.create(URL)) + .headers(signature()).contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(body); + try { + ResponseEntity resp = restTemplate.exchange(request, JSONObject.class); + JSONObject respBody = resp.getBody(); + logger.debug("daas sms response:{}", respBody); + Optional.ofNullable(respBody) + .map(resBody -> resBody.getJSONArray("data")) + .filter(arr -> !arr.isEmpty()) + .map(dataArr -> dataArr.getJSONObject(0)) + .ifPresent(data -> { + JSONObject record0 = data.getJSONArray("record").getJSONObject(0); + logger.info("send sms success:[{}]{}, messageNum:{}", record0.getString("resCode"), record0.getString("resDesc"), data.getString("recordNum")); + }); + } catch (RestClientResponseException e) { + logger.error("request sms service failed:[{}]{}", e.getRawStatusCode(), e.getResponseBodyAsString(), e); + } catch (RestClientException e) { + logger.error("request sms service failed", e); + } + } + + private HttpHeaders signature() { + String source = "market"; + String datetime = DateTime.now().withZone(DateTimeZone.UTC).toString("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH); + try { + String auth = calcAuthorization(source, serectId, secretKey, datetime); + MultiValueMap headers = new LinkedMultiValueMap<>(); + headers.add("X-Source", source); + headers.add("X-Date", datetime); + headers.add("Authorization", auth); + return new HttpHeaders(headers); + } catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) { + throw new ServerErrorException("Signature failed", e); + } + } + + public static String calcAuthorization(String source, String secretId, String secretKey, String datetime) + throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { + String signStr = "x-date: " + datetime + "\n" + "x-source: " + source; + Mac mac = Mac.getInstance("HmacSHA1"); + Key sKey = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm()); + mac.init(sKey); + byte[] hash = mac.doFinal(signStr.getBytes(StandardCharsets.UTF_8)); + String sig = Base64.encodeBase64String(hash); + + return "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\""; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderQcloudImpl.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderQcloudImpl.java new file mode 100644 index 000000000..73e83ad17 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/SmsSenderQcloudImpl.java @@ -0,0 +1,65 @@ +package au.com.royalpay.payment.manage.support.sms; + +import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage; +import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage; +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; +import com.github.qcloudsms.SmsSingleSender; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +import java.util.Locale; + +/** + * @author kira + * @date 2018/8/1 + */ +@Component +@ConditionalOnProperty(value = "royalpay.sms.qcloud.enable", havingValue = "true") +public class SmsSenderQcloudImpl implements SmsSender { + + private static final int OPEN_MESSAGE_ID = 462770; + private static final int REGISTER_CLIENT_TEMPLID_ZH = 126978; + private static final int REGISTER_CLIENT_TEMPLID_EN = 346078; + private static final int REFUSE_CLIENT_TEMPLID = 166108; + private static final int RESET_PASSWORD_TEMPLID = 126978; + private final SmsSingleSender sender; + private final String sign; + + public SmsSenderQcloudImpl(@Value("${royalpay.sms.qcloud.appid}") int appId, @Value("${royalpay.sms.qcloud.appkey}") String appKey) { + sender = new SmsSingleSender(appId, appKey); + sign = "RoyalPay"; + } + + public SmsSingleSender getSender() { + return sender; + } + + @Override + public void sendOpenMessage(String nationCode, String mobile, Locale locale, OpenMessage msg) { + sendMessage(nationCode, mobile, OPEN_MESSAGE_ID, msg.getMaskedPhoneNumber()); + } + + private void sendMessage(String nationCode, String mobile, int tplId, String... param) { + try { + sender.sendWithParam(nationCode, mobile.replace("+61", ""), tplId, param, sign, "", ""); + } catch (Exception e) { + throw new ServerErrorException("Phone number is wrong :" + mobile, e); + } + } + + @Override + public void sendAuthCodeMessage(String nationCode, String mobile, Locale locale, AuthCodeMessage register) { + if (locale.equals(Locale.CHINESE) || locale.equals(Locale.SIMPLIFIED_CHINESE)) { + sendMessage(nationCode, mobile, REGISTER_CLIENT_TEMPLID_ZH, register.getBiz(), register.getCode(), register.getExpireMin() + ""); + } else { + sendMessage(nationCode, mobile, REGISTER_CLIENT_TEMPLID_EN, register.getBiz(), register.getCode(), register.getExpireMin() + ""); + } + } + + @Override + public void sendRefuseMessage(String nationCode, String mobile, Locale locale, RefuseMessage refuse) { + sendMessage(nationCode, mobile, REFUSE_CLIENT_TEMPLID, refuse.getReason()); + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/AuthCodeMessageTemplate.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/AuthCodeMessageTemplate.java new file mode 100644 index 000000000..82c3005ef --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/AuthCodeMessageTemplate.java @@ -0,0 +1,27 @@ +package au.com.royalpay.payment.manage.support.sms.daas; + +import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage; + +import java.util.Locale; + +public class AuthCodeMessageTemplate implements DaasMessageTemplate { + private static final String CN_TPL = "您正在申请%s,验证码为%s,%s分钟内有效,请勿向任何人提供此验证码"; + private static final String EN_TPL = "Your %s secret code is %s(valid for %s minutes). Do not provide this to anyone."; + private final Locale locale; + private final AuthCodeMessage register; + + public AuthCodeMessageTemplate(Locale locale, AuthCodeMessage register) { + this.locale = locale; + this.register = register; + } + + @Override + public String getMessage() { + String tpl = EN_TPL; + if (locale.equals(Locale.CHINESE) || locale.equals(Locale.SIMPLIFIED_CHINESE)) { + tpl = CN_TPL; + } + return String.format(tpl, register.getBiz(), register.getCode(), register.getExpireMin()); + } + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/DaasMessageTemplate.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/DaasMessageTemplate.java new file mode 100644 index 000000000..d01811e03 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/DaasMessageTemplate.java @@ -0,0 +1,9 @@ +package au.com.royalpay.payment.manage.support.sms.daas; + +import org.springframework.util.MultiValueMap; + +public interface DaasMessageTemplate { + + String getMessage(); + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/OpenMessageTemplate.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/OpenMessageTemplate.java new file mode 100644 index 000000000..ebc30e0b1 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/OpenMessageTemplate.java @@ -0,0 +1,19 @@ +package au.com.royalpay.payment.manage.support.sms.daas; + +import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage; + +public class OpenMessageTemplate implements DaasMessageTemplate { + private static final String TEMPLATE = "尊敬的商户,恭喜您注册成功,请使用您的(%s)账户登录网址(https://mpay.royalpay.com.au/)或者使用RoyalPay手机App。您可以使用RoyalPay所有的功能,包括接受客户的付款,在您激活账号之前,RoyalPay将不执行清算操作。因此,请您尽快提交合规材料,若有任何疑问,请联系我们客服。"; + private final OpenMessage openMessage; + + public OpenMessageTemplate(OpenMessage openMessage) { + this.openMessage = openMessage; + } + + + @Override + public String getMessage() { + return String.format(TEMPLATE, openMessage.getMaskedPhoneNumber()); + } + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/RefuseMessageTemplate.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/RefuseMessageTemplate.java new file mode 100644 index 000000000..f0155965e --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/daas/RefuseMessageTemplate.java @@ -0,0 +1,17 @@ +package au.com.royalpay.payment.manage.support.sms.daas; + +import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage; + +public class RefuseMessageTemplate implements DaasMessageTemplate{ + private static final String TPL = "尊敬的RoyalPay商户,您提交的商户资料审核不通过,原因(%s),请登录系统后台修改后,重新提交审核。[RoyalPay合规部]"; + private final RefuseMessage refuseMessage; + + public RefuseMessageTemplate(RefuseMessage refuseMessage) { + this.refuseMessage = refuseMessage; + } + + @Override + public String getMessage() { + return String.format(TPL,refuseMessage.getReason()); + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/AuthCodeMessage.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/AuthCodeMessage.java new file mode 100644 index 000000000..da844eb82 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/AuthCodeMessage.java @@ -0,0 +1,25 @@ +package au.com.royalpay.payment.manage.support.sms.msg; + +public class AuthCodeMessage { + private final String biz; + private final String code; + private final int expireMin; + + public AuthCodeMessage(String biz, String code, int expireMin) { + this.biz = biz; + this.code = code; + this.expireMin = expireMin; + } + + public String getBiz() { + return biz; + } + + public String getCode() { + return code; + } + + public int getExpireMin() { + return expireMin; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/OpenMessage.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/OpenMessage.java new file mode 100644 index 000000000..9cadf9ef2 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/OpenMessage.java @@ -0,0 +1,14 @@ +package au.com.royalpay.payment.manage.support.sms.msg; + +public class OpenMessage { + private final String phoneNumber; + + + public OpenMessage(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getMaskedPhoneNumber(){ + return new StringBuilder(phoneNumber).replace(3,7,"****").toString(); + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/RefuseMessage.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/RefuseMessage.java new file mode 100644 index 000000000..a8fde5868 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/msg/RefuseMessage.java @@ -0,0 +1,13 @@ +package au.com.royalpay.payment.manage.support.sms.msg; + +public class RefuseMessage { + private final String reason; + + public RefuseMessage(String reason) { + this.reason = reason; + } + + public String getReason() { + return reason; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/sms/qcloud/QCloudMessageTemplate.java b/src/main/java/au/com/royalpay/payment/manage/support/sms/qcloud/QCloudMessageTemplate.java new file mode 100644 index 000000000..018ddab9c --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/sms/qcloud/QCloudMessageTemplate.java @@ -0,0 +1,4 @@ +package au.com.royalpay.payment.manage.support.sms.qcloud; + +public interface QCloudMessageTemplate { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bf9fc95dd..32c9cd361 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -120,8 +120,14 @@ qcloud: secret-key: YswoQDsIZfWEqEAEvMXS8Yic84lFn9Jp royalpay: sms: - appid: 1400094878 - appkey: 43390d81e20c5191c278fbf4cd275be2 + qcloud: + appid: 1400094878 + appkey: 43390d81e20c5191c278fbf4cd275be2 + enable: false + daas: + secret-id: AKIDKR8WAXdaRcfbZysCU41K6ViGMwL2XG49zdDE + secret-key: 15ug40UL3X74v7705tKqb2u0bmqyOr873Yyyn41C + enable: true settle: abafile: bank: