PaymentController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\PaymentFailedException;
  4. use App\Exceptions\PaymentException;
  5. use App\Http\Requests\PayServicePackageRequest;
  6. use App\Http\Requests\PaymentRequest;
  7. use App\Http\Resources\PaymentResource;
  8. use App\Models\ServicePackage;
  9. use App\Services\PaymentService;
  10. use Illuminate\Http\JsonResponse;
  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: $item->status->message(),
  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 payServicePackage(PayServicePackageRequest $request, int $servicePackageId): JsonResponse
  48. {
  49. $validated = $request->validated();
  50. try {
  51. $item = $this->service->payServicePackage(
  52. servicePackageId: $servicePackageId,
  53. paymentMethod: data_get($validated, 'payment_method'),
  54. clientPaymentMethodId: data_get($validated, 'client_payment_method_id'),
  55. options: [
  56. 'phone' => data_get($validated, 'phone'),
  57. 'card_id' => data_get($validated, 'card_id'),
  58. ],
  59. );
  60. } catch (PaymentFailedException) {
  61. return $this->errorResponse(message: __('messages.payment_not_confirmed'), code: 422);
  62. } catch (PaymentException) {
  63. return $this->errorResponse(message: __('messages.payment_error'), code: 422);
  64. }
  65. return $this->successResponse(
  66. payload: new PaymentResource($item),
  67. message: $item->status->message(),
  68. code: 201,
  69. );
  70. }
  71. //
  72. public function getServicePackagePix(ServicePackage $servicePackage): JsonResponse
  73. {
  74. if ($servicePackage->client?->user_id !== Auth::id()) {
  75. abort(403);
  76. }
  77. $item = $this->service->getOrCreateServicePackagePixPayment($servicePackage);
  78. return $this->successResponse(payload: new PaymentResource($item));
  79. }
  80. public function platformFees(): JsonResponse
  81. {
  82. return $this->successResponse(payload: $this->service->platformFees());
  83. }
  84. }