ClientPaymentMethodRequest.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. class ClientPaymentMethodRequest extends FormRequest
  6. {
  7. public function authorize(): bool
  8. {
  9. return true;
  10. }
  11. public function rules(): array
  12. {
  13. $rules = [
  14. 'client_id' => ['sometimes', 'integer', 'exists:clients,id'],
  15. 'token' => ['sometimes', 'string', 'max:255'],
  16. 'gateway_card_id' => ['sometimes', 'nullable', 'string', 'max:255'],
  17. 'card_number' => ['sometimes', 'nullable', 'string', 'min:13', 'max:19'],
  18. 'holder_name' => ['sometimes', 'string', 'max:255'],
  19. 'expiration' => ['sometimes', 'string', 'regex:/^(0[1-9]|1[0-2])\/\d{4}$/'],
  20. 'cvv' => ['sometimes', 'nullable', 'string', 'min:3', 'max:4'],
  21. 'card_name' => ['nullable', 'string', 'max:255'],
  22. 'brand' => ['nullable', 'string', 'max:50'],
  23. 'last_four_digits' => ['sometimes', 'string', 'size:4'],
  24. 'is_active' => ['nullable', 'boolean'],
  25. ];
  26. if ($this->isMethod('POST')) {
  27. $rules['client_id'] = ['required', 'integer', 'exists:clients,id'];
  28. $rules['token'] = ['nullable', 'string', 'max:255', Rule::requiredIf(! $this->filled('gateway_card_id'))];
  29. $rules['gateway_card_id'] = ['nullable', 'string', 'max:255', Rule::requiredIf(! $this->filled('token'))];
  30. $rules['card_number'] = ['sometimes', 'nullable', 'string', 'min:13', 'max:19'];
  31. $rules['holder_name'] = ['required', 'string', 'max:255'];
  32. $rules['expiration'] = ['required', 'string', 'regex:/^(0[1-9]|1[0-2])\/\d{4}$/'];
  33. $rules['cvv'] = ['sometimes', 'nullable', 'string', 'min:3', 'max:4'];
  34. $rules['last_four_digits'] = ['required', 'string', 'size:4'];
  35. }
  36. return $rules;
  37. }
  38. public function messages(): array
  39. {
  40. return [
  41. 'client_id.required' => 'O cliente é obrigatório.',
  42. 'client_id.exists' => 'Cliente não encontrado.',
  43. 'token.required' => 'O token do cartão é obrigatório.',
  44. 'card_number.required' => 'O número do cartão é obrigatório.',
  45. 'card_number.min' => 'O número do cartão deve ter no mínimo 13 dígitos.',
  46. 'card_number.max' => 'O número do cartão deve ter no máximo 19 dígitos.',
  47. 'holder_name.required' => 'O nome do titular é obrigatório.',
  48. 'expiration.required' => 'A data de validade é obrigatória.',
  49. 'expiration.regex' => 'A data de validade deve estar no formato MM/YYYY.',
  50. 'cvv.required' => 'O CVV é obrigatório.',
  51. 'cvv.min' => 'O CVV deve ter no mínimo 3 dígitos.',
  52. 'cvv.max' => 'O CVV deve ter no máximo 4 dígitos.',
  53. 'last_four_digits.required' => 'Os últimos 4 dígitos são obrigatórios.',
  54. 'last_four_digits.size' => 'Devem ser exatamente 4 dígitos.',
  55. ];
  56. }
  57. protected function prepareForValidation(): void
  58. {
  59. if ($this->has('card_number')) {
  60. $this->merge([
  61. 'card_number' => str_replace(' ', '', $this->card_number),
  62. ]);
  63. }
  64. if ($this->has('cvv')) {
  65. $this->merge([
  66. 'cvv' => str_replace(' ', '', $this->cvv),
  67. ]);
  68. }
  69. }
  70. }