| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace App\Data\Pagarme\Request\PagarmeOrderRequestData;
- use App\Data\Pagarme\PagarmeData;
- use App\Data\Pagarme\Request\PagarmeCustomerRequestData\PagarmeCustomerRequestData as CustomerData;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderCreditCardData;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderPaymentData;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderPixData;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderSplitData;
- use App\Data\Pagarme\Request\PagarmeOrderRequestData\PagarmeOrderPaymentData\PagarmeOrderSplitOptionsData;
- use App\Models\PaymentSplit;
- use Illuminate\Support\Collection;
- final readonly class PagarmeOrderRequestData extends PagarmeData
- {
- /**
- * @param PagarmeOrderItemData[] $items
- * @param PagarmeOrderPaymentData[] $payments
- */
- public function __construct(
- public string $code,
- public array $items,
- public array $payments,
- public array $metadata,
- public ?string $customerId = null,
- public ?string $channel = null,
- public bool $closed = true,
- public ?CustomerData $customer = null,
- ) {
- self::requireFilled($this->code, 'code');
- if (empty($this->items)) {
- throw new \InvalidArgumentException('items nao pode estar vazio.');
- }
- if (empty($this->payments)) {
- throw new \InvalidArgumentException('payments nao pode estar vazio.');
- }
- if (! $this->customerId && ! $this->customer) {
- throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
- }
- }
- public static function fromOrderPayload(
- string $code,
- array $items,
- PagarmeOrderPaymentData $paymentMethod,
- array $metadata,
- mixed $customerId = null,
- bool $closed = true,
- ?string $channel = null,
- CustomerData $customer,
- ): self {
- if (empty($items)) {
- throw new \InvalidArgumentException('items nao pode estar vazio.');
- }
- $customerIdPayload = self::filled($customerId) ? (string) $customerId : null;
- if (! $customerIdPayload && ! $customer) {
- throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
- }
- return new self(
- code: $code,
- items: self::validateItems($items),
- payments: [$paymentMethod],
- metadata: $metadata,
- customerId: $customerIdPayload,
- closed: $closed,
- channel: $channel,
- customer: $customer,
- );
- }
- //
- public static function amountInCents(float $amount): int
- {
- return (int) round($amount * 100);
- }
- /**
- * @param PagarmeOrderSplitData[]|null $split
- */
- public static function creditCardPaymentMethod(PagarmeOrderCreditCardData $creditCard, ?array $split = null): PagarmeOrderPaymentData
- {
- return PagarmeOrderPaymentData::creditCard($creditCard, $split);
- }
- /**
- * @param PagarmeOrderSplitData[]|null $split
- */
- public static function pixPaymentMethod(PagarmeOrderPixData $pix, ?array $split = null): PagarmeOrderPaymentData
- {
- return PagarmeOrderPaymentData::pix($pix, $split);
- }
- /**
- * @param Collection<PaymentSplit> $transfers
- * @return PagarmeOrderSplitData[]
- */
- public static function splitFromTransfers(Collection $transfers): array
- {
- return $transfers
- ->filter(fn (PaymentSplit $split) => ! empty($split->gateway_transfer_target_reference))
- ->map(function (PaymentSplit $split) {
- return new PagarmeOrderSplitData(
- amount: self::amountInCents((float) $split->gross_amount),
- recipientId: $split->gateway_transfer_target_reference,
- type: 'flat',
- options: new PagarmeOrderSplitOptionsData(
- chargeProcessingFee: false,
- chargeRemainderFee: false,
- liable: false,
- ),
- );
- })
- ->values()
- ->all();
- }
- //
- public function toArray(): array
- {
- return $this->filterFilledRecursive([
- 'code' => $this->code,
- 'items' => $this->items,
- 'payments' => $this->payments,
- 'closed' => $this->closed,
- 'metadata' => $this->metadata,
- 'customer_id' => $this->customerId,
- 'channel' => $this->channel,
- 'customer' => $this->customer,
- ]);
- }
- //
- private static function filled(mixed $value): bool
- {
- return $value !== null && $value !== '' && $value !== [];
- }
- private static function validateItems(array $items): array
- {
- return collect($items)
- ->map(function (PagarmeOrderItemData $item) {
- return $item;
- })
- ->values()
- ->all();
- }
- }
|