Files
2025-07-24 16:18:27 +07:00

132 lines
4.2 KiB
PHP

<?php
namespace App\Jobs\Mail\Customer;
use App\Http\Facade\SignRSA;
use App\Http\Services\Telegram;
use App\Models\Setting;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class CustomerPayment implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected int $template_id;
protected int $code;
protected string $email;
protected int $customer;
protected Telegram $telegram;
/**
* Create a new job instance.
*/
public function __construct($template = 0, $code = 0, $email = '', $customer = 0)
{
$this->template_id = $template;
$this->code = $code;
$this->email = $email;
$this->customer = $customer;
$this->telegram = new Telegram();
}
/**
* Execute the job.
*/
public function handle()
{
// Log job start
Log::channel('Custom')->info("Starting CustomerPayment job for email: {$this->email}");
// Notify via Telegram that job is starting
$this->telegram->SendJobRunInfo('CustomerPayment', 'Started', [
'email' => $this->email,
'customer_id' => $this->customer
]);
$hash = SignRSA::EncryptFunc(base64_encode($this->code));
$setting = Setting::where('key', 'setting')->first();
$parsed = json_decode($setting->value);
$data = [
'company_name' => $parsed->company,
'company_logo' => $parsed->logo,
];
try {
// Send email
$email = new \App\Mail\Customer\CustomerPayment($this->template_id, $data);
Mail::to($this->email)->send($email);
// Log email in database
\App\Models\Mail::create([
'template_id' => 3,
'subject' => $email->envelope()->subject,
'to' => $this->email,
'customer_id' => $this->customer,
'message' => $email->render()
]);
// Send Telegram notification about new user registration
$this->telegram->SendNewUserRegistration($this->email, $this->code, $verificationLink);
// Log job completion
Log::channel('Custom')->info("CustomerPayment job completed successfully for email: {$this->email}");
// Notify via Telegram that job completed successfully
$this->telegram->SendJobRunInfo('CustomerPayment', 'Completed', [
'email' => $this->email,
'status' => 'success'
]);
} catch (\Exception $exception) {
// Log error
Log::channel('Custom')->error("CustomerPayment job failed for email: {$this->email}. Error: {$exception->getMessage()}");
// Notify via Telegram that job failed
$this->telegram->SendJobRunInfo('CustomerPayment', 'Failed', [
'email' => $this->email,
'error' => $exception->getMessage()
]);
throw new \Exception($exception->getMessage());
}
}
/**
* Handle job failure.
*
* @param \Throwable $exception
* @return void
*/
public function failed(\Throwable $exception)
{
$timestamp = Carbon::now()->format('Y-m-d H:i:s');
Log::channel('Custom')->info("=================================");
Log::channel('Custom')->info("Thông báo lỗi lúc: " . $timestamp);
Log::channel('Custom')->info("Notification Send Email Register for Customer: {$this->email}");
Log::channel('Custom')->info("========");
Log::channel('Custom')->info($exception->getMessage());
Log::channel('Custom')->info("=================================");
// Send error message to Telegram
$errorData = [
'timestamp' => $timestamp,
'email' => $this->email,
'customer_id' => $this->customer,
'error' => $exception->getMessage()
];
$this->telegram->SendErrorMessage(json_encode($errorData));
}
}