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 $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(); } }