diff --git a/README.md b/README.md index eb8ffe0..24c86b2 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ - [天瑞云](http://cms.tinree.com/) - [时代互联](https://www.now.cn/) - [火山引擎](https://console.volcengine.com/sms/) +- [移动云MAS(黑名单模式)](https://mas.10086.cn) ## 环境需求 @@ -995,6 +996,27 @@ $easySms->send(18888888888, [ ]); ``` +### [移动云MAS(黑名单模式)](https://mas.10086.cn/) + +短信内容使用 `template` + `data` + +```php + 'yidongmasblack' => [ + 'ecName' => '', // 机构名称 + 'secretKey' => '', // 密钥 + 'apId' => '', // 应用ID + 'sign' => '', // 签名 + 'addSerial' => '', // 通道号默认空 + ], +``` + +发送示例: + +```php +$easySms->send(18888888888, [ + 'content' => '您的验证码为: 6379', +]); +``` ## :heart: 支持我 [![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue) diff --git a/src/Gateways/YiDongMasBlackGateway.php b/src/Gateways/YiDongMasBlackGateway.php new file mode 100644 index 0000000..f8f2c00 --- /dev/null +++ b/src/Gateways/YiDongMasBlackGateway.php @@ -0,0 +1,75 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Overtrue\EasySms\Gateways; + +use Overtrue\EasySms\Contracts\MessageInterface; +use Overtrue\EasySms\Contracts\PhoneNumberInterface; +use Overtrue\EasySms\Exceptions\GatewayErrorException; +use Overtrue\EasySms\Support\Config; +use Overtrue\EasySms\Traits\HasHttpRequest; + +/** + * Class YidongmasblackGateway. + * 移动MAS黑名单模式(免创建模板) + * @author houang + * + * @see https://mas.10086.cn + */ +class YidongmasblackGateway extends Gateway +{ + use HasHttpRequest; + + const ENDPOINT_URL = 'http://112.35.1.155:1992/sms/norsubmit'; + + const ENDPOINT_METHOD = 'send'; + + /** + * @param PhoneNumberInterface $to + * @param MessageInterface $message + * @param Config $config + * + * @return \Psr\Http\Message\ResponseInterface|array|string + * + * @throws GatewayErrorException + */ + public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) + { + $params["ecName"] = $config->get('ecName'); + $params["apId"] = $config->get('apId'); + $params["sign"] = $config->get('sign'); + $params["addSerial"] = $config->get('addSerial'); + $params["mobiles"] = $to->getNumber(); + $params["content"] = $message->getContent(); + $result = $this->postJson(self::ENDPOINT_URL, $this->generateContent($params)); + + if ('true' != $result['success']) { + throw new GatewayErrorException($result['success'], $result['rspcod'], $result); + } + + return $result; + } + + /** + * Generate Content. + * + * @param array $params + * + * @return string + */ + protected function generateContent($params) + { + $secretKey = $this->config->get('secretKey'); + $params['mac'] = md5($params["ecName"].$params["apId"].$secretKey.$params["mobiles"].$params["content"].$params["sign"].$params["addSerial"]); + + return base64_encode(json_encode($params)); + } +} diff --git a/tests/Gateways/YiDongMasBlackGatewayTest.php b/tests/Gateways/YiDongMasBlackGatewayTest.php new file mode 100644 index 0000000..ca65532 --- /dev/null +++ b/tests/Gateways/YiDongMasBlackGatewayTest.php @@ -0,0 +1,87 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Overtrue\EasySms\Tests\Gateways; + +use Overtrue\EasySms\Exceptions\GatewayErrorException; +use Overtrue\EasySms\Gateways\YidongmasblackGateway; +use Overtrue\EasySms\Message; +use Overtrue\EasySms\PhoneNumber; +use Overtrue\EasySms\Support\Config; +use Overtrue\EasySms\Tests\TestCase; + +class YidongmasblackGatewayTest extends TestCase +{ + public function testSend() + { + $config = [ + 'ecName' => 'mock-ec-name', + 'secretKey' => 'mock-secret-key', + 'apId' => 'mock-ap-id', + 'sign' => 'mock-sign', + 'addSerial' => 'mock-add-serial', + ]; + $gateway = \Mockery::mock(YidongmasblackGateway::class.'[get]', [$config])->shouldAllowMockingProtectedMethods(); + + $expected = [ + 'RegionId' => 'cn-hangzhou', + 'AccessKeyId' => 'mock-api-key', + 'Format' => 'JSON', + 'SignatureMethod' => 'HMAC-SHA1', + 'SignatureVersion' => '1.0', + // 'SignatureNonce' => uniqid(), + // 'Timestamp' => date('Y-m-d\TH:i:s\Z'), + 'Action' => 'SendSms', + 'Version' => '2017-05-25', + 'PhoneNumbers' => strval(new PhoneNumber(18888888888)), + 'SignName' => 'mock-api-sign-name', + 'TemplateCode' => 'mock-template-code', + 'TemplateParam' => json_encode(['code' => '123456']), + ]; + $gateway->shouldReceive('post') + ->with(YidongmasblackGateway::ENDPOINT_URL, \Mockery::on(function ($params) use ($expected) { + if (empty($params['Signature'])) { + return false; + } + + unset($params['SignatureNonce'], $params['Timestamp'], $params['Signature']); + + ksort($params); + ksort($expected); + + return $params == $expected; + })) + ->andReturn([ + 'Code' => 'OK', + 'Message' => 'mock-result', + ], [ + 'Code' => 1234, + 'Message' => 'mock-err-msg', + ]) + ->twice(); + + $message = new Message([ + 'content' => '123456', + ]); + + $config = new Config($config); + + $this->assertSame([ + 'Code' => 'OK', + 'Message' => 'mock-result', + ], $gateway->send(new PhoneNumber(18888888888), $message, $config)); + + $this->expectException(GatewayErrorException::class); + $this->expectExceptionCode(1234); + $this->expectExceptionMessage('mock-err-msg'); + $gateway->send(new PhoneNumber(18888888888), $message, $config); + } +}