| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Services\Pagarme;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderRequestData;
- use App\Data\Pagarme\Response\PagarmeOrderResponseData\PagarmeOrderResponseData;
- use App\Enums\PaymentSplitStatusEnum;
- use App\Enums\PaymentStatusEnum;
- use App\Models\Payment;
- use App\Models\PaymentSplit;
- use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
- use Illuminate\Support\Str;
- class PagarmePaymentService
- {
- use SendsPagarmeRequests;
- public function createOrderWithCreditCard(
- Payment $payment,
- array $items,
- array $customer,
- array $creditCard,
- array $options = []
- ): array {
- return $this->createOrder(
- payment: $payment,
- items: $items,
- customer: $customer,
- paymentMethod: PagarmeOrderRequestData::creditCardPaymentMethod(
- creditCard: $creditCard,
- split: is_array($options['split'] ?? null) ? $options['split'] : null,
- ),
- options: $options,
- );
- }
- public function createOrderWithPix(
- Payment $payment,
- array $items,
- array $customer,
- array $pix,
- array $options = []
- ): array {
- return $this->createOrder(
- payment: $payment,
- items: $items,
- customer: $customer,
- paymentMethod: PagarmeOrderRequestData::pixPaymentMethod(
- pix: $pix,
- split: is_array($options['split'] ?? null) ? $options['split'] : null,
- ),
- options: $options,
- );
- }
- // criacao de pedidos por metodo de pagamento
- public function createOrder(
- Payment $payment,
- array $items,
- array $customer,
- array $paymentMethod,
- array $options = []
- ): array {
- $requestData = PagarmeOrderRequestData::fromOrderPayload(
- code: $this->ensurePaymentCode($payment),
- items: $items,
- customer: $customer,
- paymentMethod: $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'] ?? []),
- customerId: $options['customer_id'] ?? 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.',
- ));
- $order->requireId();
- return $order->toArray();
- }
- // evita criacao duplicada de pedidos
- private function idempotencyKey(Payment $payment): string
- {
- return "payment-{$payment->id}-schedule-{$payment->schedule_id}";
- }
- private function ensurePaymentCode(Payment $payment): string
- {
- if (! empty($payment->gateway_code)) {
- return $payment->gateway_code;
- }
- $code = 'payment-'.(string) Str::uuid();
- $payment->forceFill(['gateway_code' => $code])->save();
- return $code;
- }
- //
- public function applyGatewayResponseToPayment(Payment $payment, array $orderResponse): Payment
- {
- $order = PagarmeOrderResponseData::fromArray($orderResponse);
- $newStatus = $order->paymentStatus();
- $failureCode = null;
- $failureMessage = null;
- if ($newStatus === PaymentStatusEnum::FAILED) {
- $failureCode = $order->failureCode();
- $failureMessage = $order->failureMessage();
- }
- $payment->forceFill([
- 'gateway_provider' => 'pagarme',
- 'gateway_entity_reference' => $order->gatewayEntityReference(),
- 'gateway_entity_label' => $order->gatewayEntityLabel(),
- 'gateway_operation_reference' => $order->gatewayOperationReference(),
- 'gateway_operation_label' => $order->gatewayOperationLabel(),
- 'status' => $newStatus,
- 'paid_at' => $order->paidAt(),
- 'authorized_at' => $order->authorizedAt(),
- 'gateway_payload' => $orderResponse,
- 'failure_code' => $failureCode,
- 'failure_message' => $failureMessage,
- ])->save();
- $splitStatus = match ($newStatus) {
- PaymentStatusEnum::PAID => PaymentSplitStatusEnum::TRANSFERRED,
- PaymentStatusEnum::FAILED => PaymentSplitStatusEnum::FAILED,
- PaymentStatusEnum::CANCELLED => PaymentSplitStatusEnum::CANCELLED,
- PaymentStatusEnum::AUTHORIZED => PaymentSplitStatusEnum::PROCESSING,
- default => PaymentSplitStatusEnum::PENDING,
- };
- PaymentSplit::query()
- ->where('payment_id', $payment->id)
- ->update(['status' => $splitStatus]);
- return $payment->fresh();
- }
- }
|