api.so-manager-dev.com/app/Http/Requests/Api/RemLinkRequest.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

107 lines
4.3 KiB
PHP

<?php
namespace App\Http\Requests\Api;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
/**
* REM支払案内リンク生成リクエストバリデーション
*/
class RemLinkRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* バリデーションルール
*/
public function rules(): array
{
return [
'SyunoRecvNum' => ['required', 'string', 'max:20', 'regex:/^[a-zA-Z0-9]+$/'],
'SyunoTel' => ['required', 'string', 'max:13', 'regex:/^[0-9\-]+$/'],
'SyunoNameKanji' => ['required', 'string', 'max:40'],
'SyunoNameKana' => ['nullable', 'string', 'max:40'],
'SyunoPayLimit' => ['required', 'string', 'regex:/^\d{12}$/'],
'SyunoPayAmount' => ['required', 'integer', 'min:1'],
'SyunoFree1' => ['required', 'string', 'max:32'],
'SyunoFree9' => ['required', 'string', 'max:60'],
'SyunoFree19' => ['required', 'string', 'max:42'],
'SyunoMemberNum' => ['nullable', 'string', 'max:20'],
'SyunoReserveNum' => ['nullable', 'string', 'max:20'],
'SyunoFree2' => ['nullable', 'string', 'max:32'],
'SyunoFree3' => ['nullable', 'string', 'max:32'],
'SyunoFree4' => ['nullable', 'string', 'max:32'],
'SyunoFree5' => ['nullable', 'string', 'max:32'],
'SyunoFree6' => ['nullable', 'string', 'max:32'],
'SyunoFree7' => ['nullable', 'string', 'max:32'],
'SyunoFree8' => ['nullable', 'string', 'max:32'],
'SyunoFree10' => ['nullable', 'string', 'max:60'],
'SyunoFree11' => ['nullable', 'string', 'max:60'],
'SyunoFree12' => ['nullable', 'string', 'max:60'],
'SyunoFree13' => ['nullable', 'string', 'max:60'],
'SyunoFree14' => ['nullable', 'string', 'max:60'],
'SyunoFree15' => ['nullable', 'string', 'max:60'],
'SyunoFree16' => ['nullable', 'string', 'max:60'],
'SyunoFree17' => ['nullable', 'string', 'max:60'],
'SyunoFree18' => ['nullable', 'string', 'max:60'],
'SyunoFree20' => ['nullable', 'string', 'max:12'],
'SyunoFree21' => ['nullable', 'string', 'max:11'],
'SyunoFree22' => ['nullable', 'string', 'max:128'],
'SyunoFree23' => ['nullable', 'string', 'max:40'],
];
}
/**
* バリデーション失敗時のJSONエラーレスポンス
*/
protected function failedValidation(Validator $validator): void
{
throw new HttpResponseException(response()->json([
'error' => [
'code' => 'INVALID_REQUEST',
'message' => $validator->errors()->first(),
]
], 400));
}
/**
* 追加バリデーション(支払期限の範囲チェック)
*/
public function withValidator($validator): void
{
$validator->after(function ($validator) {
$payLimit = $this->input('SyunoPayLimit');
if (!$payLimit) {
return;
}
try {
$dt = Carbon::createFromFormat('YmdHi', $payLimit, '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('SyunoPayLimit', 'SyunoPayLimitの日時が不正です。');
return;
}
} catch (\Throwable) {
$validator->errors()->add('SyunoPayLimit', 'SyunoPayLimitの日時が不正です。');
return;
}
$now = Carbon::now('Asia/Tokyo');
$max = $now->copy()->addDays(365);
if ($dt->lessThanOrEqualTo($now)) {
$validator->errors()->add('SyunoPayLimit', 'SyunoPayLimitは現在時刻より未来を指定してください。');
} elseif ($dt->greaterThan($max)) {
$validator->errors()->add('SyunoPayLimit', 'SyunoPayLimitは365日以内で指定してください。');
}
});
}
}