提交 25e8ec71 编写于 作者: T timjuly 提交者: 安正超

修复在填写了大陆国际区号的情况下短信发送失败的Bug (#165)

上级 078abd52
......@@ -41,7 +41,7 @@ class SubmailGateway extends Gateway
*/
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$endpoint = $this->buildEndpoint($to->getIDDCode() ? 'internationalsms/xsend' : 'message/xsend');
$endpoint = $this->buildEndpoint($this->inChineseMainland($to) ? 'message/xsend' : 'internationalsms/xsend');
$data = $message->getData($this);
......@@ -71,4 +71,18 @@ class SubmailGateway extends Gateway
{
return sprintf(self::ENDPOINT_TEMPLATE, $function, self::ENDPOINT_FORMAT);
}
/**
* Check if the phone number belongs to chinese mainland.
*
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
*
* @return bool
*/
protected function inChineseMainland($to)
{
$code = $to->getIDDCode();
return empty($code) || 86 === $code;
}
}
......@@ -95,4 +95,70 @@ class SubmailGatewayTest extends TestCase
'sms_credits' => 14197,
], $gateway->send(new PhoneNumber(18188888888), $message, $config));
}
public function testEndpointChina()
{
$config = [
'app_id' => 'mock-app-id',
'app_key' => 'mock-app-key',
'project' => 'mock-project',
];
$gateway = \Mockery::mock(SubmailGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('post')->with('https://api.mysubmail.com/message/xsend.json', [
'appid' => 'mock-app-id',
'signature' => 'mock-app-key',
'project' => 'mock-project',
'to' => new PhoneNumber(18188888888, 86),
'vars' => json_encode(['code' => '123456', 'time' => '15']),
])->andReturn([
'status' => 'success',
'send_id' => '093c0a7df143c087d6cba9cdf0cf3738',
'fee' => 1,
'sms_credits' => 14197,
]);
$message = new Message(['data' => ['code' => '123456', 'time' => '15']]);
$config = new Config($config);
$this->assertSame([
'status' => 'success',
'send_id' => '093c0a7df143c087d6cba9cdf0cf3738',
'fee' => 1,
'sms_credits' => 14197,
], $gateway->send(new PhoneNumber(18188888888, 86), $message, $config));
}
public function testEndpointInternational()
{
$config = [
'app_id' => 'mock-app-id',
'app_key' => 'mock-app-key',
'project' => 'mock-project',
];
$gateway = \Mockery::mock(SubmailGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('post')->with('https://api.mysubmail.com/internationalsms/xsend.json', [
'appid' => 'mock-app-id',
'signature' => 'mock-app-key',
'project' => 'mock-project',
'to' => new PhoneNumber(18188888888, 1),
'vars' => json_encode(['code' => '123456', 'time' => '15']),
])->andReturn([
'status' => 'success',
'send_id' => '093c0a7df143c087d6cba9cdf0cf3738',
'fee' => 1,
'sms_credits' => 14197,
]);
$message = new Message(['data' => ['code' => '123456', 'time' => '15']]);
$config = new Config($config);
$this->assertSame([
'status' => 'success',
'send_id' => '093c0a7df143c087d6cba9cdf0cf3738',
'fee' => 1,
'sms_credits' => 14197,
], $gateway->send(new PhoneNumber(18188888888, 1), $message, $config));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册