PagarmeOrderRequestData.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Data\Pagarme\Request\PagarmeOrderRequestData;
  3. use App\Data\Pagarme\PagarmeData;
  4. use App\Data\Pagarme\Request\PagarmeCustomerRequestData\PagarmeCustomerRequestData as CustomerData;
  5. use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderCreditCardData;
  6. use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderPaymentData;
  7. use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderPixData;
  8. use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderSplitData;
  9. use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderSplitOptionsData;
  10. use App\Models\PaymentSplit;
  11. use Illuminate\Support\Collection;
  12. final readonly class PagarmeOrderRequestData extends PagarmeData
  13. {
  14. /**
  15. * @param PagarmeOrderItemData[] $items
  16. * @param PagarmeOrderPaymentData[] $payments
  17. */
  18. public function __construct(
  19. public string $code,
  20. public array $items,
  21. public array $payments,
  22. public array $metadata,
  23. public ?string $customerId = null,
  24. public ?string $channel = null,
  25. public bool $closed = true,
  26. public ?CustomerData $customer = null,
  27. ) {
  28. self::requireFilled($this->code, 'code');
  29. if (empty($this->items)) {
  30. throw new \InvalidArgumentException('items nao pode estar vazio.');
  31. }
  32. if (empty($this->payments)) {
  33. throw new \InvalidArgumentException('payments nao pode estar vazio.');
  34. }
  35. if (! $this->customerId && ! $this->customer) {
  36. throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
  37. }
  38. }
  39. public static function fromOrderPayload(
  40. string $code,
  41. array $items,
  42. PagarmeOrderPaymentData $paymentMethod,
  43. array $metadata,
  44. mixed $customerId = null,
  45. bool $closed = true,
  46. ?string $channel = null,
  47. CustomerData $customer,
  48. ): self {
  49. if (empty($items)) {
  50. throw new \InvalidArgumentException('items nao pode estar vazio.');
  51. }
  52. $customerIdPayload = self::filled($customerId) ? (string) $customerId : null;
  53. if (! $customerIdPayload && ! $customer) {
  54. throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
  55. }
  56. return new self(
  57. code: $code,
  58. items: self::validateItems($items),
  59. payments: [$paymentMethod],
  60. metadata: $metadata,
  61. customerId: $customerIdPayload,
  62. closed: $closed,
  63. channel: $channel,
  64. customer: $customer,
  65. );
  66. }
  67. //
  68. public static function amountInCents(float $amount): int
  69. {
  70. return (int) round($amount * 100);
  71. }
  72. /**
  73. * @param PagarmeOrderSplitData[]|null $split
  74. */
  75. public static function creditCardPaymentMethod(PagarmeOrderCreditCardData $creditCard, ?array $split = null): PagarmeOrderPaymentData
  76. {
  77. return PagarmeOrderPaymentData::creditCard($creditCard, $split);
  78. }
  79. /**
  80. * @param PagarmeOrderSplitData[]|null $split
  81. */
  82. public static function pixPaymentMethod(PagarmeOrderPixData $pix, ?array $split = null): PagarmeOrderPaymentData
  83. {
  84. return PagarmeOrderPaymentData::pix($pix, $split);
  85. }
  86. /**
  87. * @param Collection<PaymentSplit> $transfers
  88. * @return PagarmeOrderSplitData[]
  89. */
  90. public static function splitFromTransfers(Collection $transfers): array
  91. {
  92. return $transfers
  93. ->filter(fn (PaymentSplit $split) => ! empty($split->gateway_transfer_target_reference))
  94. ->map(function (PaymentSplit $split) {
  95. return new PagarmeOrderSplitData(
  96. amount: self::amountInCents((float) $split->gross_amount),
  97. recipientId: $split->gateway_transfer_target_reference,
  98. type: 'flat',
  99. options: new PagarmeOrderSplitOptionsData(
  100. chargeProcessingFee: false,
  101. chargeRemainderFee: false,
  102. liable: false,
  103. ),
  104. );
  105. })
  106. ->values()
  107. ->all();
  108. }
  109. //
  110. public function toArray(): array
  111. {
  112. return $this->filterFilledRecursive([
  113. 'code' => $this->code,
  114. 'items' => $this->items,
  115. 'payments' => $this->payments,
  116. 'closed' => $this->closed,
  117. 'metadata' => $this->metadata,
  118. 'customer_id' => $this->customerId,
  119. 'channel' => $this->channel,
  120. 'customer' => $this->customer,
  121. ]);
  122. }
  123. //
  124. private static function filled(mixed $value): bool
  125. {
  126. return $value !== null && $value !== '' && $value !== [];
  127. }
  128. private static function validateItems(array $items): array
  129. {
  130. return collect($items)
  131. ->map(function (PagarmeOrderItemData $item) {
  132. return $item;
  133. })
  134. ->values()
  135. ->all();
  136. }
  137. }