Skip to content

Commit

Permalink
处理大小写导致网关查找失败 (#284)
Browse files Browse the repository at this point in the history
* 新增摩杜云支持

* 添加测试用例

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

* 处理大小写导致网关查找失败
  • Loading branch information
Fenguoz authored Jan 20, 2021
1 parent 966fb79 commit 85040b8
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/Gateways/ModuyunGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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 ModuyunGateway.
*
* @see https://www.moduyun.com/doc/index.html#10002
*/
class ModuyunGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'https://live.moduyun.com/sms/v2/sendsinglesms';

/**
* @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)
{
$urlParams = [
'accesskey' => $config->get('accesskey'),
'random' => rand(100000, 999999),
];

$params = [
'tel' => [
'mobile' => $to->getNumber(),
'nationcode' => $to->getIDDCode() ?: '86',
],
'signId' => $config->get('signId', ''),
'templateId' => $message->getTemplate($this),
'time' => time(),
'type' => $config->get('type', 0),
'params' => array_values($message->getData($this)),
'ext' => '',
'extend' => '',
];
$params['sig'] = $this->generateSign($params, $urlParams['random']);

$result = $this->request('post', $this->getEndpointUrl($urlParams), [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
'json' => $params,
]);

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

return $result;
}

/**
* @param array $params
*
* @return string
*/
protected function getEndpointUrl($params)
{
return self::ENDPOINT_URL . '?' . http_build_query($params);
}

/**
* Generate Sign.
*
* @param array $params
* @param string $random
*
* @return string
*/
protected function generateSign($params, $random)
{
return hash('sha256', sprintf(
'secretkey=%s&random=%d&time=%d&mobile=%s',
$this->config->get('secretkey'),
$random,
$params['time'],
$params['tel']['mobile']
));
}
}
71 changes: 71 additions & 0 deletions tests/Gateways/ModuyunGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\ModuyunGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;

class ModuyunGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'accesskey' => 'mock-accesskey',
'secretkey' => 'mock-secretkey',
];
$gateway = \Mockery::mock(ModuyunGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();

$gateway->shouldReceive('request')
->andReturn(
[
'result' => 0,
'errmsg' => 'OK',
'ext' => '',
'sid' => "mock-sid",
'surplus' => 4,
'balance' => 0,
],
[
'result' => 1001,
'errmsg' => 'accesskey not exist.',
]
)->twice();

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

$config = new Config($config);

$this->assertSame([
'result' => 0,
'errmsg' => 'OK',
'ext' => '',
'sid' => "mock-sid",
'surplus' => 4,
'balance' => 0,
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(1001);
$this->expectExceptionMessage('accesskey not exist.');

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

0 comments on commit 85040b8

Please sign in to comment.