PaymentController.php 5.5 KB

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