sms服务更新:根据nationCode路由

master
yixian 4 years ago
parent f799b86c4d
commit 8c49b413d5

@ -7,6 +7,8 @@ import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage;
import java.util.Locale;
public interface SmsSender {
void sendOpenMessage(String nationCode, String mobile, Locale locale, OpenMessage msg);
void sendAuthCodeMessage(String nationCode, String mobile, Locale locale, AuthCodeMessage register);

@ -0,0 +1,7 @@
package au.com.royalpay.payment.manage.support.sms;
import org.springframework.core.Ordered;
public interface SmsSenderChannel extends SmsSender, Ordered {
boolean supportNationCode(String nationCode);
}

@ -44,15 +44,15 @@ import java.util.Optional;
@Service
@ConditionalOnProperty(value = "royalpay.sms.daas.enable", havingValue = "true")
public class SmsSenderDaasImpl implements SmsSender {
public class SmsSenderDaasImpl implements SmsSenderChannel {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final String serectId;
private final String secretId;
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;
public SmsSenderDaasImpl(@Value("${royalpay.sms.daas.secret-id}") String secretId, @Value("${royalpay.sms.daas.secret-key}") String secretKey) {
this.secretId = secretId;
this.secretKey = secretKey;
this.restTemplate = new RestTemplateBuilder().messageConverters(new FormHttpMessageConverter(),
new StringHttpMessageConverter(StandardCharsets.UTF_8),
@ -105,7 +105,7 @@ public class SmsSenderDaasImpl implements SmsSender {
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);
String auth = calcAuthorization(source, secretId, secretKey, datetime);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("X-Source", source);
headers.add("X-Date", datetime);
@ -127,4 +127,14 @@ public class SmsSenderDaasImpl implements SmsSender {
return "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\"";
}
@Override
public boolean supportNationCode(String nationCode) {
return "61".equals(nationCode);
}
@Override
public int getOrder() {
return 100;
}
}

@ -7,6 +7,7 @@ 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.core.Ordered;
import org.springframework.stereotype.Component;
import java.util.Locale;
@ -17,7 +18,7 @@ import java.util.Locale;
*/
@Component
@ConditionalOnProperty(value = "royalpay.sms.qcloud.enable", havingValue = "true")
public class SmsSenderQcloudImpl implements SmsSender {
public class SmsSenderQcloudImpl implements SmsSenderChannel {
private static final int OPEN_MESSAGE_ID = 462770;
private static final int REGISTER_CLIENT_TEMPLID_ZH = 126978;
@ -61,4 +62,14 @@ public class SmsSenderQcloudImpl implements SmsSender {
public void sendRefuseMessage(String nationCode, String mobile, Locale locale, RefuseMessage refuse) {
sendMessage(nationCode, mobile, REFUSE_CLIENT_TEMPLID, refuse.getReason());
}
@Override
public boolean supportNationCode(String nationCode) {
return true;
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}

@ -0,0 +1,35 @@
package au.com.royalpay.payment.manage.support.sms;
import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage;
import au.com.royalpay.payment.manage.support.sms.msg.OpenMessage;
import au.com.royalpay.payment.manage.support.sms.msg.RefuseMessage;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Locale;
@Service
@Primary
public class SmsSenderRoute implements SmsSender{
private final List<SmsSenderChannel> channels;
public SmsSenderRoute(List<SmsSenderChannel> channels) {
this.channels = channels;
}
@Override
public void sendOpenMessage(String nationCode, String mobile, Locale locale, OpenMessage msg) {
channels.stream().filter(channel->channel.supportNationCode(nationCode)).findFirst().ifPresent(channel->channel.sendOpenMessage(nationCode, mobile, locale, msg));
}
@Override
public void sendAuthCodeMessage(String nationCode, String mobile, Locale locale, AuthCodeMessage register) {
channels.stream().filter(channel->channel.supportNationCode(nationCode)).findFirst().ifPresent(channel->channel.sendAuthCodeMessage(nationCode, mobile, locale, register));
}
@Override
public void sendRefuseMessage(String nationCode, String mobile, Locale locale, RefuseMessage refuse) {
channels.stream().filter(channel->channel.supportNationCode(nationCode)).findFirst().ifPresent(channel->channel.sendRefuseMessage(nationCode, mobile, locale, refuse));
}
}

@ -123,7 +123,7 @@ royalpay:
qcloud:
appid: 1400094878
appkey: 43390d81e20c5191c278fbf4cd275be2
enable: false
enable: true
daas:
secret-id: AKIDKR8WAXdaRcfbZysCU41K6ViGMwL2XG49zdDE
secret-key: 15ug40UL3X74v7705tKqb2u0bmqyOr873Yyyn41C

@ -0,0 +1,23 @@
package au.com.royalpay.payment.manage.support.sms;
import au.com.royalpay.payment.manage.support.sms.msg.AuthCodeMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Locale;
import static org.junit.jupiter.api.Assertions.*;
class SmsSenderDaasImplTest {
private SmsSender sender;
@BeforeEach
void initService() {
sender = new SmsSenderQcloudImpl(1400094878, "43390d81e20c5191c278fbf4cd275be2");
}
@Test
void testSms() {
sender.sendAuthCodeMessage("86", "18913315655", Locale.CHINESE, new AuthCodeMessage("测试", "111", 1));
}
}
Loading…
Cancel
Save