PaymentController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if ($item->status === 'failed') {
  50. return response()->json([
  51. 'payload' => new PaymentResource($item),
  52. 'message' => $item->failure_message ?: $this->paymentMessage($item->status),
  53. ], 422);
  54. }
  55. return $this->successResponse(
  56. payload: new PaymentResource($item),
  57. message: $this->paymentMessage($item->status),
  58. code: 201,
  59. );
  60. }
  61. public function show(int $id): JsonResponse
  62. {
  63. $item = $this->service->findById($id);
  64. return $this->successResponse(payload: new PaymentResource($item));
  65. }
  66. public function update(PaymentRequest $request, int $id): JsonResponse
  67. {
  68. $item = $this->service->update($id, $request->validated());
  69. return $this->successResponse(payload: new PaymentResource($item), message: __('messages.updated'));
  70. }
  71. public function destroy(int $id): JsonResponse
  72. {
  73. $this->service->delete($id);
  74. return $this->successResponse(message: __('messages.deleted'), code: 204);
  75. }
  76. private function paymentMessage(string $status): string
  77. {
  78. return match ($status) {
  79. 'paid' => __('messages.payment_confirmed'),
  80. 'authorized', 'processing', 'pending' => __('messages.payment_pending_confirmation'),
  81. 'failed' => __('messages.payment_not_confirmed'),
  82. default => __('messages.created'),
  83. };
  84. }
  85. }