Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加: 时代互联的短信网关API https://www.now.cn #347

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [蜘蛛云](https://zzyun.com/)
- [融合云信](https://maap.wo.cn/)
- [天瑞云](http://cms.tinree.com/)
- [时代互联](https://www.now.cn/)
- [火山引擎](https://console.volcengine.com/sms/)

## 环境需求
Expand Down Expand Up @@ -925,6 +926,25 @@ $easySms->send(18888888888, [
]);
```

### [时代互联](https://www.now.cn/)

短信使用 `content`

```php
'nowcn' => [
'key' => '', //用户ID
'secret' => '', //开发密钥
'api_type' => '', // 短信通道,
],
```

发送示例:
```php
$easySms->send(18888888888, [
'content' => '您的验证码为: 6379',
]);
```

### [火山引擎](https://console.volcengine.com/sms/)

短信内容使用 `template` + `data`
Expand Down
34 changes: 34 additions & 0 deletions src/Gateways/NowcnGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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 NowcnGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'http://ad1200.now.net.cn:2003/sms/sendSMS';

const SUCCESS_CODE = 0;

public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$params=[
'mobile' => $to->getNumber(),
'content' => $message->getContent($this),
'userId' => $config->get('key'),
'password' => $config->get('secret'),
'apiType' => $config->get('api_type'),
];
$result = $this->get(self::ENDPOINT_URL, $params);
if (self::SUCCESS_CODE != $result['code']) {
throw new GatewayErrorException($result['msg'], $result['code'], $result);
}
return $result;
}
}
52 changes: 52 additions & 0 deletions tests/Gateways/NowcnGatewaysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Overtrue\EasySms\Tests\Gateways;

use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\NowcnGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;

class NowcnGatewaysTest extends TestCase
{
public function testSend()
{
$config = [
'key' => '1',
'secret' => '1',
'api_type' => '3',
];

$gateway = \Mockery::mock(NowcnGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('request')->with(
'get',
\Mockery::on(function ($api) {
return 0 === strpos($api, NowcnGateway::ENDPOINT_URL);
}),
\Mockery::on(function ($params) {
return true;
})
) ->andReturn([
'code' => NowcnGateway::SUCCESS_CODE,
], [
'code' => "-4",
'msg' => 'authorize failed',
])->times(2);

$message = new Message([
'content' => 'mock-content',
]);
$config = new Config($config);
$this->assertSame([
'code' => NowcnGateway::SUCCESS_CODE,
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(-4);
$this->expectExceptionMessage('authorize failed');
$gateway->send(new PhoneNumber(18888888888), $message, $config);

}
}
Loading