-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 增加移动云MAS黑名单模式通道 * 修正名称 * 增加移动云MAS黑名单通道(格式修复) * 增加移动云MAS黑名单模式通道
- Loading branch information
Showing
3 changed files
with
184 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,75 @@ | ||
<?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 YidongmasblackGateway. | ||
* 移动MAS黑名单模式(免创建模板) | ||
* @author houang <[email protected]> | ||
* | ||
* @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)); | ||
} | ||
} |
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,87 @@ | ||
<?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\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); | ||
} | ||
} |