154 lines
4.9 KiB
PHP
154 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\BlastWasap;
|
|
use App\Customer;
|
|
use App\Services\Whatsapp\WhatsappCloudClient;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class WhatsappPembiayaanOnlinePaid extends Job
|
|
{
|
|
public $kodcaw;
|
|
public $cawName;
|
|
public $nokp;
|
|
public $norujukan;
|
|
public $pinjaman;
|
|
public $namaBank;
|
|
public $noakaun;
|
|
public $tarikhBayaran;
|
|
public $noRujukanBayaran;
|
|
|
|
public function __construct(array $data)
|
|
{
|
|
$this->kodcaw = $data['kodcaw'] ?? null;
|
|
$this->cawName = $data['caw_name'] ?? null;
|
|
$this->nokp = $data['nokp'] ?? null;
|
|
$this->norujukan = $data['norujukan'] ?? null;
|
|
$this->pinjaman = $data['pinjaman'] ?? null;
|
|
$this->namaBank = $data['nama_bank'] ?? null;
|
|
$this->noakaun = $data['noakaun'] ?? null;
|
|
$this->tarikhBayaran = $data['tarikh_bayaran'] ?? null;
|
|
$this->noRujukanBayaran = $data['no_rujukan_bayaran'] ?? null;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
$client = new WhatsappCloudClient();
|
|
$now = Carbon::now();
|
|
|
|
if (!$client->isConfigured()) {
|
|
Log::warning('WhatsApp Cloud skipped: token or phone_number_id missing', [
|
|
'norujukan' => $this->norujukan,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$customer = Customer::on('mysql7')
|
|
->where(function ($q) {
|
|
$q->where('kpbaru', $this->nokp)->orWhere('kplama', $this->nokp);
|
|
})
|
|
->first();
|
|
|
|
$storedPhone = $customer ? ($customer->telefon ?: $customer->telefon2) : null;
|
|
$to = $client->normalizeMsisdn($storedPhone);
|
|
|
|
if (!config('whatsapp.whatsapp_cloud_enabled')) {
|
|
$admin = $client->normalizeMsisdn(env('ADMIN_PHONE_NUMBER'));
|
|
if (!$admin) {
|
|
Log::warning('WhatsApp Cloud skipped: ADMIN_PHONE_NUMBER required when WHATSAPP_ENABLED is false', [
|
|
'norujukan' => $this->norujukan,
|
|
]);
|
|
$this->logBlast($now, $storedPhone, 'skipped: ADMIN_PHONE_NUMBER empty');
|
|
return;
|
|
}
|
|
$to = $admin;
|
|
}
|
|
|
|
if (!$to) {
|
|
Log::warning('WhatsApp Cloud skipped: no phone', [
|
|
'nokp' => $this->nokp,
|
|
'norujukan' => $this->norujukan,
|
|
]);
|
|
$this->logBlast($now, $storedPhone, 'skipped: no phone');
|
|
return;
|
|
}
|
|
|
|
$template = config('whatsapp.template', 'hello_world');
|
|
$lang = config('whatsapp.template_lang', 'en_US');
|
|
$bodyParams = $this->isHelloWorld($template) ? [] : $this->bodyParams();
|
|
|
|
try {
|
|
$response = $client->sendTemplate($to, $template, $lang, $bodyParams);
|
|
$this->logBlast($now, $to, $response->body());
|
|
} catch (\Throwable $e) {
|
|
Log::error('WhatsappPembiayaanOnlinePaid failed: ' . $e->getMessage(), [
|
|
'norujukan' => $this->norujukan,
|
|
]);
|
|
$this->logBlast($now, $to, 'exception: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
protected function isHelloWorld($template)
|
|
{
|
|
return strtolower((string) $template) === 'hello_world';
|
|
}
|
|
|
|
/**
|
|
* Matches Meta template pembiayaan_online_paid (ms):
|
|
* AR-RAHN KoPKB: Pembiayaan bagi No. SAG *{{1}}* sebanyak *RM{{2}}*
|
|
* telah berjaya dikreditkan ke akaun *{{3}}* berakhir *{{4}}* pada *{{5}}*.
|
|
* Rujukan: *{{6}}*
|
|
*
|
|
* {{1}} SAG, {{2}} amaun, {{3}} nama bank/akaun, {{4}} 4 digit akhir, {{5}} tarikh, {{6}} rujukan bayaran
|
|
*
|
|
* @return array<int,string>
|
|
*/
|
|
protected function bodyParams()
|
|
{
|
|
$bank = trim((string) $this->namaBank);
|
|
$tail = $this->accountTail($this->noakaun);
|
|
|
|
$tarikh = $this->tarikhBayaran
|
|
? Carbon::parse($this->tarikhBayaran)->format('d/m/Y')
|
|
: Carbon::now()->format('d/m/Y');
|
|
|
|
return [
|
|
$this->norujukan ?: '-',
|
|
number_format((float) $this->pinjaman, 2, '.', ''),
|
|
$bank !== '' ? $bank : '-',
|
|
$tail !== '' ? $tail : '-',
|
|
$tarikh,
|
|
$this->noRujukanBayaran ?: '-',
|
|
];
|
|
}
|
|
|
|
protected function accountTail($noakaun)
|
|
{
|
|
$digits = preg_replace('/\D+/', '', (string) $noakaun);
|
|
if ($digits === '') {
|
|
return '';
|
|
}
|
|
|
|
return substr($digits, -4);
|
|
}
|
|
|
|
protected function logBlast($now, $telefon, $status)
|
|
{
|
|
try {
|
|
$log = new BlastWasap();
|
|
$log->kodcaw = $this->kodcaw;
|
|
$log->nokp = $this->nokp;
|
|
$log->notelefon = $telefon;
|
|
$log->norujukan = $this->norujukan;
|
|
$log->status = is_string($status) ? $status : json_encode($status);
|
|
$log->created_at = $now;
|
|
$log->updated_at = $now;
|
|
$log->save();
|
|
} catch (\Throwable $e) {
|
|
Log::error('log_blast_wasap save failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|