feat: [barobill] 구독/과금/사용량 관리 API 이관

- BarobillBillingService 생성 (구독 CRUD, 월별 과금 배치, 사용량 과금, 집계)
- BarobillUsageService 생성 (회원별 사용량 조회, 통계, 정책 기반 과금 계산)
- BarobillBillingController 생성 (9개 엔드포인트)
  - 구독 관리, 월별 현황, 과금 배치, 연간 추이, 정책 관리
- BarobillUsageController 생성 (4개 엔드포인트)
  - 사용량 목록/통계, 회원별 상세, 과금 정책 정보
- finance.php 라우트 등록 (barobill/billing/*, barobill/usage/*)
This commit is contained in:
김보곤
2026-03-22 19:59:41 +09:00
parent a1f3de782f
commit ac94602cf1
5 changed files with 759 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
<?php
namespace App\Http\Controllers\Api\V1;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\Barobill\BarobillBillingService;
use Carbon\Carbon;
use Illuminate\Http\Request;
class BarobillBillingController extends Controller
{
public function __construct(
private BarobillBillingService $billingService,
) {}
/**
* 구독 목록
*/
public function subscriptions(Request $request)
{
$data = $request->validate([
'member_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
return [
'subscriptions' => $this->billingService->getSubscriptions($data['member_id'] ?? null),
];
}, __('message.fetched'));
}
/**
* 구독 등록/수정
*/
public function saveSubscription(Request $request)
{
$data = $request->validate([
'member_id' => 'required|integer|exists:barobill_members,id',
'service_type' => 'required|in:bank_account,card,hometax',
'monthly_fee' => 'nullable|integer|min:0',
'started_at' => 'nullable|date',
'ended_at' => 'nullable|date|after_or_equal:started_at',
'is_active' => 'nullable|boolean',
'memo' => 'nullable|string|max:500',
]);
return ApiResponse::handle(function () use ($data) {
return $this->billingService->saveSubscription(
$data['member_id'],
$data['service_type'],
$data
);
}, __('message.created'));
}
/**
* 구독 해지
*/
public function cancelSubscription(int $id)
{
return ApiResponse::handle(function () use ($id) {
$this->billingService->cancelSubscription($id);
return ['cancelled' => true];
}, __('message.deleted'));
}
/**
* 월별 과금 현황 목록
*/
public function billingList(Request $request)
{
$data = $request->validate([
'billing_month' => 'nullable|string|date_format:Y-m',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$billingMonth = $data['billing_month'] ?? Carbon::now()->format('Y-m');
$tenantId = $data['tenant_id'] ?? null;
return [
'list' => $this->billingService->getMonthlyBillingList($billingMonth, $tenantId),
'total' => $this->billingService->getMonthlyTotal($billingMonth, $tenantId),
'billing_month' => $billingMonth,
];
}, __('message.fetched'));
}
/**
* 월별 과금 통계
*/
public function billingStats(Request $request)
{
$data = $request->validate([
'billing_month' => 'nullable|string|date_format:Y-m',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$billingMonth = $data['billing_month'] ?? Carbon::now()->format('Y-m');
return $this->billingService->getMonthlyTotal($billingMonth, $data['tenant_id'] ?? null);
}, __('message.fetched'));
}
/**
* 월별 과금 배치 처리
*/
public function processBilling(Request $request)
{
$data = $request->validate([
'billing_month' => 'nullable|string|date_format:Y-m',
]);
return ApiResponse::handle(function () use ($data) {
return $this->billingService->processMonthlyBilling($data['billing_month'] ?? null);
}, __('message.created'));
}
/**
* 연간 과금 추이
*/
public function yearlyTrend(Request $request)
{
$data = $request->validate([
'year' => 'nullable|integer|min:2020|max:2030',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$year = $data['year'] ?? Carbon::now()->year;
return [
'trend' => $this->billingService->getYearlyTrend($year, $data['tenant_id'] ?? null),
'year' => $year,
];
}, __('message.fetched'));
}
/**
* 과금 정책 목록
*/
public function pricingPolicies()
{
return ApiResponse::handle(function () {
return ['policies' => $this->billingService->getPricingPolicies()];
}, __('message.fetched'));
}
/**
* 과금 정책 수정
*/
public function updatePricingPolicy(Request $request, int $id)
{
$data = $request->validate([
'name' => 'nullable|string|max:100',
'description' => 'nullable|string|max:500',
'free_quota' => 'nullable|integer|min:0',
'free_quota_unit' => 'nullable|string|max:10',
'additional_unit' => 'nullable|integer|min:1',
'additional_unit_label' => 'nullable|string|max:10',
'additional_price' => 'nullable|integer|min:0',
'is_active' => 'nullable|boolean',
]);
return ApiResponse::handle(function () use ($id, $data) {
return $this->billingService->updatePricingPolicy($id, $data);
}, __('message.updated'));
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers\Api\V1;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Barobill\BarobillMember;
use App\Services\Barobill\BarobillUsageService;
use Carbon\Carbon;
use Illuminate\Http\Request;
class BarobillUsageController extends Controller
{
public function __construct(
private BarobillUsageService $usageService,
) {}
/**
* 사용량 목록
*/
public function index(Request $request)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
$tenantId = $data['tenant_id'] ?? null;
$usageList = $this->usageService->getUsageList($startDate, $endDate, $tenantId);
return [
'data' => $usageList,
'stats' => $this->usageService->aggregateStats($usageList),
'meta' => [
'start_date' => $startDate,
'end_date' => $endDate,
],
];
}, __('message.fetched'));
}
/**
* 사용량 통계
*/
public function stats(Request $request)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
$usageList = $this->usageService->getUsageList($startDate, $endDate, $data['tenant_id'] ?? null);
return $this->usageService->aggregateStats($usageList);
}, __('message.fetched'));
}
/**
* 회원별 사용량 상세
*/
public function show(Request $request, int $memberId)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
]);
return ApiResponse::handle(function () use ($memberId, $data) {
$member = BarobillMember::withoutGlobalScopes()->findOrFail($memberId);
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
return $this->usageService->getMemberUsage($member, $startDate, $endDate);
}, __('message.fetched'));
}
/**
* 과금 정책 정보
*/
public function priceInfo()
{
return ApiResponse::handle(function () {
return ['prices' => BarobillUsageService::getPriceInfo()];
}, __('message.fetched'));
}
}