提交 ae0f7576 编写于 作者: N Nathan Wu 提交者: 安正超

添加了基于aliyun rest gateway (#142)

* add aliyun rest gateway, aliyun gateway is not working for sending sms

* update document

* update document

* aplly patch
上级 d7e8fd6d
......@@ -326,6 +326,18 @@ $easySms->send(13188888888, $message);
],
```
### [阿里云Rest](https://www.aliyun.com/)
短信内容使用 `template` + `data`
```php
'aliyunrest' => [
'app_key' => '',
'app_secret_key' => '',
'sign_name' => '',
],
```
### [云片](https://www.yunpian.com)
短信内容使用 `content`
......
<?php
/*
* This file is part of the overtrue/easy-sms.
*
* (c) overtrue <i@overtrue.me>
*
* 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 AliyunrestGateway.
*/
class AliyunrestGateway extends Gateway
{
use HasHttpRequest;
const ENDPOINT_URL = 'http://gw.api.taobao.com/router/rest';
const ENDPOINT_VERSION = '2.0';
const ENDPOINT_FORMAT = 'json';
const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send';
const ENDPOINT_SIGNATURE_METHOD = 'md5';
const ENDPOINT_PARTNER_ID = 'EasySms';
/**
* @param PhoneNumberInterface $to
* @param MessageInterface $message
* @param Config $config
*
* @return array|void
*/
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$urlParams = [
'app_key' => $config->get('app_key'),
'v' => self::ENDPOINT_VERSION,
'format' => self::ENDPOINT_FORMAT,
'sign_method' => self::ENDPOINT_SIGNATURE_METHOD,
'method' => self::ENDPOINT_METHOD,
'timestamp' => date('Y-m-d H:i:s'),
'partner_id' => self::ENDPOINT_PARTNER_ID,
];
$params = [
'extend' => '',
'sms_type' => 'normal',
'sms_free_sign_name' => $config->get('sign_name'),
'sms_param' => json_encode($message->getData()),
'rec_num' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(),
'sms_template_code' => $message->getTemplate($this),
];
$urlParams['sign'] = $this->generateSign(array_merge($params, $urlParams));
$result = $this->post($this->getEndpointUrl($urlParams), $params);
if (isset($result['error_response']) && $result['error_response']['code'] != 0) {
throw new GatewayErrorException($result['error_response']['msg'], $result['error_response']['code'], $result);
}
return $result;
}
/**
* @param array $params
*
* @return string
*/
protected function getEndpointUrl($params)
{
return self::ENDPOINT_URL.'?'.http_build_query($params);
}
/**
* @param array $params
*
* @return string
*/
protected function generateSign($params)
{
ksort($params);
$stringToBeSigned = $this->config->get('app_secret_key');
foreach ($params as $k => $v) {
if (!is_array($v) && '@' != substr($v, 0, 1)) {
$stringToBeSigned .= "$k$v";
}
}
unset($k, $v);
$stringToBeSigned .= $this->config->get('app_secret_key');
return strtoupper(md5($stringToBeSigned));
}
}
<?php
/*
* This file is part of the overtrue/easy-sms.
*
* (c) overtrue <i@overtrue.me>
*
* 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\AliyunrestGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;
/**
* Class AliyunrestGatewayTest.
*/
class AliyunrestGatewayTest extends TestCase
{
public function testSend()
{
$urlParams = [
'app_key' => 'mock-app-key',
'v' => '2.0',
'format' => 'json',
'sign_method' => 'md5',
'method' => 'alibaba.aliqin.fc.sms.num.send',
'timestamp' => date('Y-m-d H:i:s'),
'partner_id' => 'EasySms',
];
$config = [
'app_key' => 'mock-app-key',
'app_secret_key' => 'mock-app-secret',
'sign_name' => 'mock-app-sign-name',
'template_code' => 'mock-template-code',
];
$expected = [
'extend' => '',
'sms_type' => 'normal',
'sms_free_sign_name' => 'mock-app-sign-name',
'sms_param' => json_encode(['code' => '123456']),
'rec_num' => strval(new PhoneNumber(18888888888)),
'sms_template_code' => 'mock-template-code',
];
$gateway = \Mockery::mock(AliyunrestGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('post')->with(\Mockery::on(function ($url) use ($urlParams) {
$url = implode('&', array_filter(explode('&', $url), function ($s) {
return 'sign=' != substr($s, 0, 5);
}));
return $url == 'http://gw.api.taobao.com/router/rest?'.http_build_query($urlParams);
}), \Mockery::on(function ($params) use ($expected) {
return $params == $expected;
}))->andReturn([
'alibaba_aliqin_fc_sms_num_send_response' => [
'result' => [
'err_code' => '0', 'msg' => 'mock-result', 'success' => true,
],
], ], [
'error_response' => [
'code' => 15,
'msg' => 'mock-err-msg',
], ])->twice();
$message = new Message([
'template' => 'mock-template-code',
'data' => ['code' => '123456'],
]);
$config = new Config($config);
$this->assertSame([
'alibaba_aliqin_fc_sms_num_send_response' => [
'result' => [
'err_code' => '0', 'msg' => 'mock-result', 'success' => true,
],
], ], $gateway->send(new PhoneNumber(18888888888), $message, $config));
$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(15);
$this->expectExceptionMessage('mock-err-msg');
$gateway->send(new PhoneNumber(18888888888), $message, $config);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册