PagarmeTransferService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services\Pagarme;
  3. use App\Data\Pagarme\Request\PagarmeTransferRequestData;
  4. use App\Models\Provider;
  5. use App\Models\ProviderWithdrawal;
  6. use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
  7. use Carbon\Carbon;
  8. class PagarmeTransferService
  9. {
  10. use SendsPagarmeRequests;
  11. public function createTransfer(int $amountInCents, string $recipientId, string $idempotencyKey): array
  12. {
  13. if ($this->shouldMockTransferRequest()) {
  14. return $this->mockTransferResponse(
  15. amountInCents: $amountInCents,
  16. recipientId: $recipientId,
  17. idempotencyKey: $idempotencyKey,
  18. );
  19. }
  20. return $this->pagarmeRequest(
  21. method: 'POST',
  22. path: '/transfers',
  23. payload: new PagarmeTransferRequestData(
  24. amount: $amountInCents,
  25. recipientId: $recipientId,
  26. ),
  27. idempotencyKey: $idempotencyKey,
  28. errorMessage: 'Erro ao criar transferencia (saque) no Pagar.me.',
  29. );
  30. }
  31. public function getTransfer(string $transferId): array
  32. {
  33. if ($this->shouldMockTransferRequest()) {
  34. return $this->mockTransferLookupResponse($transferId);
  35. }
  36. return $this->pagarmeRequest(
  37. method: 'GET',
  38. path: "/transfers/{$transferId}",
  39. payload: [],
  40. idempotencyKey: "get-transfer-{$transferId}",
  41. errorMessage: 'Erro ao consultar transferencia no Pagar.me.',
  42. );
  43. }
  44. //
  45. private function shouldMockTransferRequest(): bool
  46. {
  47. return app()->environment('local', 'development');
  48. }
  49. private function mockTransferResponse(int $amountInCents, string $recipientId, string $idempotencyKey): array
  50. {
  51. $provider = Provider::query()
  52. ->where('recipient_id', $recipientId)
  53. ->first();
  54. $createdAt = Carbon::now();
  55. return [
  56. 'id' => $this->mockTransferId($idempotencyKey),
  57. 'amount' => $amountInCents,
  58. 'type' => 'credito_em_conta',
  59. 'status' => 'pending_transfer',
  60. 'fee' => 0,
  61. 'funding_date' => null,
  62. 'funding_estimated_date' => $createdAt->copy()->addWeekday()->toISOString(),
  63. 'transaction_id' => null,
  64. 'bank_account' => $provider?->recipient_default_bank_account,
  65. 'date_created' => $createdAt->toISOString(),
  66. 'created_at' => $createdAt->toISOString(),
  67. 'metadata' => [
  68. 'mocked' => true,
  69. 'environment' => app()->environment(),
  70. 'recipient_id' => $recipientId,
  71. 'idempotency_key' => $idempotencyKey,
  72. 'provider_id' => $provider?->id,
  73. ],
  74. 'bank_response' => null,
  75. ];
  76. }
  77. private function mockTransferLookupResponse(string $transferId): array
  78. {
  79. $withdrawal = ProviderWithdrawal::query()
  80. ->where('transfer_id', $transferId)
  81. ->first();
  82. if ($withdrawal?->gateway_payload) {
  83. return $withdrawal->gateway_payload;
  84. }
  85. $createdAt = Carbon::now();
  86. return [
  87. 'id' => $transferId,
  88. 'amount' => $withdrawal ? (int) round((float) $withdrawal->gross_amount * 100) : 0,
  89. 'type' => $withdrawal?->type ?? 'credito_em_conta',
  90. 'status' => $withdrawal?->status?->value ?? 'pending_transfer',
  91. 'fee' => $withdrawal ? (int) round((float) $withdrawal->gateway_fee_amount * 100) : 0,
  92. 'funding_date' => $withdrawal?->completed_at?->toISOString(),
  93. 'funding_estimated_date' => $createdAt->copy()->addWeekday()->toISOString(),
  94. 'transaction_id' => null,
  95. 'bank_account' => $withdrawal?->bank_account,
  96. 'date_created' => $withdrawal?->created_at?->toISOString() ?? $createdAt->toISOString(),
  97. 'created_at' => $withdrawal?->created_at?->toISOString() ?? $createdAt->toISOString(),
  98. 'metadata' => $withdrawal?->metadata ?? [
  99. 'mocked' => true,
  100. 'environment' => app()->environment(),
  101. ],
  102. 'bank_response' => $withdrawal?->bank_response,
  103. ];
  104. }
  105. private function mockTransferId(string $idempotencyKey): string
  106. {
  107. return (string) abs(crc32($idempotencyKey));
  108. }
  109. }