Skip to content

Commit

Permalink
新增融合云(助通)支持 (overtrue#293)
Browse files Browse the repository at this point in the history
* 新增摩杜云支持

* 添加测试用例

* 删除文件(修改大小写文件git不识别)

* 处理大小写导致网关查找失败

* 新增融合云(助通)支持

Co-authored-by: 安正超 <[email protected]>
  • Loading branch information
Fenguoz and overtrue authored Mar 16, 2021
1 parent daa0b43 commit a625230
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- [短信宝](http://www.smsbao.com/)
- [Tiniyo](https://tiniyo.com/)
- [摩杜云](https://www.moduyun.com/)
- [融合云(助通)](https://www.ztinfo.cn/products/sms)

## 环境需求

Expand Down Expand Up @@ -757,6 +758,29 @@ $easySms->send(18888888888, [

```

### [融合云(助通)](https://www.ztinfo.cn/products/sms)

短信使用 `template` + `data`

```php
'rongheyun' => [
'username' => '', //必填 用户名
'password' => '', //必填 密码
'signature'=> '', //必填 已报备的签名
],
```

```php
$easySms->send(18888888888, [
'template' => '31874', //短信模板
'data' => [
'valid_code' => '888888', //模板参数,对应模板的{valid_code}
//...
],
]);

```

## PHP 扩展包开发

> 想知道如何从零开始构建 PHP 扩展包?
Expand Down
72 changes: 72 additions & 0 deletions src/Gateways/RongheyunGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 RongheyunGateway.
*
* @see https://doc.zthysms.com/web/#/1?page_id=13
*/
class RongheyunGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'https://api.mix2.zthysms.com/v2/sendSmsTp';

/**
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param \Overtrue\EasySms\Support\Config $config
*
* @return array
*
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
*/
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$tKey = time();
$password = md5(md5($config->get('password')) . $tKey);
$params = [
'username' => $config->get('username', ''),
'password' => $password,
'tKey' => $tKey,
'signature' => $config->get('signature', ''),
'tpId' => $message->getTemplate($this),
'ext' => '',
'extend' => '',
'records' => [
'mobile' => $to->getNumber(),
'tpContent' => $message->getData($this),
],
];

$result = $this->request('post', self::ENDPOINT_URL, [
'headers' => [
'Content-Type' => 'application/json; charset="UTF-8"',
],
'body' => $params,
]);

$result = is_string($result) ? json_decode($result, true) : $result;
if (200 != $result['code']) {
throw new GatewayErrorException($result['msg'], $result['code'], $result);
}

return $result;
}
}
73 changes: 73 additions & 0 deletions tests/Gateways/RongheyunGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\RongheyunGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;

class RongheyunGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'username' => 'mock-username',
'password' => 'mock-password',
'signature' => 'mock-signature',
];
$gateway = \Mockery::mock(RongheyunGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();

$gateway->shouldReceive('request')
->andReturn(
[
'code' => 200,
'msg' => 'success',
'tpId' => '31874',
'msgId' => '161553136878837480961',
'invalidList' => []
],
[
'code' => 4025,
'msg' => 'template records null',
'tpId' => '31874',
'msgId' => '161553131051357039361',
'invalidList' => null
]
)->twice();

$message = new Message([
'template' => 'mock-template',
'data' => [
'valid_code' => 888888,
],
]);

$config = new Config($config);

$this->assertSame([
'code' => 200,
'msg' => 'success',
'tpId' => '31874',
'msgId' => '161553136878837480961',
'invalidList' => []
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(4025);
$this->expectExceptionMessage('template records null');

$gateway->send(new PhoneNumber(18888888888), $message, $config);
}
}

0 comments on commit a625230

Please sign in to comment.