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
195 additions
and
1 deletion.
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
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,105 @@ | ||
<?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\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Traits\HasHttpRequest; | ||
|
||
/** | ||
* Class AlidayuGateway. | ||
* | ||
* @see https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=xxxxx&random=xxxx | ||
*/ | ||
class QcloudGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_URL = 'https://yun.tim.qq.com/v5/'; | ||
|
||
const ENDPOINT_METHOD = 'tlssmssvr/sendsms'; | ||
|
||
const ENDPOINT_VERSION = 'v5'; | ||
|
||
const ENDPOINT_FORMAT = 'json'; | ||
|
||
/** | ||
* Get gateway name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'qcloud'; | ||
} | ||
|
||
/** | ||
* @param array|int|string $to | ||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message | ||
* @param \Overtrue\EasySms\Support\Config $config | ||
* | ||
* @return array | ||
* | ||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
*/ | ||
public function send($to, MessageInterface $message, Config $config) | ||
{ | ||
$params = [ | ||
'tel' => [ | ||
'nationcode' => $message->getData($this)['notioncode'] ?? '86', | ||
'mobile' => $to, | ||
], | ||
'type' => $message->getData($this)['type'] ?? 0, | ||
'msg' => $message->getContent($this), | ||
'time' => time(), | ||
'extend' => '', | ||
'ext' => '', | ||
]; | ||
|
||
$random = str_random(10); | ||
|
||
$params['sig'] = $this->generateSign($params, $random); | ||
|
||
$url = self::ENDPOINT_URL . self::ENDPOINT_METHOD . '?sdkappid=' . $config->get('sdk_app_id') . '&random=' . $random; | ||
|
||
$result = $this->request('post', $url, [ | ||
'headers' => ['Accept' => 'application/json'], | ||
'json' => $params, | ||
]); | ||
|
||
if ($result['result'] != 0) { | ||
throw new GatewayErrorException($result['errmsg'], $result['result'], $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Generate Sign. | ||
* | ||
* @param array $params | ||
* @param string $random | ||
* | ||
* @return string | ||
*/ | ||
protected function generateSign($params, $random) | ||
{ | ||
ksort($params); | ||
|
||
return hash("sha256", sprintf('appkey=%s&random=%s&time=%s&mobile=%s', | ||
$this->config->get('app_key'), | ||
$random, | ||
$params['time'], | ||
$params['tel']['mobile']), false); | ||
} | ||
} |
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,77 @@ | ||
<?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\QcloudGateway; | ||
use Overtrue\EasySms\Message; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
class QcloudGatewayTest extends TestCase | ||
{ | ||
public function testGetName() | ||
{ | ||
$this->assertSame('tencent', (new QcloudGateway([]))->getName()); | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$config = [ | ||
'sdk_app_id' => 'mock-sdk-app-id', | ||
'app_key' => 'mock-api-key', | ||
]; | ||
$gateway = \Mockery::mock(QcloudGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$expected = [ | ||
'tel' => [ | ||
'nationcode' => '86', | ||
'mobile' => strval(18888888888), | ||
], | ||
'type' => 0, | ||
'msg' => 'This is a test message.', | ||
'timestamp' => time(), | ||
'extend' => '', | ||
'ext' => '', | ||
]; | ||
|
||
$gateway->shouldReceive('request') | ||
->andReturn([ | ||
'result' => 0, | ||
'errmsg' => 'OK', | ||
'ext' => '', | ||
'sid' => 3310228982, | ||
'fee' => 1 | ||
], [ | ||
'result' => 1001, | ||
'errmsg' => 'sig校验失败', | ||
])->twice(); | ||
|
||
$message = new Message(['data' => [ 'type' => 0 ], 'content' => 'This is a test message.']); | ||
|
||
$config = new Config($config); | ||
|
||
$this->assertSame([ | ||
'result' => 0, | ||
'errmsg' => 'OK', | ||
'ext' => '', | ||
'sid' => 3310228982, | ||
'fee' => 1 | ||
], $gateway->send(18888888888, $message, $config)); | ||
|
||
$this->expectException(GatewayErrorException::class); | ||
$this->expectExceptionCode(1001); | ||
$this->expectExceptionMessage('sig校验失败'); | ||
|
||
$gateway->send(18888888888, $message, $config); | ||
} | ||
} |