From 95b099b7643c0a5e4cb657a67c5b69028b8af90e Mon Sep 17 00:00:00 2001 From: Sudhir Mitharwal Date: Tue, 29 Sep 2020 15:31:39 +0530 Subject: [PATCH] fix(sms): fix content type for sms sending apis --- src/Contracts/Options.php | 7 +++++++ src/Support/Request.php | 5 +++-- src/Support/Service.php | 11 +++++++++++ tests/OTP/OTPServiceTest.php | 11 ++++------- tests/SMS/SMSServiceTest.php | 6 +++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Contracts/Options.php b/src/Contracts/Options.php index 864f7d9..4448607 100644 --- a/src/Contracts/Options.php +++ b/src/Contracts/Options.php @@ -31,4 +31,11 @@ public function message($message = ''); * @return $this; */ public function mergeWith($options = null); + + /** + * Set the sender + * @param string|null sender id + * @return $this + */ + public function from($sender_id = null); } diff --git a/src/Support/Request.php b/src/Support/Request.php index 43565f7..660e5e6 100644 --- a/src/Support/Request.php +++ b/src/Support/Request.php @@ -93,9 +93,10 @@ public function handle() $method = strtolower($this->method); try { $resp = $client->{$method}($this->url, [ - "form_params" => $payload, + \GuzzleHttp\RequestOptions::JSON => $payload, "headers" => [ - 'authkey' => $payload['authkey'] + 'authkey' => $payload['authkey'], + 'content-type' => 'application/json' ] ]); return new Response($resp); diff --git a/src/Support/Service.php b/src/Support/Service.php index a76c532..6ad5a09 100644 --- a/src/Support/Service.php +++ b/src/Support/Service.php @@ -47,6 +47,17 @@ public function to($mobile = null) return $this; } + /** + * Set the sender + * @param int|null $sender_id + * @return $this + */ + public function from($sender_id = null) + { + $this->getOptions()->from($sender_id); + return $this; + } + /** * Set the content of message * @param string|null $message diff --git a/tests/OTP/OTPServiceTest.php b/tests/OTP/OTPServiceTest.php index 6db2fa5..50ab044 100644 --- a/tests/OTP/OTPServiceTest.php +++ b/tests/OTP/OTPServiceTest.php @@ -49,9 +49,9 @@ public function test_otp_send() $phone_number = 919999999999; $response = (new Client($this->config, $this->createMockHttpClient())) ->otp() + ->from("SMSIND") ->to($phone_number) ->send(); - $this->assertInstanceOf(CraftsysResponse::class, $response); // make sure there was exacly on request $this->assertCount(1, $this->container); @@ -60,8 +60,7 @@ public function test_otp_send() // check the method $this->assertEquals("POST", $transaction['request']->getMethod()); // check the request data - $data = []; - parse_str($transaction['request']->getBody()->getContents(), $data); + $data = (array) json_decode($transaction['request']->getBody()->getContents()); $this->assertArrayHasKey('mobile', $data); $this->assertEquals($phone_number, $data['mobile']); $this->assertArrayHasKey('authkey', $data); @@ -85,8 +84,7 @@ public function test_verify_otp() // check the method $this->assertEquals("POST", $transaction['request']->getMethod()); // check the request data - $data = []; - parse_str($transaction['request']->getBody()->getContents(), $data); + $data = (array) json_decode($transaction['request']->getBody()->getContents()); $this->assertArrayHasKey('mobile', $data); $this->assertEquals($phone_number, $data['mobile']); $this->assertArrayHasKey('authkey', $data); @@ -111,8 +109,7 @@ public function test_otp_resend() // check the method $this->assertEquals("POST", $transaction['request']->getMethod()); // check the request data - $data = []; - parse_str($transaction['request']->getBody()->getContents(), $data); + $data = (array) json_decode($transaction['request']->getBody()->getContents()); $this->assertArrayHasKey('mobile', $data); $this->assertEquals($phone_number, $data['mobile']); $this->assertArrayHasKey('authkey', $data); diff --git a/tests/SMS/SMSServiceTest.php b/tests/SMS/SMSServiceTest.php index 9dcb1d1..a10b261 100644 --- a/tests/SMS/SMSServiceTest.php +++ b/tests/SMS/SMSServiceTest.php @@ -15,7 +15,7 @@ class SMSServiceTest extends TestCase { protected $config = [ - "key" => "123123123123" + "key" => "12345678901234567890" ]; protected $container = []; @@ -43,14 +43,14 @@ protected function createMockHttpClient(): HttpClient public function test_sms_send() { $phone_number = 919999999999; - $message = "My message"; $response = (new Client($this->config, $this->createMockHttpClient())) ->sms() - ->message($message) + ->from("SENDER_ID") ->flow("flow_id_here") ->to($phone_number) ->send(); + $this->assertInstanceOf(CraftsysResponse::class, $response); // make sure there was exacly on request $this->assertCount(1, $this->container);