api.so-manager-dev.com/app/Http/Requests/Api/SubscriptionChargeRequest.php
Your Name f139a3f608
All checks were successful
Deploy api / deploy (push) Successful in 22s
支払いAPI実装
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 20:02:25 +09:00

70 lines
2.1 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\Http\Requests\Api;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
/**
* 継続課金請求リクエストバリデーション
*/
class SubscriptionChargeRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* バリデーションルール
*/
public function rules(): array
{
return [
'member_id' => ['required', 'string', 'max:20'],
'amount' => ['required', 'integer', 'min:1'],
'execute_date' => ['required', 'string', 'regex:/^\d{8}$/'],
'contract_id' => ['required', 'string', 'max:20'],
'card_seq' => ['nullable', 'integer'],
];
}
/**
* バリデーション失敗時のJSONエラーレスポンス
*/
protected function failedValidation(Validator $validator): void
{
throw new HttpResponseException(response()->json([
'error' => [
'code' => 'INVALID_REQUEST',
'message' => $validator->errors()->first(),
]
], 400));
}
/**
* 追加バリデーションexecute_dateの実在日チェック
*/
public function withValidator($validator): void
{
$validator->after(function ($validator) {
$executeDate = $this->input('execute_date');
if (!$executeDate) {
return;
}
try {
Carbon::createFromFormat('Ymd', $executeDate, 'Asia/Tokyo');
$errors = Carbon::getLastErrors() ?: ['warning_count' => 0, 'error_count' => 0];
if (($errors['warning_count'] ?? 0) > 0 || ($errors['error_count'] ?? 0) > 0) {
$validator->errors()->add('execute_date', 'execute_dateの日付が不正です。');
}
} catch (\Throwable) {
$validator->errors()->add('execute_date', 'execute_dateの日付が不正です。');
}
});
}
}