Skip to content

Commit

Permalink
Added exeption if meta fail
Browse files Browse the repository at this point in the history
  • Loading branch information
hellsythe committed Mar 27, 2024
1 parent 57d48ea commit 36b0f75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/Http/Controllers/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Services/MessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -36,7 +36,7 @@ public function sendTemplate(WabaPhone $phone, string $to, Template $template):
],
'components' => $template->vars,
],
]);
])->throw();

return $response->json();
}
Expand Down
32 changes: 23 additions & 9 deletions tests/Feature/Template/SendTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 36b0f75

Please sign in to comment.