PaymentController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\PaymentRequest;
  4. use App\Http\Resources\PaymentResource;
  5. use App\Models\Schedule;
  6. use App\Services\PaymentService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. class PaymentController extends Controller
  11. {
  12. public function __construct(
  13. protected PaymentService $service,
  14. ) {}
  15. public function index(): JsonResponse
  16. {
  17. $items = $this->service->getAll();
  18. return $this->successResponse(payload: PaymentResource::collection($items));
  19. }
  20. public function store(PaymentRequest $request): JsonResponse
  21. {
  22. $item = $this->service->create($request->validated());
  23. return $this->successResponse(
  24. payload: new PaymentResource($item),
  25. message: $this->paymentMessage($item->status),
  26. code: 201,
  27. );
  28. }
  29. public function paySchedule(Request $request, Schedule $schedule): JsonResponse
  30. {
  31. $validated = $request->validate([
  32. 'payment_method' => ['required', 'in:credit_card,pix'],
  33. 'client_payment_method_id' => ['nullable', 'integer', 'exists:client_payment_methods,id'],
  34. 'card_id' => ['nullable', 'string', 'max:255'],
  35. 'phone' => ['nullable', 'string', 'max:20'],
  36. ]);
  37. if ($schedule->client?->user_id !== Auth::id()) {
  38. abort(403);
  39. }
  40. $item = $this->service->payAcceptedSchedule(
  41. schedule: $schedule,
  42. paymentMethod: $validated['payment_method'],
  43. clientPaymentMethodId: $validated['client_payment_method_id'] ?? null,
  44. options: [
  45. 'phone' => $validated['phone'] ?? null,
  46. 'card_id' => $validated['card_id'] ?? null,
  47. ],
  48. );
  49. return $this->successResponse(
  50. payload: new PaymentResource($item),
  51. message: $this->paymentMessage($item->status),
  52. code: 201,
  53. );
  54. }
  55. public function show(int $id): JsonResponse
  56. {
  57. $item = $this->service->findById($id);
  58. return $this->successResponse(payload: new PaymentResource($item));
  59. }
  60. public function update(PaymentRequest $request, int $id): JsonResponse
  61. {
  62. $item = $this->service->update($id, $request->validated());
  63. return $this->successResponse(payload: new PaymentResource($item), message: __('messages.updated'));
  64. }
  65. public function destroy(int $id): JsonResponse
  66. {
  67. $this->service->delete($id);
  68. return $this->successResponse(message: __('messages.deleted'), code: 204);
  69. }
  70. private function paymentMessage(string $status): string
  71. {
  72. return match ($status) {
  73. 'paid' => __('messages.payment_confirmed'),
  74. 'authorized', 'processing', 'pending' => __('messages.payment_pending_confirmation'),
  75. 'failed' => __('messages.payment_not_confirmed'),
  76. default => __('messages.created'),
  77. };
  78. }
  79. }