Skip to content

Commit

Permalink
凯信通短信发送支持 (overtrue#222)
Browse files Browse the repository at this point in the history
* 凯信通短信发送支持

1. 增加 gateway
2. 修改 readme

* 样式修复

* Update KingttoGateway.php

* Update KingttoGateway.php
  • Loading branch information
molinchenxi authored and overtrue committed Sep 26, 2019
1 parent b3c16de commit bf0a537
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- [华为云](https://www.huaweicloud.com/product/msgsms.html)
- [网易云信](https://yunxin.163.com/sms)
- [云之讯](https://www.ucpaas.com/index.html)
- [凯信通](http://www.kingtto.cn/)

## 环境需求

Expand Down Expand Up @@ -514,7 +515,7 @@ $easySms->send(13188888888, $message);

### [腾讯云 SMS](https://cloud.tencent.com/product/sms)

短信内容使用 `content`
短信内容使用 `content`

```php
'qcloud' => [
Expand Down Expand Up @@ -622,6 +623,24 @@ $easySms->send(18888888888, [
]);
```

### [凯信通](http://www.kingtto.cn/)

短信内容使用 `content`

```php
'kingtto' => [
'userid' => '',
'account' => '',
'password' => '',
],
```

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

## PHP 扩展包开发

> 想知道如何从零开始构建 PHP 扩展包?
Expand Down
61 changes: 61 additions & 0 deletions src/Gateways/KingttoGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 KingttoGateWay.
*
* @see http://www.kingtto.cn/
*/
class KingttoGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'http://101.201.41.194:9999/sms.aspx';

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 = [
'action' => self::ENDPOINT_METHOD,
'userid' => $config->get('userid'),
'account' => $config->get('account'),
'password' => $config->get('password'),
'mobile' => $to->getNumber(),
'content' => $message->getContent(),
];

$result = $this->post(self::ENDPOINT_URL, $params);

if ('Success' != $result['returnstatus']) {
throw new GatewayErrorException($result['message'], $result['remainpoint'], $result);
}

return $result;
}
}
75 changes: 75 additions & 0 deletions tests/Gateways/KingttoGatewayTest.php
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\Tests\Gateways;

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

class KingttoGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'userid' => 'mock-id',
'account' => 'mock-account',
'password' => 'mock-password',
];

$gateway = \Mockery::mock(KingttoGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods();

$params = [
'action' => KingttoGateway::ENDPOINT_METHOD,
'userid' => 'mock-id',
'account' => 'mock-account',
'password' => 'mock-password',
'mobile' => '18888888888',
'content' => '【molin】This is a test message.',
];

$gateway->shouldReceive('post')->with(KingttoGateway::ENDPOINT_URL, $params)
->andReturn([
'returnstatus' => 'Success',
'message' => 'ok',
'remainpoint' => '56832',
'taskID' => '106470408',
'successCounts' => '1',
], [
'returnstatus' => 'Faild',
'message' => 'mock-message',
'remainpoint' => '0',
'taskID' => '0',
'successCounts' => '0',
])->times(2);

$this->assertSame([
'returnstatus' => 'Success',
'message' => 'ok',
'remainpoint' => '56832',
'taskID' => '106470408',
'successCounts' => '1',
], $gateway->send(new PhoneNumber('18888888888'), new Message([
'content' => '【molin】This is a test message.',
]), new Config($config)));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('mock-message');

$gateway->send(new PhoneNumber('18888888888'), new Message([
'content' => '【molin】This is a test message.',
]), new Config($config));
}
}

0 comments on commit bf0a537

Please sign in to comment.