From 36b0f75835dd38871aff67fe067be8041313439e Mon Sep 17 00:00:00 2001 From: Camilo Rodriguez Date: Tue, 26 Mar 2024 23:19:59 -0600 Subject: [PATCH] Added exeption if meta fail --- src/Http/Controllers/TemplateController.php | 6 +++- src/Services/MessageService.php | 4 +-- tests/Feature/Template/SendTemplateTest.php | 32 +++++++++++++++------ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Http/Controllers/TemplateController.php b/src/Http/Controllers/TemplateController.php index 73883c1..0e4cea4 100644 --- a/src/Http/Controllers/TemplateController.php +++ b/src/Http/Controllers/TemplateController.php @@ -43,7 +43,11 @@ public function sendTemplate(SendTemplateRequest $request) $template = Template::find($request->template); $wabaPhone = WabaPhone::find($request->waba_phone); - $message = resolve(SendTemplate::class)->send($wabaPhone, $template, $request->to, $request->vars ?? []); + try { + $message = resolve(SendTemplate::class)->send($wabaPhone, $template, $request->to, $request->vars ?? []); + } catch (\Exception $e) { + return response()->json($e->getMessage(), 500); + } return response()->json($message); } diff --git a/src/Services/MessageService.php b/src/Services/MessageService.php index cc3d408..c2430b4 100644 --- a/src/Services/MessageService.php +++ b/src/Services/MessageService.php @@ -16,7 +16,7 @@ public function sendMessage(string $phoneId, $to, array $message): array 'messaging_product' => 'whatsapp', 'recipient_type' => 'individual', 'to' => $to, - ], $message)); + ], $message))->throw(); return $response->json(); } @@ -36,7 +36,7 @@ public function sendTemplate(WabaPhone $phone, string $to, Template $template): ], 'components' => $template->vars, ], - ]); + ])->throw(); return $response->json(); } diff --git a/tests/Feature/Template/SendTemplateTest.php b/tests/Feature/Template/SendTemplateTest.php index 60eac80..3078271 100644 --- a/tests/Feature/Template/SendTemplateTest.php +++ b/tests/Feature/Template/SendTemplateTest.php @@ -41,11 +41,6 @@ public function test_send_text_template_with_missing_vars() $template = Template::factory()->create([ 'content' => json_encode(['BODY' => ['text' => 'Hello {{1}}']]), ]); - $messageId = 'wamid.'.$this->faker()->numberBetween(111, 450); - - Http::fake([ - "*/$wabaPhone->phone_id/messages" => Http::response(FakeMessageCreteResponse::getFakeMessageCreateResponse($messageId)), - ]); $this->post(route('message.template.send'), [ 'waba_phone' => $wabaPhone->id, @@ -124,18 +119,37 @@ public function test_send_template_header_image_missing_var() $template = Template::factory()->create([ 'content' => json_encode(['body' => ['text' => 'Hello this is a test'], 'header' => ['format' => 'image']]), ]); + + $this->post(route('message.template.send'), [ + 'waba_phone' => $wabaPhone->id, + 'to' => '2213428198', + 'template' => $template->id, + ]) + ->assertSessionHasErrors(['vars.header.parameters.0.image.link']) + ->assertStatus(302); + } + + public function test_send_template_fail_to_send() + { + $wabaPhone = WabaPhone::factory()->create(); + $template = Template::factory()->create(); $messageId = 'wamid.'.$this->faker()->numberBetween(111, 450); Http::fake([ - "*/$wabaPhone->phone_id/messages" => Http::response(FakeMessageCreteResponse::getFakeMessageCreateResponse($messageId)), + "*/$wabaPhone->phone_id/messages" => Http::response([ + 'error' => [ + 'message' => 'Invalid OAuth access token - Cannot parse access token', + 'type' => 'OAuthException', + 'code' => 190, + 'fbtrace_id' => 'ACbE05M7Rh6qXPVN_9_NNdG', + ], + ], 500), ]); $this->post(route('message.template.send'), [ 'waba_phone' => $wabaPhone->id, 'to' => '2213428198', 'template' => $template->id, - ]) - ->assertSessionHasErrors(['vars.header.parameters.0.image.link']) - ->assertStatus(302); + ])->assertStatus(500); } }