From 25e8ec71696b72a3c9d500a36292ba1b5fee06c2 Mon Sep 17 00:00:00 2001 From: timjuly Date: Wed, 27 Feb 2019 21:36:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8=E5=A1=AB=E5=86=99?= =?UTF-8?q?=E4=BA=86=E5=A4=A7=E9=99=86=E5=9B=BD=E9=99=85=E5=8C=BA=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E7=9F=AD=E4=BF=A1=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=A4=B1=E8=B4=A5=E7=9A=84Bug=20(#165)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Gateways/SubmailGateway.php | 16 ++++++- tests/Gateways/SubmailGatewayTest.php | 66 +++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Gateways/SubmailGateway.php b/src/Gateways/SubmailGateway.php index 41059e6..7d4678f 100644 --- a/src/Gateways/SubmailGateway.php +++ b/src/Gateways/SubmailGateway.php @@ -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 @@ 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; + } } diff --git a/tests/Gateways/SubmailGatewayTest.php b/tests/Gateways/SubmailGatewayTest.php index a12ef03..692392a 100644 --- a/tests/Gateways/SubmailGatewayTest.php +++ b/tests/Gateways/SubmailGatewayTest.php @@ -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)); + } }