-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加: 时代互联的短信网关API https://www.now.cn (#347)
* add: 时代互联的短信网关API https://www.now.cn * ### 补充时代互联文档说明 --------- Co-authored-by: dsuzejian <[email protected]> Co-authored-by: 安正超 <[email protected]>
- Loading branch information
1 parent
78b9bda
commit 81d4dee
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |