forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
198 additions
and
5 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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/easy-sms. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* 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 SmsbaoGateway | ||
* @author iwindy <[email protected]> | ||
* @see http://www.smsbao.com/openapi/ | ||
*/ | ||
class SmsbaoGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_URL = 'http://api.smsbao.com/%s'; | ||
|
||
const SUCCESS_CODE = '0'; | ||
|
||
protected $errorStatuses = [ | ||
'0' => '短信发送成功', | ||
'-1' => '参数不全', | ||
'-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!', | ||
'30' => '密码错误', | ||
'40' => '账号不存在', | ||
'41' => '余额不足', | ||
'42' => '帐户已过期', | ||
'43' => 'IP地址限制', | ||
'50' => '内容含有敏感词' | ||
]; | ||
|
||
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) | ||
{ | ||
$data = $message->getContent($this); | ||
|
||
if (is_null($to->getIDDCode())) { | ||
$number = $to->getNumber(); | ||
$action = 'sms'; | ||
} else { | ||
$number = $to->getUniversalNumber(); | ||
$action = 'wsms'; | ||
} | ||
|
||
$params = [ | ||
'u' => $config->get('user'), | ||
'p' => md5($config->get('password')), | ||
'm' => $number, | ||
'c' => $data | ||
]; | ||
|
||
$result = $this->get($this->buildEndpoint($action), $params); | ||
|
||
if ($result !== self::SUCCESS_CODE) { | ||
throw new GatewayErrorException($this->errorStatuses[$result], $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected function buildEndpoint($type) | ||
{ | ||
return sprintf(self::ENDPOINT_URL, $type); | ||
} | ||
} |
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,97 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/easy-sms. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* 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\SmsbaoGateway; | ||
use Overtrue\EasySms\Message; | ||
use Overtrue\EasySms\PhoneNumber; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
/** | ||
* Class SmsbaoGatewayTest | ||
* @author iwindy <[email protected]> | ||
*/ | ||
class SmsbaoGatewayTest extends TestCase | ||
{ | ||
public function testSendWithSMS() | ||
{ | ||
$config = [ | ||
'user' => 'mock-user', | ||
'password' => 'mock-password' | ||
]; | ||
|
||
$gateway = \Mockery::mock(SmsbaoGateway::class . '[get]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$params = [ | ||
'u' => 'mock-user', | ||
'p' => md5('mock-password'), | ||
'm' => '18188888888', | ||
'c' => 'This is a test message.' | ||
]; | ||
|
||
$endpoint = sprintf(SmsbaoGateway::ENDPOINT_URL, 'sms'); | ||
$gateway->shouldReceive('get') | ||
->with($endpoint, $params) | ||
->andReturn('0', '30') | ||
->times(2); | ||
|
||
$message = new Message(['content' => 'This is a test message.']); | ||
$config = new Config($config); | ||
|
||
$this->assertSame( | ||
'0', | ||
$gateway->send(new PhoneNumber(18188888888), $message, $config) | ||
); | ||
|
||
$this->expectException(GatewayErrorException::class); | ||
$this->expectExceptionCode(30); | ||
$gateway->send(new PhoneNumber(18188888888), $message, $config); | ||
} | ||
|
||
public function testSendWithWSMS() | ||
{ | ||
$config = [ | ||
'user' => 'mock-user', | ||
'password' => 'mock-password' | ||
]; | ||
|
||
$gateway = \Mockery::mock(SmsbaoGateway::class . '[get]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$params = [ | ||
'u' => 'mock-user', | ||
'p' => md5('mock-password'), | ||
'm' => '+8618188888888', | ||
'c' => 'This is a test message.' | ||
]; | ||
|
||
$endpoint = sprintf(SmsbaoGateway::ENDPOINT_URL, 'wsms'); | ||
$gateway->shouldReceive('get') | ||
->with($endpoint, $params) | ||
->andReturn('0', '30') | ||
->times(2); | ||
|
||
$message = new Message(['content' => 'This is a test message.']); | ||
$config = new Config($config); | ||
|
||
$this->assertSame( | ||
'0', | ||
$gateway->send(new PhoneNumber(18188888888, 86), $message, $config) | ||
); | ||
|
||
$this->expectException(GatewayErrorException::class); | ||
$this->expectExceptionCode(30); | ||
$gateway->send(new PhoneNumber(18188888888, 86), $message, $config); | ||
} | ||
} |