Files
lelong/app/Console/Commands/CheckBillplzPayments.php
ismailmasseran e29a5891c8 Bugfix/list clickup issue (#5)
Reviewed-on: #5
Co-authored-by: ISMAIL MASSERAN <ismail@koppkb.com>
Co-committed-by: ISMAIL MASSERAN <ismail@koppkb.com>
2026-10-19 13:30:36 +08:00

113 lines
3.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Console\Commands;
use App\Http\Controllers\ApiController;
use App\Services\BillplzService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
class CheckBillplzPayments extends Command
{
protected $signature = 'billplz:semak
{--date= : Date in YYYY-MM-DD format}
{--preview : Preview pending resits only}';
protected $description = 'Check Billplz payment status and update database';
protected $billplzService;
public function __construct(BillplzService $billplzService)
{
parent::__construct();
$this->billplzService = $billplzService;
}
public function handle()
{
$date = $this->option('date') ?? now()->toDateString();
$preview = $this->option('preview');
// Validate date
try {
$dateObj = Carbon::createFromFormat('Y-m-d', $date);
} catch (\Exception $e) {
$this->error('Invalid date format. Use YYYY-MM-DD.');
return 1;
}
$this->info("Retrying Billplz check for date: {$dateObj->toDateString()}");
$date = $dateObj->toDateString();
$pendingResits = DB::table('resit as r')
->join('payment as p', 'p.resit_id', '=', 'r.id')
->join(DB::raw('(
SELECT user_id, total_payment, MIN(id) AS min_id
FROM payment
GROUP BY user_id, total_payment
) as unique_payment'), 'p.id', '=', 'unique_payment.min_id')
->whereNull('r.status')
->where('p.status', 0)
->whereNotNull('r.billplz_id')
->whereDate('r.created_at', $date)
->where('r.billplz_id', 'NOT LIKE', '%CDM%')
->select('r.*', 'p.id as payment_id', 'p.total_payment')
->distinct()
->get();
if ($pendingResits->isEmpty()) {
$this->info('Tiada Resit belum dibayar ditemui.');
return 0;
}
if ($preview) {
$this->info('Preview mode aktif. Senarai Resit:');
foreach ($pendingResits as $resit) {
$this->line("Resit ID: {$resit->id} | Billplz ID: {$resit->billplz_id} | Jumlah: RM{$resit->total_payment}");
}
return 0;
}
foreach ($pendingResits as $resit) {
$this->info("Semak Resit ID: {$resit->id}");
$billData = $this->billplzService->testGetBill($resit->billplz_id);
if (isset($billData['error'])) {
$this->error("❌ Gagal semak Billplz untuk Resit {$resit->id}: {$billData['details']}");
continue;
}
if (isset($billData['paid']) && $billData['paid'] === true) {
// Simulate Request object
$data = [
'billplz' => [
'id' => $resit->billplz_id,
'paid' => 'true',
'paid_at' => $billData['paid_at'] ?? now()->toDateTimeString(),
],
];
// dd($data);
$apiController = App::make(ApiController::class);
$apiController->processPaidResit($data);
// dd($fakeRequest);
$this->info("✅ Resit ID {$resit->id} handled via processPaidResit.");
} else {
$this->info("ℹ️ Resit ID {$resit->id} masih belum dibayar.");
}
}
$this->info('Semakan semula Billplz selesai.');
}
}