PaymentController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\PaymentStatusEnum;
  4. use App\Http\Requests\PaymentRequest;
  5. use App\Http\Resources\PaymentResource;
  6. use App\Models\Schedule;
  7. use App\Services\PaymentService;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. class PaymentController extends Controller
  12. {
  13. public function __construct(
  14. protected PaymentService $service,
  15. ) {}
  16. public function index(): JsonResponse
  17. {
  18. $items = $this->service->getAll();
  19. return $this->successResponse(payload: PaymentResource::collection($items));
  20. }
  21. public function store(PaymentRequest $request): JsonResponse
  22. {
  23. $item = $this->service->create($request->validated());
  24. return $this->successResponse(
  25. payload: new PaymentResource($item),
  26. message: $this->paymentMessage($item->status->value),
  27. code: 201,
  28. );
  29. }
  30. public function show(int $id): JsonResponse
  31. {
  32. $item = $this->service->findById($id);
  33. return $this->successResponse(payload: new PaymentResource($item));
  34. }
  35. public function update(PaymentRequest $request, int $id): JsonResponse
  36. {
  37. $item = $this->service->update($id, $request->validated());
  38. return $this->successResponse(payload: new PaymentResource($item), message: __('messages.updated'));
  39. }
  40. public function destroy(int $id): JsonResponse
  41. {
  42. $this->service->delete($id);
  43. return $this->successResponse(message: __('messages.deleted'), code: 204);
  44. }
  45. //
  46. public function payCart(Request $request): JsonResponse
  47. {
  48. $validated = $request->validate([
  49. 'cart_id' => ['required', 'integer', 'exists:carts,id'],
  50. 'payment_method' => ['required', 'in:credit_card,pix'],
  51. 'client_payment_method_id' => ['nullable', 'integer', 'exists:client_payment_methods,id'],
  52. 'card_id' => ['nullable', 'string', 'max:255'],
  53. 'phone' => ['nullable', 'string', 'max:20'],
  54. ]);
  55. $items = $this->service->payCart($validated, Auth::id());
  56. return $this->successResponse(
  57. payload: PaymentResource::collection($items),
  58. message: __('messages.created'),
  59. code: 201,
  60. );
  61. }
  62. public function paySchedule(Request $request, Schedule $schedule): JsonResponse
  63. {
  64. $validated = $request->validate([
  65. 'payment_method' => ['required', 'in:credit_card,pix'],
  66. 'client_payment_method_id' => ['nullable', 'integer', 'exists:client_payment_methods,id'],
  67. 'card_id' => ['nullable', 'string', 'max:255'],
  68. 'phone' => ['nullable', 'string', 'max:20'],
  69. ]);
  70. if ($schedule->client?->user_id !== Auth::id()) {
  71. abort(403);
  72. }
  73. $item = $this->service->payAcceptedSchedule(
  74. schedule: $schedule,
  75. paymentMethod: $validated['payment_method'],
  76. clientPaymentMethodId: $validated['client_payment_method_id'] ?? null,
  77. options: [
  78. 'phone' => $validated['phone'] ?? null,
  79. 'card_id' => $validated['card_id'] ?? null,
  80. ],
  81. );
  82. if ($item->status === PaymentStatusEnum::FAILED) {
  83. return response()->json([
  84. 'payload' => new PaymentResource($item),
  85. 'message' => $item->failure_message ?: $this->paymentMessage($item->status->value),
  86. ], 422);
  87. }
  88. return $this->successResponse(
  89. payload: new PaymentResource($item),
  90. message: $this->paymentMessage($item->status->value),
  91. code: 201,
  92. );
  93. }
  94. //
  95. public function getSchedulePix(Schedule $schedule): JsonResponse
  96. {
  97. if ($schedule->client?->user_id !== Auth::id()) {
  98. abort(403);
  99. }
  100. $item = $this->service->getOrCreatePixPayment($schedule);
  101. return $this->successResponse(payload: new PaymentResource($item));
  102. }
  103. public function platformFees(): JsonResponse
  104. {
  105. return $this->successResponse(payload: $this->service->platformFees());
  106. }
  107. //
  108. private function paymentMessage(string $status): string
  109. {
  110. return match ($status) {
  111. 'paid' => __('messages.payment_confirmed'),
  112. 'authorized', 'processing', 'pending' => __('messages.payment_pending_confirmation'),
  113. 'failed' => __('messages.payment_not_confirmed'),
  114. default => __('messages.created'),
  115. };
  116. }
  117. }