| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
- namespace App\Services\Pagarme;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderRequestData;
- use App\Data\Pagarme\Response\PagarmeOrderResponseData\PagarmeOrderResponseData;
- use App\Models\Payment;
- use App\Models\PaymentTransfer;
- use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Str;
- class PagarmePaymentService
- {
- use SendsPagarmeRequests;
- public function createOrderWithCreditCard(
- Payment $payment,
- array $items,
- array $customer,
- array $creditCard,
- array $options = []
- ): array {
- $paymentMethod = [
- 'payment_method' => 'credit_card',
- 'credit_card' => $this->buildCreditCardPayload($creditCard),
- ];
- if (! empty($options['split']) && is_array($options['split'])) {
- $paymentMethod['split'] = $options['split'];
- }
- return $this->createOrder(
- payment: $payment,
- items: $items,
- customer: $customer,
- paymentMethod: $paymentMethod,
- options: $options,
- );
- }
- public function createOrderWithPix(
- Payment $payment,
- array $items,
- array $customer,
- array $pix,
- array $options = []
- ): array {
- $paymentMethod = [
- 'payment_method' => 'pix',
- 'pix' => $this->buildPixPayload($pix),
- ];
- if (! empty($options['split']) && is_array($options['split'])) {
- $paymentMethod['split'] = $options['split'];
- }
- return $this->createOrder(
- payment: $payment,
- items: $items,
- customer: $customer,
- paymentMethod: $paymentMethod,
- options: $options,
- );
- }
- //
- public function createOrder(
- Payment $payment,
- array $items,
- array $customer,
- array $paymentMethod,
- array $options = []
- ): array {
- if (empty($items)) {
- throw new \InvalidArgumentException('items nao pode estar vazio.');
- }
- if (empty($paymentMethod['payment_method'])) {
- throw new \InvalidArgumentException('payment_method e obrigatorio.');
- }
- if (! in_array($paymentMethod['payment_method'], ['credit_card', 'pix'], true)) {
- throw new \InvalidArgumentException('payment_method deve ser credit_card ou pix.');
- }
- $customerIdPayload = $options['customer_id'] ?? null;
- $customerObjectPayload = $this->filterFilledRecursive($customer);
- if (! $this->filled($customerIdPayload) && empty($customerObjectPayload)) {
- throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
- }
- $requestData = new PagarmeOrderRequestData(
- code: $this->ensurePaymentCode($payment),
- items: $this->validateItems($items),
- payments: [$this->filterFilledRecursive($paymentMethod)],
- metadata: array_merge([
- 'payment_id' => (string) $payment->id,
- 'schedule_id' => (string) $payment->schedule_id,
- 'client_id' => (string) $payment->client_id,
- 'provider_id' => (string) $payment->provider_id,
- ], $options['metadata'] ?? []),
- customer: ! empty($customerObjectPayload) ? $customerObjectPayload : null,
- customerId: $this->filled($customerIdPayload) ? (string) $customerIdPayload : null,
- closed: $options['closed'] ?? true,
- channel: $options['channel'] ?? null,
- );
- $order = PagarmeOrderResponseData::fromArray($this->pagarmeRequest(
- method: 'POST',
- path: '/orders',
- payload: $requestData,
- idempotencyKey: $this->idempotencyKey($payment),
- errorMessage: 'Erro ao criar pedido de pagamento no Pagar.me.',
- ));
- if (empty($order->id())) {
- throw new \RuntimeException('Pagar.me order creation returned an empty id.');
- }
- return $order->toArray();
- }
- private function ensurePaymentCode(Payment $payment): string
- {
- if ($this->hasUuidCode($payment->gateway_code, 'payment')) {
- return $payment->gateway_code;
- }
- $code = 'payment-'.(string) Str::uuid();
- $payment->forceFill(['gateway_code' => $code])->save();
- return $code;
- }
- private function hasUuidCode(?string $code, string $prefix): bool
- {
- return is_string($code)
- && preg_match("/^{$prefix}-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i", $code) === 1;
- }
- //
- public function applyGatewayResponseToPayment(Payment $payment, array $orderResponse): Payment
- {
- $charge = $orderResponse['charges'][0] ?? [];
- $transaction = $charge['last_transaction'] ?? [];
- $chargeStatus = $charge['status'] ?? null;
- $transactionStatus = $transaction['status'] ?? null;
- $payment->forceFill([
- 'gateway_provider' => 'pagarme',
- 'gateway_entity_reference' => $charge['id'] ?? $orderResponse['id'] ?? null,
- 'gateway_entity_label' => isset($charge['id']) ? 'charge' : 'order',
- 'gateway_operation_reference' => $transaction['id'] ?? $charge['id'] ?? $orderResponse['id'] ?? null,
- 'gateway_operation_label' => isset($transaction['id']) ? 'transaction' : (isset($charge['id']) ? 'charge' : 'order'),
- 'status' => $this->mapPaymentStatus($chargeStatus, $transactionStatus),
- 'paid_at' => $this->filledArrayValue($charge, 'paid_at'),
- 'authorized_at' => $this->resolveAuthorizedAt($transactionStatus, $transaction),
- 'gateway_payload' => $orderResponse,
- 'failure_code' => $this->extractFailureCode($transaction),
- 'failure_message' => $this->extractFailureMessage($transaction),
- ])->save();
- return $payment->fresh();
- }
- public function buildSplitFromTransfers(Collection $transfers): array
- {
- return $transfers
- ->filter(fn (PaymentTransfer $transfer) => ! empty($transfer->gateway_transfer_target_reference))
- ->map(function (PaymentTransfer $transfer) {
- return [
- 'amount' => $this->toGatewayAmountInCents((float) $transfer->gross_amount),
- 'recipient_id' => $transfer->gateway_transfer_target_reference,
- 'type' => 'flat',
- 'options' => [
- 'charge_processing_fee' => false,
- 'charge_remainder_fee' => false,
- 'liable' => false,
- ],
- ];
- })
- ->values()
- ->all();
- }
- public function toGatewayAmountInCents(float $amount): int
- {
- return (int) round($amount * 100);
- }
- //
- private function idempotencyKey(Payment $payment): string
- {
- return "payment-{$payment->id}-schedule-{$payment->schedule_id}";
- }
- private function buildCreditCardPayload(array $creditCard): array
- {
- $payload = [];
- foreach ([
- 'installments',
- 'statement_descriptor',
- 'operation_type',
- 'recurrence_cycle',
- 'metadata',
- 'extended_limit_enabled',
- 'extended_limit_code',
- 'merchant_category_code',
- 'authentication',
- 'auto_recovery',
- 'payload',
- 'payment_type',
- 'funding_source',
- 'initiated_type',
- 'recurrence_model',
- 'channel',
- 'payment_origin',
- ] as $field) {
- if (array_key_exists($field, $creditCard) && $this->filled($creditCard[$field])) {
- $payload[$field] = $creditCard[$field];
- }
- }
- $allowedCardOptions = ['card', 'card_id', 'card_token', 'network_token'];
- $provided = array_values(array_filter(
- $allowedCardOptions,
- static fn (string $field) => ! empty($creditCard[$field])
- ));
- if (count($provided) !== 1) {
- throw new \InvalidArgumentException('Informe exatamente uma opcao entre card, card_id, card_token ou network_token.');
- }
- $selected = $provided[0];
- $payload[$selected] = $creditCard[$selected];
- return $payload;
- }
- private function buildPixPayload(array $pix): array
- {
- if (! $this->filled($pix['expires_in'] ?? null) && ! $this->filled($pix['expires_at'] ?? null)) {
- throw new \InvalidArgumentException('pix.expires_in ou pix.expires_at e obrigatorio.');
- }
- $payload = [];
- foreach (['expires_in', 'expires_at', 'additional_information'] as $field) {
- if (array_key_exists($field, $pix) && $this->filled($pix[$field])) {
- $payload[$field] = $pix[$field];
- }
- }
- return $payload;
- }
- private function extractFailureCode(array $transaction): ?string
- {
- return $this->filledArrayValue($transaction['gateway_response'] ?? [], 'code');
- }
- private function extractFailureMessage(array $transaction): ?string
- {
- $acquirerMessage = $this->filledArrayValue($transaction, 'acquirer_message');
- if ($acquirerMessage) {
- return $acquirerMessage;
- }
- $gatewayErrors = $transaction['gateway_response']['errors'] ?? [];
- if (! is_array($gatewayErrors) || empty($gatewayErrors)) {
- return null;
- }
- $message = collect($gatewayErrors)
- ->pluck('message')
- ->filter()
- ->implode('; ') ?: null;
- return $this->translateGatewayMessage($message);
- }
- private function filled(mixed $value): bool
- {
- return $value !== null && $value !== '' && $value !== [];
- }
- private function filledArrayValue(array $data, string $field): ?string
- {
- if (! array_key_exists($field, $data) || ! $this->filled($data[$field])) {
- return null;
- }
- return (string) $data[$field];
- }
- private function filterFilledRecursive(array $data): array
- {
- $filtered = [];
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $value = $this->filterFilledRecursive($value);
- }
- if ($this->filled($value)) {
- $filtered[$key] = $value;
- }
- }
- return $filtered;
- }
- private function translateGatewayMessage(?string $message): ?string
- {
- if (! $message) {
- return null;
- }
- if (str_contains($message, 'Sem ambiente configurado')) {
- return 'Pix não esta habilitado ou configurado neste ambiente do Pagar.me.';
- }
- return $message;
- }
- private function mapPaymentStatus(?string $chargeStatus, ?string $transactionStatus): string
- {
- $status = strtolower((string) ($transactionStatus ?: $chargeStatus));
- return match ($status) {
- 'captured', 'paid', 'overpaid' => 'paid',
- 'authorized_pending_capture', 'waiting_capture' => 'authorized',
- 'pending', 'waiting_payment' => 'pending',
- 'processing' => 'processing',
- 'not_authorized', 'with_error', 'failed',
- 'underpaid', 'chargedback' => 'failed',
- 'voided', 'partial_void', 'canceled',
- 'cancelled', 'refunded', 'partial_refunded',
- 'partial_canceled' => 'cancelled',
- default => 'pending',
- };
- }
- private function resolveAuthorizedAt(?string $transactionStatus, array $transaction): ?string
- {
- if (in_array($transactionStatus, ['authorized_pending_capture', 'captured', 'partial_capture'], true)) {
- return $this->filledArrayValue($transaction, 'created_at');
- }
- return null;
- }
- private function validateItems(array $items): array
- {
- return collect($items)
- ->map(function (array $item, int $index) {
- foreach (['code', 'amount', 'quantity'] as $field) {
- if (! array_key_exists($field, $item) || ! $this->filled($item[$field])) {
- throw new \InvalidArgumentException("items.{$index}.{$field} e obrigatorio.");
- }
- }
- if ((int) $item['amount'] <= 0 || (int) $item['quantity'] <= 0) {
- throw new \InvalidArgumentException("items.{$index}.amount e quantity devem ser maiores que zero.");
- }
- return $this->filterFilledRecursive($item);
- })
- ->values()
- ->all();
- }
- }
|