tambah function semakan billplz

This commit is contained in:
BTMS2
2025-05-06 10:12:20 +08:00
parent d95767c5f9
commit 791ca8dd84
8 changed files with 580 additions and 1 deletions
@@ -0,0 +1,112 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Resit;
use App\Services\BillplzService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Models\Payment;
use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
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.');
}
}