Skip to content

Commit

Permalink
升级百度云短信到V3版本 (overtrue#277)
Browse files Browse the repository at this point in the history
* 精简过滤签名getHeadersToSign的方法,headers确定,指定的keys确定,兼容写法可以全部删除。
精简getCanonicalHeaders方法,无空值,兼容代码删除

Signed-off-by: iwzh <[email protected]>

* 优化readme.md 阿里大于配置参数。

Signed-off-by: iwzh <[email protected]>

* 升级百度云短信V3接口

变更如下,签名参数有变动
请求地址有变动
短信参数名称有变动,新增用户自定义参数(字符串回调时会调用)和通道扩展码(通道扩展码默认不开通,需申请)

Signed-off-by: iwzh <[email protected]>

* fix

* 修复 key 不存在报错
  • Loading branch information
iwzh authored Nov 24, 2020
1 parent 7f880cb commit 3501e03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/Gateways/BaiduGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
/**
* Class BaiduGateway.
*
* @see https://cloud.baidu.com/doc/SMS/API.html
* @see https://cloud.baidu.com/doc/SMS/index.html
*/
class BaiduGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_HOST = 'sms.bj.baidubce.com';
const ENDPOINT_HOST = 'smsv3.bj.baidubce.com';

const ENDPOINT_URI = '/bce/v2/message';
const ENDPOINT_URI = '/api/v3/sendSms';

const BCE_AUTH_VERSION = 'bce-auth-v1';

Expand All @@ -50,22 +50,31 @@ class BaiduGateway extends Gateway
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$params = [
'invokeId' => $config->get('invoke_id'),
'phoneNumber' => $to->getNumber(),
'templateCode' => $message->getTemplate($this),
'signatureId' => $config->get('invoke_id'),
'mobile' => $to->getNumber(),
'template' => $message->getTemplate($this),
'contentVar' => $message->getData($this),
];
if (!empty($params['contentVar']['custom'])) {
//用户自定义参数,格式为字符串,状态回调时会回传该值
$params['custom'] = $params['contentVar']['custom'];
unset($params['contentVar']['custom']);
}
if (!empty($params['contentVar']['userExtId'])) {
//通道自定义扩展码,上行回调时会回传该值,其格式为纯数字串。默认为不开通,请求时无需设置该参数。如需开通请联系客服申请
$params['userExtId'] = $params['contentVar']['userExtId'];
unset($params['contentVar']['userExtId']);
}

$datetime = gmdate('Y-m-d\TH:i:s\Z');

$headers = [
'host' => self::ENDPOINT_HOST,
'content-type' => 'application/json',
'x-bce-date' => $datetime,
'x-bce-content-sha256' => hash('sha256', json_encode($params)),
];
//获得需要签名的数据
$signHeaders = $this->getHeadersToSign($headers, ['host', 'x-bce-content-sha256']);
$signHeaders = $this->getHeadersToSign($headers, ['host', 'x-bce-date']);

$headers['Authorization'] = $this->generateSign($signHeaders, $datetime, $config);

Expand Down
6 changes: 3 additions & 3 deletions tests/Gateways/BaiduGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function testSend()
];
$gateway = \Mockery::mock(BaiduGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
$expected = [
'phoneNumber' => 18888888888,
'templateCode' => 'mock-tpl-id',
'invokeId' => $config['invoke_id'],
'mobile' => 18888888888,
'template' => 'mock-tpl-id',
'signatureId' => $config['invoke_id'],
'contentVar' => ['mock-data-1', 'mock-data-2'],
];
$gateway->shouldReceive('request')->with(
Expand Down

0 comments on commit 3501e03

Please sign in to comment.