nanosanteservice/app/Listeners/NotifyUser.php

59 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Listeners;
use App\Events\ExampleEvent;
use App\Events\InsuranceSubscribed;
use App\Traits\Helper;
use GuzzleHttp\Client;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Throwable;
class NotifyUser
{
use Helper;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param InsuranceSubscribed $event
* @return void
*/
public function handle(InsuranceSubscribed $event)
{
//
$subscription = $event->subscription;
$user = $subscription->user;
try {
$client = new Client([
'base_uri' => config('services.notification_service.base_uri'),
]);
$headers = [
'Authorization' => config('services.notification_service.key'),
];
$body = new \stdClass();
$body->title = trans('messages.insurance_subscription');
$body->message = trans('messages.insurance_subscription_mail', ['name' => $user->lastname, 'subscription_id' => $subscription->insurance_subscription_id,
2021-10-20 13:05:23 +00:00
'bonus_amount' => $this->toMoneyWithNetwork($subscription->total_bonus_amount, $subscription->network_id), 'number_of_beneficiaries' => $subscription->number_of_beneficiaries]);
$body->email = $user->email;
$response = $client->request('POST', '/send-mail', ['json' => $body, 'headers' => $headers]);
} catch (Throwable $t) {
Log::error('-------- User notification not sent-----------');
Log::error($t->getMessage() . '\n' . $t->getTraceAsString());
}
}
}