Files
mykopkb-be/Modules/Investment/Http/Controllers/BillplzInvestmentController.php
T

74 lines
2.3 KiB
PHP

<?php
namespace Modules\Investment\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Investment\Entities\Investment;
use Modules\Investment\Http\Requests\BillplzInvestmentRequest;
use Modules\Investment\Services\BillplzInvestmentService;
use Modules\Investment\Transformers\InvestmentResource;
class BillplzInvestmentController extends Controller
{
use AuthorizesRequests;
public function __construct(
protected BillplzInvestmentService $billplzInvestmentService,
) {}
public function store(BillplzInvestmentRequest $request): JsonResponse
{
$this->authorize('createBillplz', Investment::class);
$validated = $request->validated();
$result = $this->billplzInvestmentService->initiate(
$request->user()->id,
$validated['investment_type_id'],
$validated['amount'],
$validated['remarks'] ?? null,
$request->user(),
);
return response()->json([
'success' => true,
'data' => [
'investment' => new InvestmentResource($result['investment']),
'bill_url' => $result['bill_url'],
],
'message' => 'Bil Billplz berjaya dijana',
], 201);
}
public function callback(Request $request): JsonResponse
{
$investment = $this->billplzInvestmentService->handleCallback($request->all());
return response()->json([
'success' => true,
'data' => [
'investment_id' => $investment->id,
'status' => $investment->status?->value ?? $investment->status,
],
'message' => 'Callback Billplz diterima',
]);
}
public function status(string $id): JsonResponse
{
$this->authorize('view', Investment::class);
$investment = $this->billplzInvestmentService->syncFromGateway($id);
return response()->json([
'success' => true,
'data' => new InvestmentResource(
$investment->loadMissing(['account', 'investmentType', 'payment.billplzTransaction', 'ledgerEntries'])
),
'message' => 'Status pelaburan berjaya dimuatkan',
]);
}
}