PaymentController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\PaymentFailedException;
  4. use App\Exceptions\PaymentException;
  5. use App\Exceptions\PaymentMissingDataException;
  6. use App\Http\Requests\PayServicePackageRequest;
  7. use App\Http\Requests\PaymentRequest;
  8. use App\Http\Resources\PaymentResource;
  9. use App\Models\ServicePackage;
  10. use App\Services\PaymentService;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Support\Facades\Auth;
  13. class PaymentController extends Controller
  14. {
  15. public function __construct(
  16. protected PaymentService $service,
  17. ) {}
  18. public function index(): JsonResponse
  19. {
  20. $items = $this->service->getAll();
  21. return $this->successResponse(payload: PaymentResource::collection($items));
  22. }
  23. public function store(PaymentRequest $request): JsonResponse
  24. {
  25. $item = $this->service->create($request->validated());
  26. return $this->successResponse(
  27. payload: new PaymentResource($item),
  28. message: $item->status->message(),
  29. code: 201,
  30. );
  31. }
  32. public function show(int $id): JsonResponse
  33. {
  34. $item = $this->service->findById($id);
  35. return $this->successResponse(payload: new PaymentResource($item));
  36. }
  37. public function update(PaymentRequest $request, int $id): JsonResponse
  38. {
  39. $item = $this->service->update($id, $request->validated());
  40. return $this->successResponse(payload: new PaymentResource($item), message: __('messages.updated'));
  41. }
  42. public function destroy(int $id): JsonResponse
  43. {
  44. $this->service->delete($id);
  45. return $this->successResponse(message: __('messages.deleted'), code: 204);
  46. }
  47. //
  48. public function payServicePackage(PayServicePackageRequest $request, int $servicePackageId): JsonResponse
  49. {
  50. $validated = $request->validated();
  51. try {
  52. $item = $this->service->payServicePackage(
  53. servicePackageId: $servicePackageId,
  54. paymentMethod: data_get($validated, 'payment_method'),
  55. clientPaymentMethodId: data_get($validated, 'client_payment_method_id'),
  56. options: [
  57. 'phone' => data_get($validated, 'phone'),
  58. 'card_id' => data_get($validated, 'card_id'),
  59. ],
  60. );
  61. } catch (PaymentMissingDataException $e) {
  62. return response()->json([
  63. 'message' => $e->getMessage(),
  64. 'error' => 'missing_payment_data',
  65. ], 422);
  66. } catch (PaymentFailedException) {
  67. return $this->errorResponse(message: __('messages.payment_not_confirmed'), code: 422);
  68. } catch (PaymentException) {
  69. return $this->errorResponse(message: __('messages.payment_error'), code: 422);
  70. }
  71. return $this->successResponse(
  72. payload: new PaymentResource($item),
  73. message: $item->status->message(),
  74. code: 201,
  75. );
  76. }
  77. //
  78. public function getServicePackagePix(ServicePackage $servicePackage): JsonResponse
  79. {
  80. if ($servicePackage->client?->user_id !== Auth::id()) {
  81. abort(403);
  82. }
  83. try {
  84. $item = $this->service->getOrCreateServicePackagePixPayment($servicePackage);
  85. } catch (PaymentMissingDataException $e) {
  86. return response()->json([
  87. 'message' => $e->getMessage(),
  88. 'error' => 'missing_payment_data',
  89. ], 422);
  90. }
  91. return $this->successResponse(payload: new PaymentResource($item));
  92. }
  93. public function platformFees(): JsonResponse
  94. {
  95. return $this->successResponse(payload: $this->service->platformFees());
  96. }
  97. }