Skip to content

Commit

Permalink
修复在填写了大陆国际区号的情况下短信发送失败的Bug (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
timjuly authored and overtrue committed Feb 27, 2019
1 parent 078abd5 commit 25e8ec7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Gateways/SubmailGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -71,4 +71,18 @@ protected function buildEndpoint($function)
{
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;
}
}
66 changes: 66 additions & 0 deletions tests/Gateways/SubmailGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,70 @@ public function testProject()
'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));
}
}

0 comments on commit 25e8ec7

Please sign in to comment.